Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QMainWindow Resize Issue



  • I have a quick question - I have a main window that needs to be resized to its minimum height after hiding some widgets in the window. I have tried

    mainScreen->resize(0,0);
    mainScreen->resize(mainScreen->width(),mainScreen->minimumSizeHint().height());
    

    I have also tried adding these before the resize (in the case of the timer the slot contains the resize code)

    QApplication::processEvents();
    QTimer::singleShot(0,this,SLOT(mainScreenResize()));
    

    None of these resize the window to the new minimum dimensions, instead acting as if the hidden widgets are still there. After hiding the widgets I can manually resize the window to its smaller size but I need this to be automatic. Any ideas? Also I have read the links below and applied their methods without success.

    link 1
    link 2

    Here's the code for hiding widgets and resizing from the backend and also setting up the main window. The resizing code is called from a backend object derived from QWidget (the backend has no widgets I guess that's just the way I set it up in the first place mistakenly) while the main window object (ReplicatorMainScreen) is derived from QMainWindow.

    *update - I tried removing the readWindowSettings() call again no success

    Replicator::Replicator(QWidget *parent) :
        QWidget(parent), wizard(0), mainScreen(0)
    {
        mainScreenSetup();
        mainScreen->setSimpleMode(true);
        //QApplication::processEvents();
        //QTimer::singleShot(0,this,SLOT(mainScreenResize()));
        mainScreen->resize(mainScreen->width(),mainScreen->minimumSizeHint().height());
    }
    
    void Replicator::mainScreenSetup(bool startMinimized, bool minToTray)
    {
        if (!mainScreen)
        {
            mainScreen = new ReplicatorMainScreen;
            mainScreen->show();
            readWindowSettings();
        }
    }
    
    void Replicator::readWindowSettings()
    {
        QSettings settings;
    
        settings.beginGroup("MainWindow");
        if (settings.contains("size"))
            mainScreen->resize(settings.value("size", QSize(400, 400)).toSize());
        if (settings.contains("pos"))
            mainScreen->move(settings.value("pos", QPoint(200, 200)).toPoint());
        settings.endGroup();
    }
    
    void Replicator::mainScreenResize()
    {
        //mainScreen->resize(0,0);
        mainScreen->resize(mainScreen->width(),mainScreen->minimumSizeHint().height());
    }
    
    void ReplicatorMainScreen::setSimpleMode(bool enabled)
    {
        ui->newJobButton->setVisible(!enabled);
    ...
    }
    

    Thanks for the help!


  • Lifetime Qt Champion

    Hi,

    You might get some ideas here.

    Hope it helps



  • Ha I found a cheap hack ...

    QTimer::singleShot(100,this,SLOT(mainScreenResize()));
    

    good enough for me you can see the resize take place but it's no deal breaker


Log in to reply