gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:06:57 24 4
gpt4 key购买 nike

我在 C++ 中创建一个 QList<>,使用 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 *列表,根据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

配置接口(interface).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;
}

关于c++ - Qt ListView 不显示 C++ 模型内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48349672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com