简体   繁体   English

QString 的 QList 如何分配给列表视图?

[英]How is a QList of QString assigned to a list view?

I wrote this database front-end program with Qt and I used a C++ function to return the result of each query.我用 Qt 编写了这个数据库前端程序,并使用了一个 C++ 函数来返回每个查询的结果。 However, I'm not able to assign the results to a list view.但是,我无法将结果分配给列表视图。

MyObject.h我的对象.h

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include <QObject>

class MyObject : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QList<QString> val READ val WRITE setVal NOTIFY valChanged)
public:
    void setVal(QList<QString>);
    QList<QString> val() const;
    explicit MyObject(QObject *parent = nullptr);
    Q_INVOKABLE int registernew(int Sno=0, QString Name="NULL",long long PhoneNo=0, QString Country="NULL",QString State="NULL", QString District="NULL", int PhoneLine=0,long long Whatsapp = 0);
    Q_INVOKABLE int querydistrict(QString);
signals:
    void valChanged();
private:
    QList<QString> m_val;
};

#endif // MYOBJECT_H

MyObject.cpp我的对象.cpp

.............. void MyObject::setVal(QList<QString> list)
    {
        if(list != m_val)
        {
            m_val = list;
        emit valChanged();
    }
}

QList<QString> MyObject::val() const
{
    return m_val;
}....................

main.cpp主程序

    #include <QGuiApplication>
#include<QQmlApplicationEngine>
#include "myobject.h"
#include<QQuickView>
#include<QQmlContext>




int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    ///delete later!!
    qmlRegisterType<MyObject>("io.qt.examples.MyObject", 1, 0, "MyObject");
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);


       return app.exec();
}

page2.ui.qml page2.ui.qml

 ScrollView {
            id: scrollView
            anchors.rightMargin: 20
            anchors.leftMargin: 20
            anchors.bottomMargin: 40
            anchors.topMargin: 200
            anchors.fill: parent
            ListView {
                id: listView
                model: myobject.val

                   delegate: ItemDelegate {
                       text: modelData
                   }
            }

        }

Where am i going wrong?我哪里出错了? The list is never updated with values when i run the program.当我运行程序时,列表永远不会更新值。 It is always blank.它始终是空白的。 But the variable m_val, when I return it in MyObject.cpp and use qDebug to output it, outputs relevant strings.但是变量m_val,当我在MyObject.cpp中返回它并使用qDebug输出它时,输出相关字符串。

You should not use QList<QString> but QStringList as it is registered.您不应该使用QList<QString>而是使用QStringList因为它已注册。 On the other hand if you are going to establish the data from C ++ since you indicate that it is the result of a query then that Q_PROPERTY is not of being written in QML.另一方面,如果您要从 C++ 建立数据,因为您指出它是查询的结果,那么Q_PROPERTY不是用 QML 编写的。 Finally, although it is not an error, it is better to register the classes before creating the QXApplication .最后,虽然这不是错误,但最好在创建QXApplication之前注册类。

myobject.h我的对象.h

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include <QObject>

class MyObject : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QStringList val READ val NOTIFY valChanged)
public:
    explicit MyObject(QObject *parent = nullptr);
    QStringList val() const;
    void setVal(QStringList val);
    // other methods
signals:
    void valChanged(QStringList val);
private:
    QStringList m_val;
};

#endif // MYOBJECT_H

myobject.cpp myobject.cpp

#include "myobject.h"

// other methods

QStringList MyObject::val() const{
    return m_val;
}

void MyObject::setVal(QStringList val)
{
    if (m_val == val)
        return;
    m_val = val;
    emit valChanged(m_val);
}

main.cpp主程序

// ...
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    qmlRegisterType<MyObject>("io.qt.examples.MyObject", 1, 0, "MyObject");
    QGuiApplication app(argc, argv);
    // ...

So when you do your query you must pass the result to setVal:因此,当您执行查询时,您必须将结果传递给 setVal:

setVal(your_QStringList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM