I do not know how to turn a QDialog
into a regular child widget. Looking at the Qt code for X11, I canβt understand how not to set the Qt::WindowFlags
passed to the QWidget
(parent) constructor so that it is a simple widget and not its own window (but I could be wrong, I did not spend much time on this) .
A simple alternative is to use a simple QWidget as your container instead of QDialog
. Here's a "PopupWidget" pattern that draws a translucent red background.
#include <QtGui> class PopupWidget: public QWidget { Q_OBJECT public: PopupWidget(QWidget *parent): QWidget(parent) { QVBoxLayout *vl = new QVBoxLayout; QPushButton *pb = new QPushButton("on top!", this); vl->addWidget(pb); connect(pb, SIGNAL(clicked()), this, SLOT(hide())); } public slots: void popup() { setGeometry(0, 0, parentWidget()->width(), parentWidget()->height()); raise(); show(); } protected: void paintEvent(QPaintEvent *) { QPainter p(this); QBrush b(QColor(255,0,0,128)); p.fillRect(0, 0, width(), height(), b); } };
To show it, call it the popup()
slot, which will raise it at the top of the widget stack, make it the size of the parent, and show it. This will mask all the widgets behind it (you cannot interact with them with the mouse). It hides when you click on this button.
Cautions:
- this does not prevent the user from using Tab to access widgets below. This could be fixed by switching the
enabled
property to your "normal" widget container, for example. (But do not disable the parent of the PopupWidget: this will disable the pop-up widget.) - this does not allow blocking a call like
QDialog::exec
- widgets in this pop-up window will not be transparent, you will have to create custom versions with a transparent background for all types of widgets in which you need AFAIK.
But this is probably less of a hassel than integrating a layout manager in your environment.
source share