Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to let Maximum size of the window is fixed size
Forum Updated to NodeBB v4.3 + New Features

how to let Maximum size of the window is fixed size

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 15.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmrCoder
    wrote on 2 Aug 2017, 16:43 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sunfluxgames
      wrote on 2 Aug 2017, 18:15 last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AmrCoder
        wrote on 2 Aug 2017, 18:21 last edited by
        #3

        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 ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sunfluxgames
          wrote on 2 Aug 2017, 18:35 last edited by
          #4
          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.

          ? 1 Reply Last reply 2 Aug 2017, 22:58
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on 2 Aug 2017, 18:48 last edited by A Former User 8 Feb 2017, 18:51
            #5

            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).

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AmrCoder
              wrote on 2 Aug 2017, 19:01 last edited by
              #6

              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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 2 Aug 2017, 20:43 last edited by mrjj 8 Feb 2017, 20:46
                #7

                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.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on 2 Aug 2017, 22:30 last edited by A Former User 8 Feb 2017, 22:47
                  #8

                  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);
                  }
                  
                  1 Reply Last reply
                  2
                  • S Sunfluxgames
                    2 Aug 2017, 18:35
                    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.

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on 2 Aug 2017, 22:58 last edited by
                    #9

                    @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?

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      AmrCoder
                      wrote on 2 Aug 2017, 23:18 last edited by
                      #10

                      @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

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        AmrCoder
                        wrote on 2 Aug 2017, 23:22 last edited by
                        #11

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

                        ? 1 Reply Last reply 2 Aug 2017, 23:23
                        0
                        • A AmrCoder
                          2 Aug 2017, 23:22

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

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on 2 Aug 2017, 23:23 last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            AmrCoder
                            wrote on 2 Aug 2017, 23:26 last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              AmrCoder
                              wrote on 2 Aug 2017, 23:26 last edited by AmrCoder 8 Feb 2017, 23:28
                              #14

                              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

                              ? 1 Reply Last reply 2 Aug 2017, 23:30
                              0
                              • A AmrCoder
                                2 Aug 2017, 23:26

                                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

                                ? Offline
                                ? Offline
                                A Former User
                                wrote on 2 Aug 2017, 23:30 last edited by
                                #15

                                @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.

                                1 Reply Last reply
                                1

                                1/15

                                2 Aug 2017, 16:43

                                • Login

                                • Login or register to search.
                                1 out of 15
                                • First post
                                  1/15
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved