Disable QSizeGrip for QMainWindow [duplicate]

2020-03-31 09:01发布

问题:

I have been trying to disable the resize grip after setting the fixed width of the main window. I read in the Qt forms that setting the statusBar()->setSizeGripEnabled() as false would disable the resize grip, but no luck. I am running this on Qt5.

I know that removing the frame would remove this problem but that's a bigger problem (creating ways to drag the window, adding buttons to close etc.). So far this is what I have in my main method:

#if defined(Q_OS_WIN)
   QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QApplication a(argc, argv);
Compressor w;
w.statusBar()->setSizeGripEnabled( false );
w.setFixedSize(QSize(360,450));
w.setGeometry(
            QStyle::alignedRect(
                Qt::LeftToRight,
                Qt::AlignCenter,
                w.size(),
                a.desktop()->availableGeometry()
                )
            );
w.show();

The setGeometry sets the window on the centre of the screen on startup, Fixed it to a size. I am not sure what the problem is. Is there any way to fix this?

Update

Setting the w.setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed); Didn't work. Tried fixing both to Fixed, no luck.

Update 2

I even remove the status bar, but still no luck.

Update 3

Example:

On Windows 10 using version Qt 5.10.1

回答1:

Windows seems to be a bit adamant about being the cross-platform friendly OS. The way to fix the problem is by adding a window flag. The code:

goes into your main.cpp:

// Disable resize arrow.
#if defined(Q_OS_WIN)
    w.setWindowFlags(w.windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
#endif

Complete code:

#if defined(Q_OS_WIN)
   QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QApplication a(argc, argv);
Compressor w;

// Disable resize arrow.
#if defined(Q_OS_WIN)
    w.setWindowFlags(w.windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
#endif

w.setFixedSize(QSize(360,450));
w.setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
w.setGeometry(
            QStyle::alignedRect(
                Qt::LeftToRight,
                Qt::AlignCenter,
                w.size(),
                a.desktop()->availableGeometry()
                )
            );
w.show();

OR

You can also add it to your MainWindow.cpp file as:

this->setWindowFlags(this->windowFlags() | Qt::MSWindowsFixedSizeDialogHint);

See http://doc.qt.io/qt-5/qt.html#WindowType-enum for more.