58

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Klasik
  • 872
  • 1
  • 9
  • 29

15 Answers15

59

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

  ...
}
Daniël Sonck
  • 869
  • 7
  • 9
  • @dsonck92: i tried this one and it works fine but for what I want to achieve I still have one leakage. Goal: MAXIMIZED + NON-RESIZABLE WINDOW. Result: Window shows maximized and with border grip and maximize button disabled, good so far. Leakage: I can still "pull" it away from the top of the screen and when I do so, it goes back to its default size (default as in QtCreator's fresh new app window size) which btw I can't maximize again, obviously, not even "throwing" it back to the top of the screen, please any insights.. thanx. using qtcreator 3.0.0 based on qt 5.2.0 – Scaramouche May 17 '17 at 14:17
  • this is the only one that I found that works for Windows 7 and 10 – dan_g Feb 11 '19 at 16:53
  • 1
    This worked for me in Python, thanks! self.setWindowFlags(QtCore.Qt.Dialog | QtCore.Qt.MSWindowsFixedSizeDialogHint | QtCore.Qt.WindowMinimizeButtonHint) – Adam Sirrelle Mar 15 '19 at 06:46
  • What about disabling only vertical and both-dimensions grips but leaving horizontal one enabled? – z33k Jun 01 '20 at 08:34
  • Doesn't work if the dock widget detached. – LRDPRDX Sep 16 '21 at 06:49
25

One-liner if you know exactly the required size of the window:

this->setFixedSize(QSize(750, 400));
Neurotransmitter
  • 6,289
  • 2
  • 51
  • 38
19

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.

thuga
  • 12,601
  • 42
  • 52
12

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

BuvinJ
  • 10,221
  • 5
  • 83
  • 96
12

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.

Aaron
  • 121
  • 1
  • 3
  • 2
    best solution if you are programatically adding widgets and don't know the size after the construct. – melMass Dec 07 '17 at 22:48
  • Just a note about a dock widget (Qt 5.9.7 tested). If you set this constraint on the window (`QMainWindow` in my case) containing a dock widget the behavior is as follows : if you detach the dock via double click on it, everything is fine, But if you drag the dock widget with your mouse the main window doesn't resize BUT the widgets inside do. So the "solution" I've found is to add the following features to my dock widgets : `setFeatures( QDockWidget::DockWidgetClosable | QDockWidgetFloatable )`. So you can't drag the dock with mouse but still can detach it. Don't know if it generates issues. – LRDPRDX Sep 16 '21 at 07:25
5

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.

Tushar
  • 462
  • 5
  • 12
5

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());
ramsudharsan
  • 165
  • 1
  • 3
3

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.

3

This helped me with Qt Creator 3.1.1:

this->setFixedSize(this->maximumSize());
Rachael
  • 424
  • 2
  • 7
3

You can in Qt5 use following code

this->setMinimumSize(sz);
this->setMaximumSize(sz);

Where sz is QSize object.

Boris Ivanov
  • 4,145
  • 1
  • 32
  • 40
3

Also you can just do something like:

this->setFixedWidth(int);
this->setFixedHeight(int);

The arrows are gone too.

GorudoYami
  • 33
  • 1
  • 1
  • 4
2

If someone looking for the same, but in Python:

    MainWindow.setWindowFlags(QtCore.Qt.MSWindowsFixedSizeDialogHint)
Cherry Red
  • 21
  • 3
  • 2
    This will overwrite *all* other flags in order to set this flag. If you wish to set this flag alone, `setWindowFlag(QtCore.Qt.MSWindowsFixedSizeDialogHint, True)` may be a better choice. – JonBrave Apr 26 '20 at 06:57
0

To prevent resizing the window add this line:

setFixedSize(width(), height());

in your QMainWindow constructor after the line: ui->setupUi(this);.

KeyC0de
  • 4,728
  • 8
  • 44
  • 68
0

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
Œlrim
  • 535
  • 2
  • 13
0

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);
    }
}
LoneSR
  • 11
  • 3