1

Edit:

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"


Question:

When there are multiple QMdiSubWindows 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>
Troyseph
  • 4,960
  • 3
  • 38
  • 61
  • Did you try to remove the flag Qt::WindowMaximizeButtonHint from the stay-on-top widgets? – falkb Apr 17 '15 at 11:47
  • That does nothing to help, all it does is hide the maximise button, it doesn't prevent the screwy behaviour. – Troyseph Apr 17 '15 at 13:10
  • I noticed mainwindow example app does not provide maximize either. Well, another workaround could be to change the stay-on-top widgets to docked ones, like the tool widgets in QtDesigner on the right-hand side. – falkb Apr 17 '15 at 13:20
  • While the minimal implementation demonstrates the bug, the issue I'm having is with more complex code where I cannot switch out one widget for another. – Troyseph Apr 17 '15 at 13:26
  • 1
    something like mdiArea->setOption(QMdiArea::DontMaximizeSubWindowOnActivation) to disallow maximizing at all? – falkb Apr 17 '15 at 13:27
  • You are a gosh darn genius! I'm not sure why/how that was turning itself on at all, but disabling it after each call to maximise actually works! – Troyseph Apr 17 '15 at 13:46
  • OK, thanks. Then I turn this into an answer. Have fun! – falkb Apr 17 '15 at 13:58

1 Answers1

3

It turns out

mdiArea->setOption(QMdiArea::DontMaximizeSubWindowOnActivation)

after each call to maximise helps.

falkb
  • 1,294
  • 11
  • 35
  • 1
    Just done some more experimenting, only needs to be done once, upon initialisation of the mdiArea, thanks again for reading the documentation better than me! – Troyseph Apr 17 '15 at 14:33