I thought this was a bug, it is actually default behavior as described (completely buried more like) in the Qt documentation... "When the active subwindow is maximized, the default behavior is to maximize the next subwindow that is activated"
When there are multiple QMdiSubWindow
s in a QMdiArea
and some are flagged to "Stay on Top" they interact strangely with "Maximised" windows. That is, when a window is maximised and there are still other sub windows rendered over it, weird behavior occurs.
When any of the "Stay on Top" windows are clicked on or interacted with, they maximise and the maximised window is restored.
So far I can't seem to work out how the state of one window could be tracked by another, the source code is a mess and the private class model they are using is confusing as hell and makes interacting with the implementation basically impossible to do nicely. I also haven't found anything in the documentation that explains the behavior.
A minimum project that demonstrates this behavior is as follows:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->subwindow->setWindowTitle("SubWindow 1: Make Me Stay On Top");
ui->subwindow->setMinimumSize(QSize(500, 200));
ui->subwindow_2->setWindowTitle("SubWindow 2: Maximise Me Then Click On SubWindow 1");
ui->subwindow_2->setMinimumSize(QSize(500, 200));
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1091</width>
<height>687</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QMdiArea" name="mdiArea">
<widget class="QWidget" name="subwindow">
<property name="baseSize">
<size>
<width>200</width>
<height>200</height>
</size>
</property>
<property name="windowTitle">
<string>Subwindow</string>
</property>
</widget>
<widget class="QWidget" name="subwindow_2">
<property name="windowTitle">
<string>Subwindow</string>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
It turns out
mdiArea->setOption(QMdiArea::DontMaximizeSubWindowOnActivation)
after each call to maximise helps.