QQuickItem* object. "Expression must have class type" when using
-
hi,
I'm trying to convert this python code in C++
def setPresetBase( self ): object = self.view.rootObject() items = object.childItems() parameters = [] for i in items: if type(i) == QtQuick.QQuickItem and i.objectName() != "" and i.objectName() != "EditPanel" and i.objectName() != "EditGrid": parameters.append( [ i.objectName(), i.x(), i.y(), i.z(), i.width(), i.height(), i.isVisible() ] ) self._preset_base = Preset( "base", parameters )
i started to write this but i can't use w_object, the visual studio tells me that the expression must have a class type. So I can't call childItems().
void PresetModel::setPresetBase(void) { QQuickItem* w_object = mp_view->rootObject(); QList<QQuickItem*> w_items = w_object.childItems(); }
That's just a pointer so I don't need to allocate any QQuickItem, right ? What's wrong ?
-
@Hodenbrecher
I'm not sure, what mp_view is, so I can't say for sure, but my guess is rootObject() returns and actual QObject pointer not a QQuickItem pointer.
So you'll have to cast it:{ QQuickItem* w_object = qObject_cast<QQuickItem*> (mp_view->rootObject()); QList<QQuickItem*> w_items = w_object.childItems(); }
-
@J.Hilk
thanks for your answer.mp_view is a QQuickView*
QQuickView * mp_view; PresetModel::PresetModel(QQuickView * aip_view, QObject * aip_parent/*=nullptr by default*/):mp_view(aip_view) { }
The documentation says that rootObject returns a QQuickitem* : https://doc.qt.io/qt-5/qquickview.html#rootObject
However I tried the qobject_cast and the IDE says
no instance of overloaded function "qobject_cast" matches the argument list
arguments are: (QQuickItem*)@Nifiro
i tried, of course. But because it did not work I tried with the "."
Anyway, visual studio underlines the error before any compilation. Just by checking the code.The error is weird as I can compile
QQuickItem* w_object = mp_view->rootObject(); if (w_object == nullptr);
But if I try to access to it's attribute... NO !!!
[edit]
I can't tell if the pointer is null, there's another error probably not linked.int argc = 0; char** argv=nullptr; QGuiApplication(argc, argv); QQuickView* view = new QQuickView();//in console : "QPixmap: Must construct a QGuiApplication before a QPixmap view->setResizeMode(QQuickView::SizeRootObjectToView); QQmlContext *ctxt = view->rootContext(); view->setSource(QString(QML_GENERIC_VIEW_PATH)); QQuickItem* w_object = view->rootObject(); if (w_object == nullptr)printf("\nthe pointer is null\n"); else printf("\nthe pointer is not null\n"); QList<QQuickItem*> w_items = w_object->childItems();//got the same error here. w_object underlined
-
possible stupid question, but did you
#include <QQuickItem>
? -
@Hodenbrecher
one of the quirks of c++QQuickItem is a forward-declared class
https://en.cppreference.com/w/cpp/language/class#Forward_declaration