是的,这是可能的.关键是QWidget的Qt :: WA_TranslucentBackground属性
原文链接:https://www.f2er.com/windows/370146.html这是一个简单的类,绘制一个圆形半透明窗口,红色背景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.