用Qt显示半透明/不规则形状的窗户
发布时间:2021-01-19 03:34:09  所属栏目:Windows  来源:网络整理 
            导读:可以用Qt显示半透明和/或不规则形状的窗户吗? (我假设它最终取决于底层GUI系统的功能,但让我们假设至少Windows XP / Mac OS X) 如果是这样,怎么做到这一点? 是的,这是可能的.关键是QWidget的Qt :: WA_TranslucentBackground属性 这是一个简单的类,绘制一
                
                
                
            | 
                         可以用Qt显示半透明和/或不规则形状的窗户吗? (我假设它最终取决于底层GUI系统的功能,但让我们假设至少Windows XP / Mac OS X) 如果是这样,怎么做到这一点? 是的,这是可能的.关键是QWidget的Qt :: WA_TranslucentBackground属性这是一个简单的类,绘制一个圆形半透明窗口,红色背景50%alpha. TranslucentRoundWindow.h: #include <QWidget>
class TranslucentRoundWindow : public QWidget
{
    public:
        TranslucentRoundWindow(QWidget *parent = 0);
        virtual QSize sizeHint() const;
    protected:
        virtual void paintEvent(QPaintEvent *paintEvent);
}; 
 TranslucentRoundWindow.cpp: #include <QtGui>
#include "TranslucentRoundWindow.h"
TranslucentRoundWindow::TranslucentRoundWindow(QWidget *parent) : QWidget(parent,Qt::FramelessWindowHint)
{
    setAttribute(Qt::WA_TranslucentBackground);
}
QSize TranslucentRoundWindow::sizeHint() const
{
    return QSize(300,300);
}
void TranslucentRoundWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(255,127));
    painter.drawEllipse(0,width(),height());
} 
 如果您想要使用鼠标移动此窗口,则必须覆盖mousePressEvent,mouseMoveEvent和mouseReleaseEvent. (编辑:滁州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
站长推荐
            
        热点阅读
            

