Possible to wrap non-QObject C++ object by an Entrymodel?
-
@Tobias83
I know nothing about QML orEntrymodel
, so excuse me if there is something special about that. But speaking for Qt at least there is no "continuous process generating" anything, and theparent
argument to aQObject
constructor simply sets up the object model hierarchy for parent/child, there is no "copying of state" between parent & child (unless you code something yourself). -
@Tobias83 Is
Entrymodel
something specific to your project? I can't see anything related to Qt with this name. If it is something specific, nobody here is going to know what this is. It is difficult to understand what you are asking about. Is there any way that you can try to extract the essence of the issue that you are having without discussing specific details of your application? To me, it feels like there is probably a solution to what you want to do but we need a better way to understand it. -
@Tobias83 OK, so
Entrymodel
is just part of the name of your class which implements aQAbstractListModel
.How is your list model used in your application? If you want to expose it to QML so that you can actually instantiate it in QML, like this
ListModel { model: Hardcore_EntryModel { } ... }
then you are restricted to not being able to pass arguments into your constructor.
On the other hand if you want to be able to instantiate your model in C++ and just make that instance accessible in QML, you are potentially less restricted. In your C++ you can instantiate your
Hardcore_EntryModel
however you want and useqmlRegisterSingletonInstance
:Hardcore_EntryModel entryModel(arg1, arg2, ...); qmlRegisterSingletonInstance("mytypes", 1, 0, "EntryModel", &entryModel);
and then use it in your QML:
ListModel { model: EntryModel // use the singleton instance exposed in the C++ ... }
-
@Bob64 Dear Bob64,
How do I have to handle this contructor:explicit Hardcore_Entrymodel(QObject *parent=0, System* system);
Is the =0 correct?
If I call it like this, it errors:tw_hardcoresystem::Hardcore_EntryModel entryModel(nullptr, &system);
It also errors with this constructor, because there is a QAbstractListModel(parent) which needs the parent argument:
explicit Hardcore_Entrymodel(System* system);
Thank you for your Help!
Tobias
-
@Tobias83 said in Possible to wrap non-QObject C++ object by an Entrymodel?:
explicit Hardcore_Entrymodel(QObject *parent=0, System* system);
Per C++, any optional/default-value parameters can only come after any mandatory ones. Your code is not legal because a parameter without a default comes after one with a default. Either give a default for
System* system
as well, or (preferable IMO) make thesystem
parameter come before theparent
one. I would always leave aQObject
-derived constructor takingQObject *parent = nullptr
as the last argument. -
@Tobias83 I now found an a little bit different solution. In stackoverflow someone suggested set and get functions. So I have:
public: explicit Hardcore_Entrymodel(); //explicit Hardcore_Entrymodel(QObject *parent, System* system); void setSystem(System* system);
and in the main fct:
Hardcore_Entrymodel entryModel; entryModel.setSystem(&system); qmlRegisterSingletonInstance("hardcoresystem", 1, 0, "Hardcoreentrymodel", &entryModel);
For this, it draws a window and the "QML Object is not creatable" error disappears.
But there are still some little mistake, which are not related to Qt, I think.
Thanks a lot until here!!!
kind regards,
TobiasPS: if the std::stof function in one case outputs the double value of a string, and some loops later gives the integer value - it is not related to Qt? What can this be? A corrupt compiler?
-
@Tobias83
std::stof()
has nothing to do with Qt, you do not have a "corrupt compiler" and even if you did that would not change its behaviour according to how many times it is called. It returns afloat
. It does not return anint
, though of course a value returned might happen to be an integer (as in, no decimal places). -
Does the QAbstractListModel work the same as Singleton if data is changed. In QML I have a Repeater which draws the particles. But if I do a simulation step, the picture in in the window is not updated, but the coordinates in cpp are. On the console I get the output qml: DataChanged received.
This my step method:
void Hardcore_Entrymodel::doNewStep() { emit newStepDoing(); hardcoresystem->testStep(); populate(); return; }
and populate looks like that:
void Hardcore_Entrymodel::populate() { int ipart; for(ipart=0; ipart<nparticles; ipart++){ mDatas[ipart]= initqtview.SystemToWindow(hardcoresystem->returnIParticle(ipart)); } emit dataChanged(index(0),index(nparticles)); return; }
And the step is made by mouseclick in qml:
MouseArea { anchors.fill: parent onClicked: Hardcoreentrymodel.doNewStep() }