Qt's unique patterns - qt

I am working at the moment with Qt and discovered signals and slots. I think this is a Qt specific pattern. I am wondering if there are more Qt specific patterns like signals and slots. Are there any? Is there any specification about them?
In addition:
Qt's specific pattern are splitted in two parts:
Qt providing special classes for using design patterns like the Qt Quick module, Signal Slots..
The second part is the structure of some classes for example: QWidget
QWidget inherits from QObject which implements the composite pattern.
For further information i recommend this book: http://www.amazon.de/Introduction-Patterns-Prentice-Software-Development/dp/0132826453

There are some features which are unique to Qt or at least few implementations exist in other languages.
To name a few other than the signal-slot mechanism :
The Qt Quick module : Enables you to write QML applications and provides everything needed to create a rich application with a fluid and dynamic user interface. Some features are The Visual Canvas, User Input, States, Transitions And Animations, Particles And Graphical Effects, ...
Qt's Undo Framework : It's based on command pattern for implementing undo/redo functionality in applications.
The State Machine Framework : Can be used to effectively embed the elements and semantics of statecharts in Qt applications. It integrates with Qt's meta-object system; for example, transitions between states can be triggered by signals, and states can be configured to set properties and invoke methods on QObjects.

Related

Qt6 C++ ownership of QObject when using QML_ELEMENT

I've started learning QML with Qt6 lately and am trying to apply "best practices" right from the start. When it comes to integrating C++ it seems that a family of macros (QML_ELEMENT and friends) is going to replace the old style *register functions. A best practices talk on the Qt youtube channel calls this "declarative registration". I've tried that in a small example using CMake and it practically worked out of the box.
Now to my knowledge C++ classes exported and instantiated within a QML module have "QML engine" ownership. Lets suppose I'd rather have C++ take ownership of my class and QML merely use it, how would I inform the QML engine about my instance? Do I need to use setContextProperty for that?
I've also wondered what benefits and drawbacks I'd get from letting either C++ or QML handle ownership? There seems to be hardly any information on those topics. I've even browsed a couple of Qt books and not a single one would elaborate on that...

Text in Custom QQuickItem

I am implementing a Custom QQuickItem which should be able to make a lineplot (x vs y). For performance reasons I did this using QSGNodes. I wonder what is the best way to implement the Text-Labels for the x-ticks (they should get set automatically according to the data range).
Should I use a QQuickPaintedItem or is there a way to do it via the QQuickItem?
Or is it possible to dynamically create text qml types ( http://doc.qt.io/qt-5/qml-qtquick-text.html) in my c++ file?
Don't over-complicate things by reinventing wheels. QML already has a Text type.
There is a downside - the decision to not have a public API to use the QML types from C++ in their C++ form. And I would not recommend creating QML objects from C++, it is just counterproductive.
Which means that your custom QML type will not be implemented purely in C++, you can implement the "core" stuff in C++ and still compose the complete type on the QML side, where you also get to use bindings, anchors, models, positioners, dynamic scoping and all that good stuff that is hard to do in C++.
This is not uncommon, lots of the stock QML controls are implemented this way - an abstract C++ core exposed to QML, where it is finished into a complete control in QML syntax.

How to use native OpenGL with Qt 5.4?

I want to use Qt 5.4 to create a window and render with normal OpenGL functions some stuff in that window. In the last few days, I read a lot about the Qt classes and how to initialize OpenGL and so on. I think, the main classes I have to deal with are QOpenGLWindow or QOpenGLWidget, but there are the QSurface and some other classes too. Now I am very unsure about what doing next and which class I should use to use the plain OpenGL functions, later. Can someone explain more clearly to me what I have to do to set up a Qt GUI in which I can use plain OpenGL?
Some other questions from me are:
At which point does Qt create a plain OpenGL context? Do I have to use the QOpenGLContext?
What is exactly the difference between a QSurface and a QOpenGLWindow? In the QOpenGLWindow example both classes are used.
Is it possible to use glew besides this qt stuff? Here are some question on, which deal with setting up glew with qt, but I think that I did not get the real point of why glew is needed.
Edit: I discussed this question with a colleague and our only conclusion was to use Offscreen-Rendering. Does anyone know another solution?
At which point does Qt create a plain OpenGL context? Do I have to use the QOpenGLContext?
Either where it's documented (for instance, creating a QOpenGLWidget or a QOpenGLWindow will automatically create a context), or you can create a context manually at any time by creating a QOpenGLContext object.
What is exactly the difference between a QSurface and a QOpenGLWindow? In the QOpenGLWindow example both classes are used.
A QSurface is a base class representing a "drawable surface" (onscreen or offscreen). QWindow is its onscreen implementation (representing a top level window), so it inherits from QSurface. You can draw over a QWindow by using OpenGL or by using a CPU-based rasterizer.
Finally, QOpenGLWindow is a QWindow subclass which offers some extra functionality and convenience, by automatically creating and managing an OpenGL context (via QOpenGLContext), having an optional partial-update strategy (through the usage of a FBO), etc.
Is it possible to use glew besides this qt stuff? Here are some question on, which deal with setting up glew with qt, but I think that I did not get the real point of why glew is needed.
Qt is not in your way. And it doesn't change your usage of OpenGL in any way. Just use Qt to create a window and a context (in a totally cross platform way), then you're free to use GLEW (to resolve OpenGL function pointers, extensions, etc.) or any 3rd party OpenGL abstraction.

Why does Qt reimplement RTTI in qobject_cast, event type, etc?

Why does Qt bother reimplementing what amounts to a custom RTTI system and a their own dynamic_cast in the QObject hierarchy, in QEvent, etc?
First of all, only a few class hierarchies in Qt actually need RTTI. When you generate embedded code you can save a whole bunch of code space by not emitting RTTI information. When building Qt and suitably written projects that use it, you can turn off RTTI in the compiler. You can't use dynamic_cast anymore, thus QObject hierarchy has its own qobject_cast, and QEvent uses explicit integer type tags.
A custom RTTI system for QObject hierarchy also allows dynamic creation of types from their metadata, as well of the metadata of new types that the compiler was unaware of. That's why QML can work, for example. In Qt 5, this functionality is offered by the private QMetaObjectBuilder. The legacy way of creating dynamic signals and slots wasn't compatible with standard QObject::connect.
Historically, with some compilers dynamic_cast failed across shared library boundaries.

Interface Segregation in Qt

I always try to apply the S.O.L.I.D principles and I really like the Qt toolkit but I find myself stuggeling all the time with the single inheritance rule.
If you are using multiple inheritance,
moc assumes that the first inherited
class is a subclass of QObject.
Also, be sure that only the first inherited class is a QObject.
How do you combine the single inheritance from a QObject rule and the Interface Segregation Principle.
I want to define the interfaces with signals and slots, but I'm not allowed to do this.
How do you get around this shortcomming?
Keep in mind that signals and slots are nothing more than functions that have special behaviors. Thus, you can use them to create interfaces.
For a full description of the process and a complete workaround for complex cases, see Qt Quarterly #15.
I don't think you can easily get around that with Qt's signal/slot mechanisms. You could try looking at either boost::signals or the sigc library, which are both more flexible in where you can place signals and slots. Be aware of possible namespace collisions with either library and Qt's signals and slots macros.

Resources