1
votes

I have two QQuickItems and I would like to draw one on top of the other in C++ before it is exposed to QML. I would like to do this because I want the second QQuickItem (which I would refer to as background QQuickItem from here on) to act as the background of the first QQuickItem (which I would refer to as parent QQuickItem from here on) and then draw a QPixmap (which has been painted on using QPainter) on top of both items. I would also prefer to pass in the second "background" QQuickItem as a property to the first QQuickItem so that it can be set dynamically from QML. Does anyone have any idea on how to do this?

I would like to mention that both QQuickItems have implementations of QQuickItem::updatePaintNode(). Is there a way to make them work together?

1
It seems to be Z order problem: stackoverflow.com/questions/31527916/…Alexander V

1 Answers

1
votes

To do that, you have to use the visual parent property (note that in c++ you access it with setParentItem() and parentItem(), not parent() which is for the QObject parent).

If you want to have a background property, I guess you don't want to have your background as the parent of your item, so you could instead set yourself as the parent of the background and set the background's z to -1 so it paints behind your item.

Alternatively, and that's what Qt Quick Controls 2 do, you could have a QQuickItem painting nothing and just be a parent for your background and your content item (QQC2 controls expose their contentItem as a property but you don't have to). For some inspiration you can check the source code of QQC2's Control here : https://code.woboq.org/qt5/qtquickcontrols2/src/quicktemplates2/qquickcontrol.cpp.html

Don't forget to handle the resizing of your background to the size of your item.