When to use which classes in Qt Quick 2 and how?

I am trying to write a game with Qt 5.1 and Qt Quick 2 and a new (faster) graphics engine. I read the documentation for several hours, but still cannot figure out which classes to use if I want to move elements on the screen whose positions are determined by C ++ code.

In QGraphics, it was simple: I create an instance of QGraphicsScene, adding any QGraphicsItem descendant to it, and then create an instance of QGraphicsView, which is a widget, and displays all the elements and their changes. I connect QGraphicsItem to signals to make changes.

In Qt Quick 2, I first read

  • official examples that used only QML and Javascript,
  • then I thought I found the equivalents of QGraphics, namely QDeclarativeEngine, QDeclarativeComponent and QDelcarativeView and was ready to create custom QML elements with Q_PROPERTY
    • UPDATE I just found out that QDeclarative * is Qt Quick 1 and QQml * is the equivalent Qt Quick 2 prefix.
  • But then I also found QQuickItem, QQuickWindow, QSGNode, etc. .
After reading many textbook documentation, I still don’t know what the “default solution” is. Each textbook shows something else. I am amazed at all the examples and classes.
  • Can someone please give me a basic example based on Qt Quick 2 and where are the images on the canvas moving along signals that are sent from regular QObjects written in C ++?
  • Could you help me classify all the classes that I talked about. What do I use when?
+6
source share
2 answers

I think you are confusing things at different levels, and this makes this question a "difficult" question, which should be divided into smaller questions ...

Anyway:

  • if you need a QWindow that can host QtQuick 2 content , you will need a QQuickView or QQuickWindow (usually the first, more convenient, see their docs).
  • QtQuick2 is not a QGraphicsView based . This is not even in QtWidgets - you cannot use any class associated with widgets or APIs.
  • The easiest way to bind a property of a QML element to one of the C ++ objects is to simply expose this object to the QML mechanism, and then perform the usual binding.

For instance:

 class MyObject : public QObject { Q_OBJECT Q_PROPERTY(int horizontalPos READ horizontalPos NOTIFY horizontalPosChanged) public: int horizontalPos() const { return m_horizontalPos; } signals: void horizontalPosChanged(); // etc. } 

Then you can open an instance of MyObject for the QML mechanism:

 MyObject obj; QQuickView view; // exposes the object under the "_myObject" name view.engine()->context()->setContextProperty("_myObject", &obj); 

The underline is a nice touch to emphasize the fact that this name comes from the C ++ world.

Finally, in QML, you can simply bind to a property:

 Rectangle { x: _myObject.horizontalPos // voilà, they're bound together } 
+6
source

You can dynamically create custom qml components from C ++ and change properties still from C ++.

You can also just work with some C ++ lists / models / several q objects containing the positions of your sprites / characters / everything that you display on the screen, and then bind this list to your qml side.

You can also write some QQuickItem from C ++ that expands its children (for example, it is suggested here: http://qt-project.org/forums/viewthread/29407/ )

-

And here are some links complementing peppe answer / detailing how to mix QtQuick2 and C ++:

Document pages for Qt Quick 2 / C ++ integration (Qt5 document is not indexed by Google):

Blog post explaining QWidget :: createWindowContainer (this is only if you want to embed QtQuick2 in a QtWidget application ... otherwise you can just use the skeleton for the QtQuick2 application from QtCreator):

+2
source

Source: https://habr.com/ru/post/950588/


All Articles