QT 事件仅对部分小部件透明
QT Event transparency only for part of the widget
软件显示 3 个小部件:
- 主窗口
- 内容小部件,覆盖大部分主窗口 自定义小部件
- ,涵盖主窗口和内容小部件的一部分。
自定义小部件有一个需要Event-opaque
的部分(定义为QRect
),而周围的区域必须Event-transparent
。
我尝试过:
setAttribute(Qt::WA_TransparentForMouseEvents);
但是自定义的所有子小部件也变得透明。
我也尝试了setMask
,但随后自定义小部件无法在周围区域绘制。
如何实现这种部分事件透明性?
示例(它没有解释完整的问题,只是添加一个测试解决方案的基础):
主.cpp
#include "transparentwidget.hpp"
#include "normalwidget.hpp"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Main Window
NormalWidget window;
window.resize(500,500);
window.setObjectName("window");
window.setStyleSheet("background-color: rgba(0,0,128,128); ");
// Content window
NormalWidget content(&window);
content.setObjectName("content");
content.resize(400, 400);
content.move(0,0);
content.setStyleSheet("background-color: rgba(128,0,0,128);");
TransparentWidget custom(&window);
custom.setObjectName("custom");
custom.resize(500, 200);
custom.setStyleSheet("background-color:rgba(0,128,0,128);");
window.show();
return a.exec();
}
transparentwidget.hpp
#ifndef TRANSPARENTWIDGET_H
#define TRANSPARENTWIDGET_H
#include <QWidget>
#include <QStyleOption>
#include <QPainter>
#include <QDebug>
#include <QEvent>
// This widget shall be transparent in some parts
class TransparentWidget : public QWidget
{
Q_OBJECT
public:
explicit TransparentWidget(QWidget *parent = 0): QWidget(parent)
{
// Start of solution with WA_TransparentForMouseEvents (not working)
setAttribute(Qt::WA_TransparentForMouseEvents);
// end solution with WA_TransparentForMouseEvents
}
~TransparentWidget(){}
protected:
QRect opaqueRect = QRect(0,0,400,100);
void paintEvent(QPaintEvent *)
{
// Solution with setMask, not working
QRegion reg(opaqueRect);
setMask(reg);
// end of setMask solution
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
bool event(QEvent *event)
{
// Starting of solution with event propagation (not working)
if (event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease)
{
QMouseEvent* e = static_cast<QMouseEvent*>(event);
if (e && !opaqueRect.contains(e->pos()) return false;
}
// end solution with event propagation.
if (event->type() == QEvent::MouseButtonPress) qDebug() << "Press: " << objectName();
else if(event->type() == QEvent::MouseButtonRelease) qDebug() << "Release: " << objectName();
return QWidget::event(event);
}
};
#endif
normalwidget.hpp
#ifndef NORMALWIDGET_H
#define NORMALWIDGET_H
#include <QWidget>
#include <QStyleOption>
#include <QPainter>
#include <QDebug>
#include <QEvent>
// Widgets that are not event-transparent
class NormalWidget : public QWidget
{
Q_OBJECT
public:
explicit NormalWidget(QWidget *parent = 0): QWidget(parent){}
~NormalWidget(){}
protected:
void paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
bool event(QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress) qDebug() << "Press: " << objectName();
else if(event->type() == QEvent::MouseButtonRelease) qDebug() << "Release: " << objectName();
}
};
#endif // NORMALWIDGET_H
就像文档说的:
启用后,此属性将禁用将鼠标事件传递到 小组件及其子项。
解决方案是忽略TransparentWidget::event()
中的所有鼠标事件。如果在 TransparentWidget
的子项上执行鼠标事件,则该事件将由该子项使用,否则将传递给TransparentWidget
的父项:
bool TransparentWidget::event(QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease ||
event->type() == QEvent::MouseButtonRelease)
return false;
else
return QWidget::event(event);
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 最小硬币更换问题(自上而下方法)
- Qt VTK交互风格的信号到小部件
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 使用递归的数组的最小值.这是怎么回事
- 为什么在浮点中从大到小会引入更多的误差
- 如何在全屏模式下(在OpenGL中)使背景透明
- 找到两对数字,使它们的乘积的绝对差最小化
- 当使用透明的std函数对象时,我们还需要写空的尖括号吗
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 最大和最小 1 C++函数
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 如何将部分流作为参数传递
- QT 事件仅对部分小部件透明
- QT中的透明小部件
- C++ 创建小部件数组
- 在创建子小组件后设置父小组件时,子小组件对事件是透明的
- 透明比较器代码最小化