Search code examples
qtmdi

How to get minimize/restore/close rectangle?


I want to draw some text in the end of menu bar like this
enter image description here

But when the child window is maximized in the MDI project it looks like this
enter image description here
I need to fix this output.
I want to check if the active child is maximized and if it is I want to get minimize/restore/close rectangle to get them total width.
How can I get active child window and how can I get it's rectangle of buttons?


Solution

  • // in yourMainWindow.cpp

    ...

    auto child = mdiArea->addSubWindow(yourWidget);
    connect(child, &QMdiSubWindow::windowStateChanged, this, &yourMainWindow::yourSlot);
    

    ...

    void yourMainWindow::yourSlot(Qt::WindowStates oldState, Qt::WindowStates newState)
    {
        if (newState.testFlag(Qt::WindowMaximized)) {
            auto child = qobject_cast<QMdiSubWindow *>(sender());
            if (!child)
                return;
    
            QStyleOptionComplex opt;
            opt.initFrom(child);
    
            auto size = child->style()->sizeFromContents(QStyle::CT_MdiControls, &opt, QSize(100, 20));
            qDebug() << size;
        }
    }