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.9k 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.
  • AmrCoderA Offline
    AmrCoderA Offline
    AmrCoder
    wrote on 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 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
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #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
        • AmrCoderA Offline
          AmrCoderA Offline
          AmrCoder
          wrote on 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
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #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 last edited by A Former User
              #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
                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 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
                • AmrCoderA Offline
                  AmrCoderA Offline
                  AmrCoder
                  wrote on 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
                  • AmrCoderA Offline
                    AmrCoderA Offline
                    AmrCoder
                    wrote on 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
                    0
                    • AmrCoderA AmrCoder

                      @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 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
                      • AmrCoderA Offline
                        AmrCoderA Offline
                        AmrCoder
                        wrote on last edited by
                        #13

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

                        1 Reply Last reply
                        0
                        • AmrCoderA Offline
                          AmrCoderA Offline
                          AmrCoder
                          wrote on last edited by AmrCoder
                          #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
                          0
                          • AmrCoderA AmrCoder

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

                            • Login

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