how can I fully disable resizing a window including the resize icon when the mouse hovers the border?
C++QtUser InterfaceBorderC++ Problem Overview
I used:
setFixedSize(size());
to stop the window from resizing, but the resize arrows still appear when the mouse is over the border of the window.
Is there a better way to disable window resizing to avoid showing the arrows when crossing the border?
C++ Solutions
Solution 1 - C++
Qt has a windowFlag called Qt::MSWindowsFixedSizeDialogHint
for that. Depending on what you exactly want, you want to combine this flag with Qt::Widget
, Qt::Window
or Qt::Dialog
.
void MyDialog::MyDialog()
{
setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
...
}
Solution 2 - C++
One-liner if you know exactly the required size of the window:
this->setFixedSize(QSize(750, 400));
Solution 3 - C++
Try something like this:
this->statusBar()->setSizeGripEnabled(false);
If this doesn't work, all you need to do is detect what widget is activating QSizeGrip. You can do this by installing an event filter on your app and try to catch the QSizeGrip's mouseMoveEvent. Then debug its parent widget.
Here's an example of the eventFilter function you could use:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::MouseMove)
{
QSizeGrip *sg = qobject_cast<QSizeGrip*>(obj);
if(sg)
qDebug() << sg->parentWidget();
}
return false;
}
You could probably catch its show event as well, it's up to you.
Solution 4 - C++
If using Qt Designer, set your window's "sizePolicy" properties to "Fixed" in the vertical and horizontal directions and set minimum and maximum dimensions to equal values. Then, right-click on the window and select "Remove Status Bar" to get rid of the "size grip" in the bottom-right corner. Or, remove just the size grip via the suggestion from francis (rather than the entire status bar).
Solution 5 - C++
I found that calling setSizeConstraint(QLayout::SetFixedSize) on the layout worked the best for me. Specifically, from a QMainWindow constructor, I called:
this->layout()->setSizeConstraint(QLayout::SetFixedSize);
Here's a link to the docs: http://doc.qt.io/qt-4.8/qlayout.html#SizeConstraint-enum
(I'm using Qt 4.8.)
Since this was also a simple way to solve the OP's question, I thought I would share it for others to consider. It appears that there are many ways to accomplish this in Qt, but not all may be ideal for every situation. I tried several of the other options posted here, but they had various issues or constraints that I wasn't happy with in my own situation.
Solution 6 - C++
Use
setMinimumSize(QSize(width_px,height_px))
setMaximumSize(QSize(width_px,height_px))
where both methods have same size.You won't see the resize cursor & the window thus doesn't get resized/maximized.
Solution 7 - C++
If you wish to obtain the values of width and height from the UI form itself without manually specifying, then you can add the following command inside your project class:
this->setFixedSize(this->width(), this->height());
You can also set separate parameters for width and height (if required) with:
this->setFixedWidth(this->width());
this->setFixedHeight(this->height());
Solution 8 - C++
If you use the Qt Creator, you can try to specify the same Width
and Height
of the window in the properties
of geometry
, minimumSize
and maximumSize
.
Solution 9 - C++
This helped me with Qt Creator 3.1.1:
this->setFixedSize(this->maximumSize());
Solution 10 - C++
You can in Qt5 use following code
this->setMinimumSize(sz);
this->setMaximumSize(sz);
Where sz is QSize object.
Solution 11 - C++
Also you can just do something like:
this->setFixedWidth(int);
this->setFixedHeight(int);
The arrows are gone too.
Solution 12 - C++
If someone looking for the same, but in Python:
MainWindow.setWindowFlags(QtCore.Qt.MSWindowsFixedSizeDialogHint)
Solution 13 - C++
To prevent resizing the window add this line:
setFixedSize(width(), height());
in your QMainWindow constructor after the line: ui->setupUi(this);
.
Solution 14 - C++
The only solution that really worked for me on Windows 10 is using the WinAPI:
#ifdef Q_OS_WIN
#include <windows.h>
...
SetWindowLong((HWND) window->winId(), GWL_STYLE, GetWindowLong((HWND) window->winId(), GWL_STYLE)&~WS_SIZEBOX);
#endif
Solution 15 - C++
The size is not known until the appearance and can vary by system settings too (100%, 125%, 150%), so you may try something like this (it also hides the resize-cursor):
void QWidget::showEvent(QShowEvent *event)
{
// disable vertical resize
int height = this->height();
if (height != minimumHeight() || height != maximumHeight()) {
setMinimumHeight(height);
setMaximumHeight(height);
}
}