Solved how to let Maximum size of the window is fixed size
-
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
-
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. - I tried this but it gives me not the maximum size
-
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); }
-
@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 -
@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
-
@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.