1

My question is best clarified by an example. I have QML with a Text{} item. In C++ I can get to this item and I have no problem using qobject_cast to turn anything into a QQuickItem*. But how do I turn it into the closest corresponding item so that I can call more specific methods directly like setText() the same way I might call setWidth()? I realize I can use the generic setProperty() method but I'm after the compile time checking that casting offers.

I'm after a more general answer for finding the correspondence between QML and their C++ classes, so that I can find out how to do this for Rectangles, MenuBars etc. I can't seem to find this in the docs. For those that prefer code examples:

auto text_object = app_item->findChild<QObject*>("myTextArea");
text_object->setProperty("text","New Text set from Code"); //THIS WORKS BUT...
auto text_qitem = qobject_cast<QQuickItem*>(text_object);
text_qitem->setWidth(128);
auto text_quick_text = qobject_cast<WHATGOESHERE???*>(text_object);
text_quick_text->setText("new Text for qml item");  //I WANT TO DO THIS
László Papp
  • 51,870
  • 39
  • 111
  • 135
Tod
  • 8,192
  • 5
  • 52
  • 93
  • you are assuming that there's a native c++ class for each qtquick control, could it be that is not the case.. – perencia May 02 '14 at 20:36
  • I'm just hoping if that is the case that there exists documentation somewhere stating what correspondence does exist. For example, I can cast the 0th element of the engine.rootObjects to a QQuickWindow. But I figured that out by guessing. – Tod May 02 '14 at 21:09
  • @perencia: there is... – László Papp May 02 '14 at 22:04
  • 1
    @Tod: you [can get the classname](https://qt-project.org/doc/qt-5/qmetaobject.html#className), but I am not sure how to turn the string into object cast, to be honest. – László Papp May 02 '14 at 22:13
  • Well that gave me the idea to just use the debugger. I can see all the children of the app and the Text{} item is of type QQuickText. Unfortunately I can't find an include that will let me cast to this. – Tod May 03 '14 at 00:00
  • You could build a static Lut. – László Papp May 03 '14 at 05:09
  • Can I ask you to clarify why you need such? Perhaps you should change the way. – S.M.Mousavi Aug 09 '22 at 00:44

2 Answers2

1

Q: but I'm after the compile time checking that casting offers.

qobject_cast does not offer any compilation-time checking. It is all runtime and dynamic, thus this request is not plausible. The context property is fine, or you could also get the class name with QMetaObject. Then, you could build a static LUT, but the effort may not be worth it overall...

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • OK, and it seems I can get the same type of safety from QMetaObject. I'm really just interested in understanding all this better. I would still like to know what I would use to cast something to a Text{}. Really I want to know in general how to find in the documentation the correspondence between a Qt Quick class like Text and the C++ equivalent. Maybe it isn't documented because it's starting to appear though that even though I saw this technique in the docs, doing things this way is not the proper technique. – Tod May 05 '14 at 17:32
0

All QML properties and methods are exposed to the meta-object system and can be called from C++ using Object::setProperty and QMetaObject::invokeMethod() respectively. invokeMethod parameters and return values passed from QML are always translated into QVariant values in C++:

QString msg("That's it");

auto text_object = app_item->findChild<QObject*>("myTextArea");

if (text_object)
    QMetaObject::invokeMethod(text_object, "append", Q_ARG(QString, msg));
dnm_t
  • 1
  • 1
  • This answer was reviewed in the [Low Quality Queue](https://stackoverflow.com/help/review-low-quality). Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than existing answers. [From Review](https://stackoverflow.com/review/low-quality-posts/32411771) – Trenton McKinney Aug 04 '22 at 19:48