我尝试使用 Qt6 的一种源代码,原则上它可以与 Qt5 一起使用:我想为我的硬核粒子系统制作图形输出。在 QAbstractListModel 的构造函数中,我有两个参数:System *system, QObject *parent=0 我认为 *system 参数是原因,为什么在 qml 中项目 Hardcore_Entrymodel{} 不可创建(QQmlApplicationEngine 无法加载组件)。
如何让我的 qml 文件再次工作?
我用 qt5 从羊群模拟中移植了模型,原则上是有效的。现在,使用新的硬核模型和 qt6,qml 文件出现错误。我在网上查了一下,感觉是因为两个参数的构造函数而出错。
Hardcore_Entrymodel的头文件:
class Hardcore_Entrymodel : public QAbstractListModel
{
Q_OBJECT
public:
explicit Hardcore_Entrymodel(System*, QObject *parent = 0);
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
void populate();
QHash<int, QByteArray> mPropertyNames;
enum mParticleProperties{
type = Qt::UserRole,
iparticle = Qt::UserRole+1,
size_semimajoraxis = Qt::UserRole+2,
size_semiminoraxis = Qt::UserRole+3,
x_mid_pxl = Qt::UserRole+4,
y_mid_pxl = Qt::UserRole+5,
xy_angle_degree = Qt::UserRole+6,
typecolor = Qt::UserRole+7,
};
Q_INVOKABLE void doNewStep();
Q_INVOKABLE void stepReady();
Q_INVOKABLE int returnWindowLx();
Q_INVOKABLE int returnWindowLy();
Q_INVOKABLE int returnSteptime_ms();
bool isNewStep();
void setIsNewStep(bool newS);
protected:
virtual QHash<int, QByteArray> roleNames() const override;
private:
System *hardcoresystem;
int nparticles;
InitQtView initqtview;
QList<std::tuple<std::string, int,int,int,int,int, QColor> > mDatas;
bool newStep;
signals:
void newStepDoing();
void newStepReady();
public slots:
void onNewStepDone();
};
主要功能:
#include <QObject>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "Hardcore_InitClass.h"
#include "Hardcore_Entrymodel.h"
int main(int argc, char *argv[])
{
Hardcore_InitClass initClass(argc, argv);
System system;
initClass.return_Hardcoresystem_single(&system);
system.writeParametersFile();
QGuiApplication app(argc, argv);
// register the type DataEntryModel
// under the url "org.example" in version 1.0
// under the name "DataEntryModel"
qmlRegisterType<Hardcore_Entrymodel>("org.hardcoresystem", 1, 0, "Hardcore_Entrymodel");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/qt/qml/Main.qml")));
return app.exec();
}
qml 文件的第一部分:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
//import QtQuick.Layouts 2.2
import QtQml.Models 2.12
import Qt.labs.qmlmodels 1.0
import org.hardcoresystem 1.0
Window {
id: appwindow
visible: true
width: hardcoreentrymodel.returnWindowLx()
height: hardcoreentrymodel.returnWindowLx()
Rectangle{
id: hardcoresystemrectangle
anchors.left: appwindow.left
anchors.top: appwindow.top
width: appwindow.width
height: appwindow.height
color: "white"
}
Hardcore_Entrymodel{
id: hardcoreentrymodel
//onNewPathDoing: console.log("New Path doing ...\n")
//onNewPathReady: {console.log("New Path ready ...\n")}
//onDataChanged: console.log("DataChanged received")
}
Timer {
id: steptimer
interval: Number(hardcoreentrymodel.returnSteptime_ms())
//interval: 100
repeat: true
running: true
triggeredOnStart: true
onTriggered: {hardcoreentrymodel.doNewStep()}
}
Item{
id: partilcesitem
width: hardcoresystemrectangle.width
height: hardcoresystemrectangle.height
anchors.left: hardcoresystemrectangle.left
anchors.top: hardcoresystemrectangle.top
Repeater {
[...]
亲切的问候, 托比亚斯
从评论中,我建议从构造函数中删除 System* 并使其成为静态成员,即
// Hardcore_Entrymodel.h
class Hardcore_Entrymodel : public QAbstractListModel
{
Q_OBJECT
public:
explicit Hardcore_Entrymodel(QObject *parent = nullptr);
public:
void SetSystem(System* value) { g_hardcoresystem= value; }
System* GetSystem() { return g_hardcoresystem; }
private:
static System *g_hardcoresystem;
};
初始化你的静态成员...
// Harcode_Entrymodel.cpp
System* Hardcore_Entrymodel::g_hardcodesystem = nullptr;
在 main.cpp 中,您可以填充静态成员,而无需调用构造函数。
/// main.cpp
int main(int argc, char *argv[])
{
System system;
Hardcode_Entrymodel::SetSystem(&sytem);
// ...