Install QDialog as an Alien

In a standalone graphical application, where I do not have a window manager or composite manager, I want to show QDialog the user a request for values.

The dialog is quite large, so I want to make it translucent so that the user can view it to see what is happening in the application while the dialog is displayed.

The problem is that for the transparency of X-native windows there must be a composite manager. Qt's internal widgets can be colored translucent because they do not correspond to native X-windows (aliens) and only Qt is fully known.

Is there a way to make the QDialog background translucent without using a compound manager? Perhaps this is a normal child widget / alien of the main application window? Is there a better alternative to this?

+4
source share
1 answer

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.

+2
source

All Articles