how can I fully disable resizing a window including the resize icon when the mouse hovers the border?

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?

Answers


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

  ...
}

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.


One-liner if you know exactly what size the window should be:

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

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


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.


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.


This helped me with Qt Creator 3.1.1:

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

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.


You can in Qt5 use following code

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

Where sz is QSize object.


Also you can just do something like:

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

The arrows are gone too.


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

To prevent resizing the window add this line:

setFixedSize(width(), height());

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


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

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

If someone looking for the same, but in Python:

    MainWindow.setWindowFlags(QtCore.Qt.MSWindowsFixedSizeDialogHint)

I just found the answer. In Qt Designer, just set the sizeGripEnabled to false under statusbar of your window. Easy


Need Your Help

dyld: Library not loaded: @rpath/libswiftAVFoundation.dylib

macos avfoundation xcode7 dyld

I have just updated to Xcode 7 and swift 2 and finished fixing the errors that come with the transition. I finally got the project to build, but after the launch screen I get an error saying:

Get Column names from a query without data

c# sql sql-server sql-server-2008 tsql

I have a view vwGetData which gets data from two tables t1,t2 and has fields: