Qt ListView 不显示C++模型内容

Qt ListView doesn't show C++ model content

本文关键字:模型 C++ 显示 ListView Qt      更新时间:2023-10-16

我正在创建一个QList<>,C++使用QML ListView来显示它。应用程序运行没有错误,但 ListView 顽固地保持为空。

QML 将显示每个列表项存在的矩形。我通过在 QML 中创建列表来检查 UI 代码。它正确显示 QML 创建的列表。

这是我的QML:

import Processes 1.0
...
ListView {
    id: qInterfaceList
    height: parent.height;
    width: parent.width;
    model: myModel
    orientation: ListView.Vertical
    delegate:
        Rectangle {
            height: 30;
            width: 120;
            border.color: "red"
            border.width: 3
        }

创建和注册列表对象的C++代码:

// Register C++ classes as a QML type named Processes (version 1.0)
qmlRegisterType<Process>("Processes", 1, 0, "Process");
QQmlApplicationEngine engine;
// read the configuration file
Config conf;
if ( conf.read() )
{
    QQmlContext* ctxt = engine.rootContext();
    if ( ctxt )
    {
        qDebug()
            << "--- conf.Interfaces: "
            << conf.Interfaces.length()
            ;
        ConfigInterface c;
        QVariant v = QVariant::fromValue( conf.Interfaces );
        qDebug()
            << "--- ConfigInterface: "
            << v
            << "--- typeName: "
            << v.typeName()
            ;
        ctxt->setContextProperty("myModel", QVariant::fromValue( conf.Interfaces ));
    }
}
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
    return -1;
return app.exec();

要调试我从C++和 QML 输出有关列表的信息:在C++中,列表项的计数是正确的。C++,转换为QVariant正在发挥作用。在 QML 中,它看到定义的列表。

调试输出:

Debugging starts
--- conf.Interfaces:  65
--- ConfigInterface:  QVariant(QList<ConfigInterface*>, ) --- typeName:  QList<ConfigInterface*>
qml: myModel: QVariant(QList<ConfigInterface*>)
Debugging has finished

任何想法出了什么问题或如何调试?

谢谢

编辑:这是用作列表项的类

类声明:

class ConfigInterface : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString sql READ getTag WRITE setTag NOTIFY tagChanged)
    Q_PROPERTY(QString description READ getDescription WRITE setDescription NOTIFY descriptionChanged)
public:
    /*explicit*/ ConfigInterface();
    /*explicit*/ ConfigInterface(QObject *parent);
    ~ConfigInterface();
    // Copy constructor needed because these are copied when added to a QList
    ConfigInterface(const ConfigInterface &p2) {_tag = p2._tag; _description = p2._description; }
    QString getDescription() const;
    void setDescription(QString&);
    QString getTag() const;
    void setTag(QString&);
signals:
    void tagChanged(QString);
    void descriptionChanged(QString);
public:
    QString _tag;
    QString _description;
    QString QueryTemplate;
    QString ConnectString;
    QString MinimumId;
};
Q_DECLARE_METATYPE(ConfigInterface*)

C++代码:

ConfigInterface::ConfigInterface()
    : QObject( nullptr )
{
}
ConfigInterface::ConfigInterface(QObject* parent)
    : QObject(parent)
{
}
ConfigInterface::~ConfigInterface()
{
}
QString ConfigInterface::getTag() const
{
    return _tag;
}
void ConfigInterface::setTag(QString& str)
{
    _tag = str;
    emit tagChanged(_tag);
}

主要问题造成是因为它有一个ConfigInterface *列表,根据文档中提供的示例应该是一个QObject *列表:

class Config{
    [...]
public:
    QList<QObject *> Interfaces;
    [...]
};

除此之外,您还应该收到以下警告:

/..../configinterface.h:17: warning: base class ‘class QObject’ should be explicitly initialized in the copy constructor [-Wextra]
     ConfigInterface(const ConfigInterface &p2) {_tag = p2._tag; _description = p2._description; }
     ^~~~~~~~~~~~~~~

这是因为QObject及其派生类不得具有复制构造函数或赋值运算符,有关详细信息,请阅读以下内容:

  • http://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operator

另一个改进是两个构造函数只能合并在一个构造函数中,最终它们的类可以具有以下结构:

配置接口.h

#ifndef CONFIGINTERFACE_H
#define CONFIGINTERFACE_H
#include <QObject>
class ConfigInterface : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString sql READ getTag WRITE setTag NOTIFY tagChanged)
    Q_PROPERTY(QString description READ getDescription WRITE setDescription NOTIFY descriptionChanged)
public:
    ConfigInterface(QObject *parent=Q_NULLPTR);
    ~ConfigInterface();
    QString getTag() const;
    void setTag(const QString &tag);
    QString getDescription() const;
    void setDescription(const QString &description);
signals:
    void tagChanged(QString);
    void descriptionChanged(QString);
private:
    QString _tag;
    QString _description;
    QString QueryTemplate;
    QString ConnectString;
    QString MinimumId;
};
#endif // CONFIGINTERFACE_H

配置接口.cpp

#include "configinterface.h"
ConfigInterface::ConfigInterface(QObject* parent)
    : QObject(parent)
{
}
ConfigInterface::~ConfigInterface()
{
}
QString ConfigInterface::getDescription() const
{
    return _description;
}
void ConfigInterface::setDescription(const QString &description)
{
    if(_description == description)
        return;
    emit descriptionChanged(description);
    _description = description;
}
QString ConfigInterface::getTag() const
{
    return _tag;
}
void ConfigInterface::setTag(const QString &tag)
{
    if(tag == _tag)
        return;
    emit tagChanged(tag);
    _tag = tag;
}