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

2022-01-19 00:00:00 qt qml listview c++ qstringlistmodel

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

So I'm new to Qt and I'm trying to improve my C++ skills, so I decided to start a project where I can search items in a QStringList using a textfield. I got the search function working and I was able to move the result of the search into another QStringList, where I can use it to display to the user in a function that is declared as a "public slot".

主要想法是,一旦用户在文本字段中输入字符,列表就会自动更新,它已经这样做了.所以我设法将结果列表放入 Slot 函数中,以便每次都能显示不同的列表,并且在文本字段中输入字符.

The main Idea is that the list will auto update as soon as the user enters a character into the textfield, which it already does. So I managed to get the resulted list into the Slot function to be able to display a different list every time and character gets typed in the textfield.

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

In the function where I pass in the list of search results, I am trying to use this

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

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

where resultModel is the name of my model in QML and m_resultList is where the results of the search are being stored, to display the list in the ListView. My program compiles but it crashes after I run it.

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

So, my true question is: Is there any way that I can display a C++ QStringList not in the main.cpp into a QML ListView?

我要求它不在 main 中的原因是因为我尝试在 main.cpp 中使用上面的同一行和硬编码的 QStringList 并且列表能够显示,所以一定有一个问题,它不是主要的.也因为我无法使用 SearchClass 中的 slot 函数来自动更新.

The reason why I'm asking for it to not be in the main is because I tried to use that same line above in the main.cpp with a hard coded QStringList and the list was able to display, so there must be an issue with it not being in the main. Also because I would not be able to use the slot function in SearchClass to auto update.

ma??in.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;

}

ma??in.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
            }
        }
    }

}

推荐答案

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

You are doing it wrong. In every possible way. You even name getContext() the function that actually sets the context.

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

m_resultList is never set to anything in that code you have provided. So there is no way to tell you why your application is crashing, because the actual data is a mystery.

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

You also have a QObject derived class - your SearchClass. So you should expose that as a context property, and then have the string list interfaced to QML by being implemented as a Q_PROPERTY of 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,用作列表视图的模型源.

As you type the text string is sent to Test::setModel(), which then splits it into space separated tokens and sets the QStringList, which is used as a model source for the list view.

相关文章