用Qt显示半透明/不规则形状的窗口 [英] Displaying translucent / irregular-shaped windows with Qt

查看:350
本文介绍了用Qt显示半透明/不规则形状的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Qt显示半透明和/或不规则形状的窗口?

Is it possible to display translucent and/or irregular-shaped windows with Qt?

(我假设它最终取决于底层GUI系统的功能,但我们至少要假设Windows XP / Mac OS X)

(I'm assuming it ultimately depends on the capabilities of the underlying GUI system, but let's assume at least Windows XP / Mac OS X)

如果是这样,一个人怎么做到的?

If so, how does one accomplish this?

推荐答案

是的,有可能。关键是 QWidget

Yes, it is possible. The key is the Qt::WA_TranslucentBackground attribute of QWidget

这是一个简单的类,它绘制一个圆形的半透明窗口,其红色背景的alpha值为50%。

Here is a simple class that draws a round translucent window with a red background 50% alpha.

#include <QWidget>

class TranslucentRoundWindow : public QWidget
{
    public:
        TranslucentRoundWindow(QWidget *parent = 0);
        virtual QSize sizeHint() const;

    protected:
        virtual void paintEvent(QPaintEvent *paintEvent);
};



TranslucentRoundWindow.cpp:



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, 0, 0, 127));

    painter.drawEllipse(0, 0, width(), height());
}

如果您希望能够使用鼠标移动此窗口,则将必须重写 mousePressEvent mouseMoveEvent mouseReleaseEvent

If you want to be able to move this window with the mouse, you will have to override mousePressEvent, mouseMoveEvent and mouseReleaseEvent.

这篇关于用Qt显示半透明/不规则形状的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆