Qt ListView 不显示C++模型内容
Qt ListView doesn't show C++ model content
我正在创建一个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;
}
相关文章:
- QTableView:endMoveRows在模型中重置水平页眉大小
- 将IBM Rhapsody模型集成到VS 2019中
- 旋转模型矩阵时的形状失真
- 成员函数调用和C++对象模型
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 打印 ONNXRUNTIME::图形没有模型
- C++内存模型和位字段的最大序列
- 使用SIR模型的疾病爆发模拟
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- 如何在 ECS 模型中组织实体?
- 在官方张量流 resnet50 模型上运行 tflite 精度工具
- Libtorch:如何加载ONNX模型?
- 使用 assimp 加载模型 - 不需要提升?
- 如何将经过训练的机器学习模型保存在python中并将其加载到C++中进行预测?
- C++内存模型中的确切规则阻止在获取操作之前重新排序
- 如何更改古罗比C++模型中的目标值系数
- 如何在 openGL 中围绕全局轴旋转模型?
- 我们可以将阈值应用于色彩空间模型的单个组件(如 RGB 和 LAB)吗?
- ASIMP 模型加载不会加载所有网格
- 平方模型为零,阶乘模型问题