Qt ListView没有显示C ++模型内容

时间:2018-01-19 21:17:40

标签: c++ qt qml qt5

我正在创建一个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();

要调试I输出有关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);
}

1 个答案:

答案 0 :(得分:1)

主要问题是因为它有一个ConfigInterface *列表,根据文档中提供的examples应该是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及其派生类不能有复制构造函数或赋值运算符,有关更多信息,请阅读以下内容:

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

<强> configinterface.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

<强> configinterface.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;
}