1

I'm trying to use Qt to make an option dialog that stays on top of the program's main window (which is a QMainWindow). QDialog seemed like a perfect fit, but clicking the main window would bring it back to the front. However, I have managed to make two tiny examples: one which works, and another which doesn't (which is derived from my actual application). I don't know what makes the behavior different between these two examples.


The option window in the following example stays on top of the main window:

test.cpp

#include <QApplication>
#include <QMainWindow>
#include <QDialog>

int main(int argc, char *argv[])
{
    QApplication test(argc, argv);
    QMainWindow *mainWindow = new QMainWindow;
    mainWindow->show();

    QDialog * optionsWindow = new QDialog(mainWindow);
    optionsWindow->show();

    return test.exec();
}

test.pro

TEMPLATE = app
TARGET = test
INCLUDEPATH += .
QT = core gui widgets 

SOURCES += test.cpp

The option window in the following example does not stay on top of the main window:

TestApp.cpp

#include <QApplication>
#include "MainWindow.hpp"

int main(int argc, char *argv[])
{
        QApplication testAppGUI(argc, argv);

        MainWindow *mainWindow = new MainWindow();
        mainWindow->show();

        return testAppGUI.exec();
}

MainWindow.hpp

#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

#include <QMainWindow>
#include "OptionWindow.hpp"

class MainWindow : public QMainWindow
{
    Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0)
        {
            optionWindow = new OptionWindow(this);
            optionWindow->show();
        }
        OptionWindow *optionWindow;
};

#endif

OptionsWindow.hpp

#ifndef OPTIONWINDOW_HPP
#define OPTIONWINDOW_HPP

#include <QDialog>

class OptionWindow : public QDialog 
{
    Q_OBJECT
    public:
        explicit OptionWindow(QWidget *parent = 0){}
};

#endif

TestApp.pro

TEMPLATE = app
TARGET = TestApp
INCLUDEPATH += .

QT = core gui widgets 

HEADERS +=  MainWindow.hpp \
            OptionWindow.hpp

SOURCES +=  TestApp.cpp

This SO answer suggests giving the QDialog a parent, which I do. It also mentions setting the Qt::Tool flag, but it could not solve my problem (and the working example above doesn't use it).

This other answer suggests using a QDockWidget, but it doesn't meet my wanted visual style or expected behavior.

Speaking of which, here's what I expect of my options dialog:

  • The options window should always be on top of the main window, but not of the other applications;
  • Only the main window shows in the taskbar;
  • Minimizing the main window also minimizes the options window, and restoring it also restores the options window (on top of the main window);
  • The main window should still be enabled when the options window is open.

I am using QMake 3.1, Qt 5.8.0, g++ 5.4.0 and Xfce 4.12 on Linux Lite 3.4.

Community
  • 1
  • 1
juef
  • 90
  • 1
  • 7

1 Answers1

2

A couple of points...

Firstly, you're not using the parent parameter passed to the OptionWindow ctor: you need...

class OptionWindow: public QDialog {
  Q_OBJECT;
public:
  explicit OptionWindow (QWidget *parent = nullptr)
    : QDialog(parent) /* <-- Added */
  {}
};

That issue I understand but... secondly, it appears you need to make sure you call show on the parent before calling show against the QDialog.

class MainWindow: public QMainWindow {
  Q_OBJECT;
public:
  explicit MainWindow (QWidget *parent = nullptr)
    {
      optionWindow = new OptionWindow(this);
      show(); /* <-- Added */
      optionWindow->show();
    }
  OptionWindow *optionWindow;
};

That one I can't really explain -- sorry (although it could be a function of my window manager: fvwm -- I'm on Suse linux using Qt5.8). Perhaps someone else can chip in and I'll update.

G.M.
  • 12,232
  • 2
  • 15
  • 18
  • First point: ah, yes, this is a mistake I made when trying to make the example as tiny as possible, sorry about that. Second point: **it works!** I'm still very interested in reading an explanation for this, but in the meantime, thank you very much. I have gladly accepted this answer. – juef May 06 '17 at 20:10
  • Looks like your second point is related to the window manager or Qt version. [this example](https://pastebin.com/Jfs9352m) satisfies all the OP's conditions when run on Qt5.6.1/MSVC2013/Windows7. I will try on more platforms when I can. – Mike May 07 '17 at 03:51
  • You must be right, because this example does not satisfy the conditions on my system. I edited my original post to add that I am using Xfce 4.12 on Linux Lite 3.4. Thank you for your comment. – juef May 07 '17 at 20:38