所以我是 Qt 的新手,我正在努力提高我的 C++ 技能,所以我决定开始一个项目,我可以在其中使用文本字段搜索 QStringList 中的项目。我使搜索功能正常工作,并且能够将搜索结果移动到另一个QStringList
,在那里我可以使用它在声明为“公共插槽”的函数中向用户显示。
主要想法是,一旦用户在文本字段中输入字符,列表就会自动更新,它已经这样做了。所以我设法将结果列表放入 Slot 函数中,以便每次都能显示不同的列表,并且在文本字段中输入字符。
在我传入搜索结果列表的函数中,我正在尝试使用它
m_context->setContextProperty("resultModel",QVariant::fromValue(m_resultList));
whereresultModel
是我的模型在 QML 中的名称,并且m_resultList
是存储搜索结果的位置,以在ListView
. 我的程序可以编译,但在我运行后它崩溃了。
所以,我真正的问题是:有什么方法可以将不在 main.cpp 中的 C++ QStringList 显示到 QML ListView 中?
我要求它不在 main 中的原因是因为我尝试在 main.cpp 中使用上面的同一行并进行硬编码QStringList
并且列表能够显示,所以它一定有问题不是主要的。也因为我无法使用插槽功能SearchClass
进行自动更新。
主文件
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QDebug>
#include "searchclass.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<SearchClass>("b9c.backend", 1, 0, "BackEnd");
QQmlApplicationEngine engine;
SearchClass obj;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QQmlContext *context = engine.rootContext();
obj.getContext(context);
//the line below works if provided with a qstringlist
//context->setContextProperty("resultModel", QVariant::fromValue(resultList));
return app.exec();
}
搜索类.h
#ifndef SEARCHCLASS_H
#define SEARCHCLASS_H
#include <QObject>
#include <QQmlContext>
class SearchClass : public QObject
{
Q_OBJECT
Q_PROPERTY(QString userSearch READ userSearch WRITE setUserSearch NOTIFY userSearchChanged)
public:
SearchClass(QObject *parent = 0);
QStringList resultList;
QString userSearch();
void setUserSearch(QString &userSearch);
void getFilenameAndInput(QString inputString);
QString CompareInputAndFilename(QString inputString, QString filename);
QStringList getFileName();
//get context
void getContext(QQmlContext *context);
signals:
void userSearchChanged();
public slots:
void setUserSearch();
private:
QStringList m_resultList;
QString m_userSearch;
QQmlContext* m_context;
};
#endif // SEARCHCLASS_H
搜索类.cpp
#include "searchclass.h"
#include <QDebug>
#include <QQmlContext>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
SearchClass::SearchClass(QObject *parent) : QObject(parent)
{
connect(this, SIGNAL(userSearchChanged()), this, SLOT(setUserSearch()));
}
//the result should be displayed in this SLOT when ever the user types in a character into the textfield
void SearchClass::setUserSearch(){
qDebug() << "SLOT: " << m_resultList;
//The line below makes the program crash. It works when implemented in the main.cpp
// m_context->setContextProperty("resultModel", QVariant::fromValue(m_resultList));
}
QString SearchClass::userSearch()
{
return m_userSearch;
}
void SearchClass::setUserSearch(QString &userSearch)
{
if (userSearch == m_userSearch)
return;
m_userSearch = userSearch;
qDebug() << "Input: " <<m_userSearch;
getFilenameAndInput(m_userSearch);
emit userSearchChanged();
}
QStringList SearchClass::getFileName(){
//Returns the items that will be searched for...
}
void SearchClass::getFilenameAndInput(QString inputString){
//Puts the search results into class variable m_resultList...
m_resultList = resultList;
}
QString SearchClass::CompareInputAndFilename(QString inputString, QString filename){
//Search processing...
}
//gets context to use setProperty in the above signal, but it crashes
void SearchClass::getContext(QQmlContext *context){
m_context = context;
}
main.qml
import QtQuick 2.6
import QtQuick.Controls 2.0
import b9c.backend 1.0
import QtQuick.Window 2.2
ApplicationWindow {
id: root
width: 300
height: 480
visible: true
BackEnd { id: backend }
TextField {
id: txtfield
text: backend.userSearch
placeholderText: qsTr("Search...")
width: parent.width
onTextChanged: backend.userSearch = text
}
ListView {
id:view
height: parent.height
width: parent.width
y: 5 + txtfield.height
model: resultModel
delegate: Rectangle {
border.color: "lightblue"
height: 25
width: parent.width
Text {
anchors.centerIn: parent
text: modelData
}
}
}
}