//头文件
#pragma
once #include <QWidget> #include "ui_widgetFullAD.h" class widgetFullAD : public QWidget { Q_OBJECT public: widgetFullAD(QWidget *parent = Q_NULLPTR); ~widgetFullAD(); protected: virtual bool eventFilter(QObject *watched, QEvent *event); void paintEvent(QPaintEvent *event); virtual void mousePressEvent(QMouseEvent *event); private: Ui::widgetFullAD ui; };
#include "widgetFullAD.h"
#include <qpainter.h>
widgetFullAD::widgetFullAD(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    setAttribute(Qt::WA_TranslucentBackground, true);
    setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);
    //ui.widget->installEventFilter(this);
    //installEventFilter(this);
}

widgetFullAD::~widgetFullAD()
{
    int xx = 343;
}

bool widgetFullAD::eventFilter(QObject * watched, QEvent * event)
{

    return QWidget::eventFilter(watched, event);
}

void widgetFullAD::paintEvent(QPaintEvent * event)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(0, 0, 0, 150));  //QColor最后一个参数80代表背景的透明度
}

void widgetFullAD::mousePressEvent(QMouseEvent * event)
{
    QRect rt = ui.widget->geometry();
    QPoint pt= this->mapFromGlobal(QCursor::pos());//将显示器坐标转换成窗口坐标
    if (ui.widget->geometry().contains(this->mapFromGlobal(QCursor::pos())))
    {

    }
    else
    {
        hide();
    }
}

今天遇到一种情况,设置Qt窗口半透明的时候,会把其他的控件全部边半透明了,还有一种情况是透明部分鼠标点击不了。所以找了一个最简单的处理,既然不影响鼠标点击事件,也就可以把背景变成半透明。

posted on 2018-12-13 23:55  chifandeyu  阅读(2581)  评论(0编辑  收藏  举报