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.