Qt 自带标题栏处理方法思路

目录

需求

相关方法也有两种

1.只需要继承QAbstractNativeEventFilter类,从写放nativeEventFilter,并调用安装函数installNativeEventFilter

2.第二种是Qt提供的继承QWidget 重写bool nativeEvent(const QByteArray &eventType, void *message, long *result),但是这个在5.11.1 版本还想有个bug,详情QT 5.11.1 nativeEvent 实现有 bug, 好在 5.11.2 已经修正了 - SHUHARI 的博客

关于Qt4处理方法

 

题外话关于自定义窗口切换出现闪烁效果解决办法也是用的相关函数:


需求

当我们想要处理原始标题栏,可能会想到Qt自带mousePressEvent()方法来处理一些鼠标点击,或者mouseEvent()方法处理鼠标移动。但是当我们点击软件自带的标题栏,却发现上面方法都没有调用,导致原因其实还是因为Qt只是对Windows上面做了一层封装,如果我们想要处理这个问题就要使用Qt与win之间的方法,

相关方法也有两种

1.只需要继承QAbstractNativeEventFilter类,从写放nativeEventFilter,并调用安装函数installNativeEventFilter

virtual bool QAbstractNativeEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result);
void QCoreApplication::installNativeEventFilter(QAbstractNativeEventFilter *filterObj);
void QAbstractEventDispatcher::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)

官方给的例子Linux:

class MyXcbEventFilter : public QAbstractNativeEventFilter
 {
 public:
     bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override
     {
         if (eventType == "xcb_generic_event_t") {
             xcb_generic_event_t* ev = static_cast<xcb_generic_event_t *>(message);
             // ...
         }
         return false;
     }
 };

需要注意参数eventType,在不同的系统上,也是不一样的,Qt文档也有说明

X11 为 "xcb_generic_event_t"

macOS 为 "mac_generic_NSEvent"

windows  为 "windows_generic_MSG" 和 "windows_dispatcher_MSG"

在Windows程序中,消息是由MSG结构体来表示的。MSG结构体的定义

typedef struct tagMSG { // msg

HWND hwnd; //窗口句柄

UINT message; //消息常量标识符

WPARAM wParam; //32位消息的特定附加信息,具体表示什么处决于message

LPARAM lParam; //32位消息的特定附加信息,具体表示什么处决于message

DWORD time; //消息创建时的时间

POINT pt; //消息创建时的鼠标位置

} MSG;

关于msg大全你们可以查看这个msg博文 

2.第二种是Qt提供的继承QWidget 重写bool nativeEvent(const QByteArray &eventType, void *message, long *result),但是这个在5.11.1 版本还想有个bug,详情QT 5.11.1 nativeEvent 实现有 bug, 好在 5.11.2 已经修正了 - SHUHARI 的博客

 问题是传参时候使用引用

bool xxxxApp::nativeEvent(const QByteArray& eventType, void* message, long* result)
{
	MSG* msg = (MSG*)(message);
	if (/*msg->message == WM_NCLBUTTONDOWN ||*/ msg->message == WM_NCLBUTTONDOWN)
	{
	}
// 停止由Qt处理的事件,返回true并设置result。result参数仅在Windows上有意义。如果返回false,
// 则将此本机事件传递回Qt, Qt将事件转换为Qt事件并将其发送给小部件
	*result = 0;
	return false;
}

关于Qt4处理方法

// 方法1
// 通过继承QWidget的类中重新实现winEvent接口,以接收在消息参数中传递的本机Windows事件
bool QWidget::winEvent(MSG *message, long *result);

// 方法2
// 通过继承QCoreApplication的类中重新实现winEventFilter接口,
// 以接收在消息参数中传递的本机Windows事件
bool QCoreApplication::winEventFilter(MSG *msg, long *result)

题外话关于自定义窗口切换出现闪烁效果解决办法也是用的相关函数:

https://blog.csdn.net/u012456851/article/details/89227521https://blog.csdn.net/u012456851/article/details/89227521

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Qt 中,可以通过自定义 QWidget 的 paintEvent() 函数来实现自定义标题栏。具体步骤如下: 1.创建一个新的 QWidget,用于作为自定义标题栏。 2.将 QWidget 的窗口标志设置为 Qt::WindowFlags(Qt::FramelessWindowHint),这样可以去掉默认的标题栏。 3.重写 QWidget 的 paintEvent() 函数,在该函数中绘制自定义标题栏。 4.将 QWidget 设置为 QMainWindow 的标题栏,即调用 QMainWindow 的 setWindowTitle() 函数,并将其参数设置为自定义标题栏的指针。 下面是一个简单的示例代码: ``` #include <QtWidgets> // 继承 QWidget,作为自定义标题栏 class CustomTitleBar : public QWidget { public: CustomTitleBar(QWidget *parent = nullptr) : QWidget(parent) { // 设置窗口标志 setWindowFlags(Qt::FramelessWindowHint); } protected: void paintEvent(QPaintEvent *) override { QPainter painter(this); painter.fillRect(rect(), Qt::blue); painter.drawText(rect(), Qt::AlignCenter, "Custom Title Bar"); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow mainWindow; // 设置主窗口标题 mainWindow.setWindowTitle("Main Window"); // 创建自定义标题栏 CustomTitleBar *titleBar = new CustomTitleBar(&mainWindow); // 设置自定义标题栏为主窗口的标题栏 mainWindow.setMenuWidget(titleBar); mainWindow.show(); return app.exec(); } ``` 运行该程序,可以看到自定义的蓝色标题栏。你可以根据自己的需要修改 paintEvent() 函数,来实现各种不同样式的标题栏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路奇怪

有钱出钱,没钱多出编程主意啊

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值