如何在C ++中将QStringList显示到QML ListView

时间:2017-07-28 15:50:55

标签: c++ qt listview qml qstringlistmodel

所以我是Qt的新手,我正在努力提高我的C ++技能,所以我决定启动一个项目,我可以使用文本字段在QStringList中搜索项目。我使搜索功能正常工作,我能够将搜索结果移动到另一个QStringList,我可以使用它在一个声明为“公共插槽”的函数中向用户显示。

主要的想法是,一旦用户将字符输入到文本字段中,该列表就会自动更新。因此,我设法将结果列表放入Slot函数,以便每次都能显示不同的列表,并在文本字段中输入字符。

在我传入搜索结果列表的函数中,我正在尝试使用此

m_context->setContextProperty("resultModel",QVariant::fromValue(m_resultList));

其中resultModel是我在QML中的模型名称,m_resultList是存储搜索结果的位置,以显示ListView中的列表。我的程序编译但运行后它崩溃了。

所以,我的真正问题是:有没有办法可以将main.cpp中的C ++ QStringList显示到QML ListView中?

我要求它不在主要部分的原因是因为我试图在main.cpp中使用上面的同一行,并使用硬编码QStringList并且列表能够显示,所以必须有一个问题,它不是主要的。另外,因为我无法使用SearchClass中的插槽功能自动更新。

的main.cpp

#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();
}

SearchClass.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

SearchClass.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
            }
        }
    }

}

1 个答案:

答案 0 :(得分:3)

你做错了。百般。您甚至可以将{{>>设置上下文的函数命名为getContext()

m_resultList永远不会设置为您提供的代码中的任何内容。所以没有办法告诉你为什么你的应用程序崩溃,因为实际的数据是一个谜。

您还有一个QObject派生类 - 您的SearchClass。因此,您应该将其公开为上下文属性,然后通过实现为Q_PROPERTY的{​​{1}}将字符串列表与QML接口。

这是一个简单的例子:

SearchClass

在您键入时,文本字符串将发送到// the equivalent of your SearchClass class Test : public QObject { Q_OBJECT Q_PROPERTY(QStringList model MEMBER m_model NOTIFY modelChanged) QStringList m_model; public slots: void setModel(QString m) { m_model = m.split(" "); modelChanged(); } signals: void modelChanged(); }; // in main.cpp Test t; engine.rootContext()->setContextProperty("Test", &t); // in main.qml Column { TextField { onTextChanged: Test.setModel(text) } ListView { width: 200; height: 300 spacing: 5 model: Test.model delegate: Rectangle { height: 25 width: 200 color: "lightgray" Text { text: modelData; anchors.centerIn: parent } } } } ,然后将其拆分为空格分隔的标记并设置Test::setModel()QStringList用作列表视图的模型源。