Can QSGNode inherit QObject?

188 views Asked by At

Can QSGNode inherit QObject and connect to signals and slots? I have tried but the slot is only called when rendering is done.

I had a matrix of cells. I was using QML to display them on a bi dimensional ListView (a ListView the had in each delegate a ListView). This lacked performance so I changed to SceneGraph. The problem is the data model. I'm now a passing a QList, where column has a QList. Each row has cells the have signals. This cells change. I want to notify render updates on those signals. My solution until I have a better one is to make each cell trigger a changed signal on the respective column which by it's turn will turn the update flag on the main QQuickItem implementing the scene.

Ideas?

2

There are 2 answers

3
Jonathan Mee On

A QSGNode cannot support signals and slots natively. But if you wanted to you could do double inheritance.

#include <QObject>
#include <QSGNode>

class Foo : public QObject, public QSGNode    {
     Q_OBJECT
     // Your additional implementation here
};
0
Konstantinos Gaitanis On

You should connect the signals from your model to the QQuickItem that handles the QSGNode instead of directly connecting to the QSGNode. QSGNode objects are supposed to be handled only inside the QSGRenderingThread.

QObjects 'live' in only one thread. This is the thread where they emit and receive signals. By making your QSGNode a QObject, you must pay attention to the emitting and receiving threads otherwise you'll get symptoms like the ones you describe. Unless you specify Qt::DirectConnection when you make a connection, if the signal is emitted from one thread and received from an object living in another thread, the signal is converted to a message and will be dispatched later using the QEventLoop.

A good practice is to have a structure like the ones provided in the Qt examples :

QQuickItem handles signal connections and stores properties. Depending on what has changed, QQuickItem sends commands to the QSGNode to recalculate certain parts of its geometry. This is done only through QQuickItem::updatePaintNode()