C++ and QML Integration
-
Hello,
Currently i am building a game using Qt Quick and i am using QQuickView and QQuickItem to create instances of my objects from QML files, for example i have a game character "image in qml file" and i am loading it using QComponent and QQuickItem and then i add it to the parent "QQuickView", now i want this character to respond to my keyboard events.
-
Hi @AhmedYossef and Welcome to Qt Devnet.
By "using QQuickItem" I'm assuming you have subclassed
QQuickItem
. If so, then in that case you can also re-implement keyPressEvent and keyReleaseEvent. You may need to setfocus
totrue
. -
Thanks for the reply, i did that i sub classed QQuickItem, and in the constructor of the character i created the QQMLComponent and the QQuickItem, then i add the QQuickItem to the parent which is the QQuickView, then i set focus and i set initial X and Y for the character, also i implemented the keyPressEvent, but still nothing happens the character appears in the default X and Y i set before but when i press the key it doesn't go to the event
-
@AhmedYossef Can you post some code ? Did you set focus ?
-
@p3c0
Here is what i do in the constructor of the character
QQmlComponent component(view->engine(), QUrl(QStringLiteral("qrc:/PlayerTank.qml")));
tankItem = qobject_cast<QQuickItem*>(component.create());
tankItem->setParentItem(view->rootObject());tankItem->setX(100); tankItem->setY(100); tankItem->setFocus(true);
tabkItem is a QQuickItem
-
@p3c0
Got it to work finally, i implemented the Keypressevents for the class that subsclassed the QQuickView and it works i can now move the characters