how to let Maximum size of the window is fixed size



  • I want to make the Maximized window of my app be fixed size so no one can resize the window
    but it changes from one computer to another so I can't make it fixed size by a number I want it a general solution I use this code to show my window as maximum as possible when it starts in main.cpp

        MainWindow w;
        w.showMaximized();
    

    and try to go to make resize event so I ignore any resize from user to the window I use this code

    void MainWindow::resizeEvent(QResizeEvent *event)
    {
        event->ignore(); //i try to use this code on closeEvent and it work but not work on this event !!
    }
    

    so it should not accept any resize when the user tries to change the size of the window
    I tried this but it gives me not the maximum size

    this->setFixedSize(this->width(), this->height());
    

    my question is how can I make the maximum size of the app a fixed size and user can't change it
    Thanks in advance



  • @AmrCoder ui->setFixedSize(false); or remove the UI for it..



  • how can i remove the ui of it and I test the code you give to me it's wrong ? what is ui->setFixedSize(false)!!! or where i should use it ?



  • MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
                   // disables the maximize button in QT
                   ui->setFixedSize(false);
    }
    

    You can also grab the height and width later on once you start to build other widgets so you can resize them to your likeing.


  • Moderators

    Keep in mind that the screen size might change during runtime, e.g. if the user manually switches the screen resolution or if she drags the window to another desktop in a multi-monitor setup. Also keep in mind that your users might find fixed-sized windows super annoying (me included).



  • Yes I know but in this situation of this project it must be fixed and user know that and it should be fixed to avoid mistake in the project I all I want when the app starts it shows the windows maximized well because this code in main.cpp

        MainWindow w;
        w.showMaximized();
    

    how can I fixed that window and fixed that size or by another meaning how can I make user can't resize window anymore


  • Qt Champions 2016

    What about
    showFullScreen();
    takes up all space an no resize is possible.

    • I tried this but it gives me not the maximum size
      this->setFixedSize(this->width(), this->height());

    What did it give you then?
    You cannot do it right after showMaximized() as it has not happened yet.


  • Moderators

    Okay, I came up with the following solution. It's a bit complicated, granted, but it takes into account the geometry of the window decorations and size / position of taskbars, and it does not rely on dirty hacks. ;-) Only tested it on KDE / Linux / X so far.

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.showMaximized();
        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();
    
    protected:
        void changeEvent(QEvent *event);
    
    private slots:
        void setToFixedGeometry();
    
    private:
        Ui::MainWindow *ui;
        bool m_geometryDiscovery = true;
        QSize m_size;
        QPoint m_pos;
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QApplication>
    #include <QScreen>
    #include <QTimer>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::changeEvent(QEvent *event)
    {
        if (m_geometryDiscovery) {
            hide();
            auto r = qApp->primaryScreen()->availableGeometry();
            if (frameGeometry() == r) {
                m_geometryDiscovery = false;
                m_size = size();
                m_pos = pos();
                QTimer::singleShot(0, this, &MainWindow::setToFixedGeometry);
            }
        }
    
        QMainWindow::changeEvent(event);
    }
    
    void MainWindow::setToFixedGeometry()
    {
        showNormal();
        setFixedSize(m_size);
        move(m_pos);
    }
    

  • Moderators

    @Sunfluxgames said in how to let Maximum size of the window is fixed size:

    ui->setFixedSize(false);

    That doesn't compile for me. Which Qt version are you using?



  • @mrjj when I use this code

    this->setFixedSize(this->width(), this->height());
    

    this code gives me the window with the current size in properties written in UI even not give me the full maximize screen which should be done due to the code in the main.cpp which is

        MainWindow w;
        w.showMaximized();
    

    about the showFullScreen(); this was good but I can't alt + tab or go to any other open app it open so it open my app only I can't swap and do another thing will app is open



  • @Wieland
    I try the code above on windows but the window not even shown !!
    I am using QT 5.9 Mingw


  • Moderators

    @AmrCoder When have you copy and pasted my code? Because i changed it a bit a couple of minutes ago.



  • @Wieland In my MainWindow.h , Mainwindow.cpp and main.cpp
    as you do exactly



  • I solve it by using this code and it works with me

        QSize size = qApp->primaryScreen()->availableSize();
        this->setFixedSize(size);
    

    now the window show Maximized and I can't resize it


  • Moderators

    @AmrCoder said in how to let Maximum size of the window is fixed size:

    QSize size = qApp->primaryScreen()->availableSize();
    this->setFixedSize(size);
    

    If you do that, your window will be a bit too high, because setFixedSize() needs the target size of the window excluding the window decoration. So a small part of your window will be hidden behind the taskbar. At least on Linux.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.