51
votes

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?

15
What OS are you using? Have you tried using dialog instead of window?Amartel
Windows 8. developing for desktop. I'm just created new project. do u think dialog will be isue?Klasik
Well, in my WinXp the only place, where arrows appeared (after setting fixed size) is bottom right corner (the one with a triangle). In dialog (after setting fixed size) - no arrows appeared.Amartel
I will try use dialog.Klasik
setFixedSize(size()) works for me. Perhaps it was an old Qt bug or something else is overriding your call after you make it.AsksAnyway

15 Answers

52
votes

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

  ...
}
20
votes

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

this->setFixedSize(QSize(750, 400));
19
votes

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.

10
votes

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

10
votes

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.

5
votes

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.

5
votes

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());
3
votes

This helped me with Qt Creator 3.1.1:

this->setFixedSize(this->maximumSize());
3
votes

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
votes

You can in Qt5 use following code

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

Where sz is QSize object.

3
votes

Also you can just do something like:

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

The arrows are gone too.

1
votes

If someone looking for the same, but in Python:

    MainWindow.setWindowFlags(QtCore.Qt.MSWindowsFixedSizeDialogHint)
0
votes

To prevent resizing the window add this line:

setFixedSize(width(), height());

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

0
votes

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

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