setFixedSize doesn't work as expected

2019-07-21 02:26发布

问题:

I'm using the latest stable version of Qt 5 built statically with GCC 4.8 on Windows 8.

I've created a simple QMainWindow-based application with just one button. When I do setFixedSize(minimumSizeHint()) the window gets resized as I want it to and the maximize button gets disabled, but I can still see the double-sided arrows, when I hover my pointer over the edges, and I can resize the window vertically by 14 pixels. If add another button or a status bar - nothing changes. If I add 14 pixels (or more) to the minimum height - it becomes truly fixed, but I can still see the arrows, which isn't normal.

This is very weird, because absolutely the same code (and its variations) works perfectly well in PyQt4 (please, note, I use C++ now). I tried:

  • layout()->setSizeConstraint(QLayout::SetFixedSize) - same behavior
  • setWindowFlags(Qt::MSWindowsFixedSizeDialogHint) - the window just disappears

Any way to fix this?

UPD: I was wrong about adding another button (apparently, it isn't the same as adding a status bar) - it isn't possible to resize the window anymore (though, why would Qt let me set the height of the window less than it likes...), but the arrows are still there.

UPD 2: Found a very similar question, but the answer didn't help (for the reasons described in a comment to the question itself).

回答1:

The sizegrip thing seems to work differently in Qt 5. Don't know if it's a bug or not. I tried setting the Qt::MSWindowsFixedSizeDialogHint flag for my main window and it seems to work fine for me. I'm on windows 7 though.

As it says in the documentation about the setWindowFlags function: Note: This function calls setParent() when changing the flags for a window, causing the widget to be hidden. You must call show() to make the widget visible again..



回答2:

If you want it to behave identically to what you had in PyQt4, you could just program in C++ using the same version of Qt that the PyQt project is using: Qt 4.8.4

http://qt-project.org/doc/qt-4.8/qwidget.html#setFixedSize

Hope that helps.