Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Add maximize button to QDialog frame



  • I have a dialog which inherits QDialog. According to the documentation, it is possible to customize the behavior of QDialog by setting some window flags which are parameters to the constructor of QDialog.

    I would like to add a maximize button but not a minimize button; the system window manager would presumably replace the maximize button with a "restore" button at runtime when the user maximizes the window.

    I am working with Linux Ubuntu 18.04 LTS, but the code should also eventually run on Windows 10 or later.

    In my class DlgGraphView which inherits QDialog, I have this in the initialization list:

    DlgGraphView::DlgGraphView(QWidget *parent)
      : QDialog     ( parent,
                        Qt::CustomizeWindowHint
                      | Qt::WindowSystemMenuHint
                      | Qt::WindowMaximizeButtonHint
                      | Qt::WindowCloseButtonHint)
      , (etc.)
    

    Apparently this has no effect.
    The constructor of QDialog looks like this:

    QDialog::QDialog(QWidget *parent, Qt::WindowFlags f)
        : QWidget(*new QDialogPrivate, parent,
                  f | ((f & Qt::WindowType_Mask) == 0 ? Qt::Dialog : Qt::WindowType(0)))
    {
    }
    

    So it looks like the flags should be honored, but they aren't. Most likely I have overlooked something?

    Thanks for any help!



  • @Robert-Hairgrove
    At a rough glance your code looks OK. When you say "Apparently this has no effect." what do you mean? Do you see no difference at all in dialog style, or just not what you wanted?

    Whether the native windowing system honours those flags is a different matter. It may be that you can't have a maximize without a minimize. Also the Linux behaviour is desktop/window manager dependent.



  • @JonB ... thanks for looking at this.

    My first attempt was only to set the first three enum values, i.e. without Qt::WindowCloseButtonHint because the default QDialog window frame has that built in. But then no buttons at all were shown, so I added it to the initialisation of the parent.

    I suppose I need to delve more into the Qt sources to understand what is happening.



  • @Robert-Hairgrove

    Check out this example (QWindowFlags Example).
    It is really good and you can play around with different settings until you get what you expect.
    You can switch between various types of windows (Window, Dialog...) and (if I have seen it correctly) even change or bypass X11 (may be different in MS Windows and Ubuntu).

    EDIT:
    In fact that there are two separate checkBlxes to toggle MinimizeButton and MaximizeButton, I guess it could be possible that you can use one of them, without the other.



  • @Pl45m4 ... thank you. I tried the example, but it appears that under Ubuntu 18.04 using the default window manager, all of the custom flags are ignored except for the close button hint if the window type "Dialog" is selected.

    I think I will implement my class differently and not inherit QDialog, but QWidget or QWindow instead.



  • Thanks to all ... I have decided to remain with QDialog because it has so many convenient signals and slots. In the constructor, I can resize the window using QScreen, e.g.:

      // Set the window to fill the available desktop area,
      // but leave a little space to allow for resizing the dialog:
      QScreen *screen = QGuiApplication::primaryScreen();
      if (screen) {
        QRect r = screen->availableGeometry();
        int l,t,w,h;
        l = t = 10;
        w = r.width() - 2*l;
        h = r.height() - 2*t;
        this->setGeometry(l,t,w,h);
      }
    

    Also, it was necessary to set the sizePolicy of the dialog to Ignored in order to allow the user to resize it without it jumping back to the default dimensions.

    So I am now marking this as "Solved".


Log in to reply