c++ - 调整 Qt 窗口大小时出现闪烁的白色区域

标签 c++ windows qt

我在调整 QWindow 大小时观察到一个奇怪的行为。当我调整窗口大小时使宽度和高度都增加或减少时,窗口不会以白色背景闪烁。但是当我增加宽度同时减小高度(或反之亦然)时,窗口会闪烁并暂时用白色填充新的区域。这是一个非常烦人的效果,尤其是当我在我的应用程序中使用深色主题并调整窗口大小时。

为了对此进行试验,我使用了 Qt SDK 中捆绑的“rasterwindow”示例。这不使用 QtWidgets 所以我使用这个例子来排除效果是由小部件引起的。我将 QThread::msleep(100); 添加到 RasterWindow::render(QPainter *) 函数来模拟非常慢的渲染。然后我尝试调整窗口大小。我只能在 Windows 上观察到这一点,但不能在 Linux 或 Mac 上观察到。请参阅随附的 GIF。

我的问题:

  1. 知道是什么导致了这个问题吗?为什么只在 Windows 上?
  2. 为什么同时增加宽度和高度时观察不到?为什么在减小一个维度并增加另一个维度时观察到它?
  3. 如何摆脱它?
  4. 如果我不能完全摆脱它,我可以用白色以外的其他颜色填充新区域吗(这样它在深色主题中就不会那么烦人)?

enter image description here

更新:我在这里创建了简约示例

/*
This is a minimalistic example to show Qt window flickering on Windows (not observed on Linux nor MacOS).

This is observable ONLY when the window is being resized by dragging lower right (or upper right or lower left) corner
so that one dimension (e.g. width) is increasing and the other dimension (is decreasing).
In that case the newly emerging areas are always filled in with white color which can be observed as terrible flickering.

Curiously the flickering does not happen:
- when you resize the window so that BOTH width and height are increasing
- when you resize the window by dragging the upper left corner

This is not a problem when you have light theme (then window background is probably white anyway).
But this is a big issue when you have dark theme, in which case it is superugly.

Notes:
1) there seems to be the same problem in QtWidgets and QtQuick applications,
   I have not found any Qt application which would behave correctly
2) This is not a problem of Windows OS. Non-Qt applications which I have tested
   seem to behave correctly, i.e. no flickering.
*/

#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <QThread>

class Widget : public QWidget
{
public:
    Widget()
    {
        /*
        The following code was used for experiments. But nothing worked, I could not get rid of flickering.
        The documentation says:
        autoFillBackground : bool
        This property holds whether the widget background is filled automatically
        If enabled, this property will cause Qt to fill the background of the widget before invoking the paint event.
        The color used is defined by the QPalette::Window color role from the widget's palette.
        */
        //setAttribute(Qt::WA_OpaquePaintEvent, true);
        //setAttribute(Qt::WA_NoSystemBackground, true);
        //setAutoFillBackground(false);
        //QPalette p = palette();
        //p.setColor(QPalette::Window, Qt::blue);
        //setPalette(p);
    }

    void paintEvent(QPaintEvent *) override
    {
        QPainter p(this);
        p.fillRect(0, 0, width(), height(), Qt::blue);
        QThread::msleep(50); // just to simulate slower painting
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

更新 2:我怀疑问题可能出在 QWindowsWindow::handleGeometryChange() 中,请参阅 https://code.woboq.org/qt5/qtbase/src/plugins/platforms/windows/qwindowswindow.cpp.html虽然我不知道如何修复它。我将此问题报告为错误 https://bugreports.qt.io/browse/QTBUG-89688

更新 3:从 Qt 6.1 开始,该错误似乎已修复。干得好。

最佳答案

此错误已在 Qt 6.1 中修复。据我在我的项目中测试,白色区域的闪烁消失了。

更新:这仅适用于 QtWidget 应用程序。对于 QtQuick/QML,从 Qt 6.3 开始仍然存在很多闪烁。参见例如https://bugreports.qt.io/browse/QTBUG-103201

关于c++ - 调整 Qt 窗口大小时出现闪烁的白色区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65442009/

相关文章:

c++ - 在 Qt 的 connect() 中传递不同的类

继承QWidget的Javascript对象

c++ - 异常抛出异常

windows - WinDbg:尝试附加到进程时 dbghelp.dll 的版本不匹配

windows - Delphi 中的这些 Windows API 签名有什么区别?

c++ - C++中的对象创建和成员声明

c++ - Memcpy uint32_t 到 char*

c++ - 二进制空间分区 - 空间分区逻辑错误

c++ - MPI 发送/接收 vector

windows - 文件夹上的 Powershell Remove-Item 仅在通过 Ansible 运行时失败