从左侧QT调整窗口大小时闪烁



我知道关于这个主题有一些类似的问题,但我找不到一个与使用Qt创建的窗口相关的问题。

我尝试了这个答案中的所有建议:如何在调整窗口大小时平滑丑陋的抖动/闪烁/跳跃,尤其是拖动左/上边界(Win7-10;bg、bitblt和DWM(?

  • WM_WINDOWPOSCHANGING标志修改为SWP_NOCOPYBITS
wp = reinterpret_cast<tagWINDOWPOS*>(msg->lParam);
wp->flags |= SWP_NOCOPYBITS;
  • WM_NCCALCSIZE中返回WVR_VALIDRECTS
if (msg->wParam == 1)
return WVR_VALIDRECTS;
  • CS_HREDRAW | CS_VREDRAW样式应用于窗口
SetWindowLongPtr(HWND(winId()), GWL_STYLE, CS_HREDRAW | CS_VREDRAW | WS_POPUPWINDOW | WS_CAPTION | WS_SIZEBOX | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_CLIPCHILDREN);
  • 应用exstyleWS_EX_COMPOSITED
  • CCD_ 8返回1

但是闪烁仍然存在。我还能尝试什么?

目标是创建一个没有标题的窗口,可以调整大小/最小化。

下面的代码正在工作,问题是当从左/上调整窗口大小时,会导致闪烁。

//.h
#include <QtWidgets/QMainWindow>
#include <Windows.h>
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void Frameless()
{
// set flags that will override what Qt does, especially with the Qt::FramelessWindowHint which essentially disables WS_SIZEBOX/WS_THICKFRAME
SetWindowLongPtr(HWND(winId()), GWL_STYLE, WS_POPUPWINDOW | WS_CAPTION | WS_SIZEBOX | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_CLIPCHILDREN);
}
private:
Ui::MainWindowClass ui;
protected:
virtual bool nativeEvent(const QByteArray& eventType, void* message, qintptr* result) override;
};
//.cpp
#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
Frameless();
return;
}
MainWindow::~MainWindow()
{}
bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, qintptr* result)
{
MSG* msg = static_cast<MSG*>(message);
switch (msg->message)
{
case WM_WINDOWPOSCHANGING:
{
tagWINDOWPOS* wp;
wp = reinterpret_cast<tagWINDOWPOS*>(msg->lParam);
wp->flags |= SWP_NOCOPYBITS;
}
break;
case WM_NCCALCSIZE:
{
if (msg->wParam == 1)
return WVR_VALIDRECTS;
// Just return 0 and mark event as handled. This will draw the widget contents
// into the full size of the frame, instead of leaving room for it.
*result = 0;

return true;
}
break;
case WM_NCHITTEST:
{
if (isMaximized())
{
return false;
}
*result = 0;
const LONG borderWidth = 8;
RECT winrect;
GetWindowRect(reinterpret_cast<HWND>(winId()), &winrect);
// must be short to correctly work with multiple monitors (negative coordinates)
short x = msg->lParam & 0x0000FFFF;
short y = (msg->lParam & 0xFFFF0000) >> 16;
bool resizeWidth = minimumWidth() != maximumWidth();
bool resizeHeight = minimumHeight() != maximumHeight();
if (resizeWidth)
{
//left border
if (x >= winrect.left && x < winrect.left + borderWidth)
{
*result = HTLEFT;
}
//right border
if (x < winrect.right && x >= winrect.right - borderWidth)
{
*result = HTRIGHT;
}
}
if (resizeHeight)
{
//bottom border
if (y < winrect.bottom && y >= winrect.bottom - borderWidth)
{
*result = HTBOTTOM;
}
//top border
if (y >= winrect.top && y < winrect.top + borderWidth)
{
*result = HTTOP;
}
}
if (resizeWidth && resizeHeight)
{
//bottom left corner
if (x >= winrect.left && x < winrect.left + borderWidth &&
y < winrect.bottom && y >= winrect.bottom - borderWidth)
{
*result = HTBOTTOMLEFT;
}
//bottom right corner
if (x < winrect.right && x >= winrect.right - borderWidth &&
y < winrect.bottom && y >= winrect.bottom - borderWidth)
{
*result = HTBOTTOMRIGHT;
}
//top left corner
if (x >= winrect.left && x < winrect.left + borderWidth &&
y >= winrect.top && y < winrect.top + borderWidth)
{
*result = HTTOPLEFT;
}
//top right corner
if (x < winrect.right && x >= winrect.right - borderWidth &&
y >= winrect.top && y < winrect.top + borderWidth)
{
*result = HTTOPRIGHT;
}
}
if (*result != 0)
return true;
QWidget *action = QApplication::widgetAt(QCursor::pos());
if (action == this){
*result = HTCAPTION;
return true;
}
return false;
}
break;
default:
return QWidget::nativeEvent(eventType, message, result);
}

return QWidget::nativeEvent(eventType, message, result);
}

编辑

我正在测试将项目编译为debug的GUI,当我更改为release时,闪烁减少了很多,但仍然存在,现在它从右侧而不是左侧闪烁。

QT 6.3.1staticdebug:测试

https://www.youtube.com/watch?v=a5fmXKsKDaY&feature=youtu.be

QT 6.3.1staticrelease:测试

https://www.youtube.com/watch?v=OwpxmCsLLRQ

计算机设置,操作系统版本:https://i.stack.imgur.com/3DXmd.png

我使用win10在两台不同的机器上进行了测试,结果都出现了闪烁。

jdfa答案中的frameless.h也出现了同样的问题,不同之处在于我的GUI调整方法更"平滑"/更快。

我建议避免Qt和WinAPI的这种混合。它不仅不可移植,而且还会产生您的问题。

该问题可以通过使用Qt::FramelessWindowHint以Qt方式解决原始任务(无框架可调整大小窗口(来避免。不幸的是,在这种情况下,调整大小的功能将与框架一起丢失。因此,您需要通过重写鼠标事件处理程序(mousePressEventmouseMoveEvent(来重新实现它。您基本上已经在做类似的事情,只是使用WinAPI。

这个问题已经在这里得到了解决:https://gist.github.com/Nico-Duduf/b8c799f1f2a694732abd1238843b1d70

以下是我的结果:https://i.imgur.com/ovbG2hI.mp4

编辑:以下是我的测试方法:

#include <QApplication>
#include <QtWidgets/QMainWindow>
#include "FrameLess.h"
class MainWindow : public QMainWindow {
public:
MainWindow(QWidget* parent = nullptr)
: QMainWindow(parent)
{
setStyleSheet("border-image:url("bg.png");");
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow main;
FrameLess f(&main);
main.show();
return app.exec();
}

我在6.4中创建了一个项目,该项目没有您在视频中描述和显示的抖动效果。我在windows 10上测试过。

请创建一个文件夹,我们称之为TestImage,并保存以下文件:

main.cpp

#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
Qt::WindowFlags flags;
flags = Qt::Window;
//flags |= Qt::FramelessWindowHint;
flags |= Qt::CustomizeWindowHint;
this->setWindowFlags(flags);
}
MainWindow::~MainWindow()
{
delete ui;
}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

TestImage.pro

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
SOURCES += 
main.cpp 
mainwindow.cpp
HEADERS += 
mainwindow.h
FORMS += 
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += 
resources.qrc

earth.jpeg(将图像保存在同一文件夹中(

resources.qrc

<RCC>
<qresource prefix="/image">
<file>earth.jpeg</file>
</qresource>
</RCC>

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>2068</width>
<height>1599</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<property name="sizeConstraint">
<enum>QLayout::SetNoConstraint</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Ignored">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="resources.qrc">:/image/earth.jpeg</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>2068</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources>
<include location="resources.qrc"/>
</resources>
<connections/>
</ui>

您可能只需要如下修改处理WM_NCCALCSIZE消息。也许它看起来很奇怪,但请尝试一下。似乎当您从WM_NCHITTEST返回HTLEFT、HTTOP等时,系统希望NCCALLSIZE_PARAMS.rgrc中的第一个rect包含客户端区域,就像有边界一样。我已经使用过这个解决方案好几次了,到目前为止,它适用于我尝试过的所有Windows版本。

case WM_NCCALCSIZE:
{
if (msg->wParam == 1)
{
NCCALCSIZE_PARAMS& pms = *(NCCALCSIZE_PARAMS*)msg->lParam;
RECT& r = pms.rgrc[0];
int borderWidth = GetSystemMetrics(SM_CXSIZEFRAME);
int borderHeight = GetSystemMetrics(SM_CYSIZEFRAME);
r.left += borderWidth;
r.top += borderHeight;
r.right -= borderWidth;
r.bottom -= borderHeight;
*result = WVR_VALIDRECTS;
return true;
}
// Just return 0 and mark event as handled. This will draw the widget contents
// into the full size of the frame, instead of leaving room for it.
*result = 0;
return true;
}
break;

最新更新