Search code examples
qtqpainter

how to draw contents of widget in another widget in QT?


I want draw an existing widget A onto another widget B, (make A as the child of B may work, but here it's another story )

after search the internet , in qtcenter there is one thread said that:

 QPixmap w1Pix = QPixmap::grabWidget(w1); // can work

is there any other way to do that ?


Solution

  • One of (don't know others) the alternatives is to use QWidget's render methods, which let you specify a QPaintDevice or a QPainter to render into, where into that painter you want to render, what region of the widget is to be painted, and render flags. The flags let you specify whether you want children drawn or not, in particular.

    This is a bit more flexible than grabWidget, but do read the docs, the device or the target's painter must be in specific states when you call these functions.