QWidget 设置窗体背景透明 实现如下效果
#include <QMainWindow>
#include <QtWidgets>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class CustomWidget : public QWidget {
public:
CustomWidget(QWidget *parent = nullptr) : QWidget(parent) {
QString qss = R"(
background-color: rgba(255, 255, 255, 179);
color:white;
border:none;
)";
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setMargin(0);
QRadioButton * radioButton= new QRadioButton("play demo");
radioButton->setStyleSheet(R"(
color:#000000;
font-size:14px;
min-height:200px;
)");
layout->addWidget(radioButton);
setLayout(layout);
resize(200, 100);
setStyleSheet(qss);
setWindowFlags(Qt::FramelessWindowHint);
// 设置窗口属性以允许透明
setAttribute(Qt::WA_TranslucentBackground);
}
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
void mousePressEvent(QMouseEvent *event) override {
if (event->button() == Qt::RightButton) {
CustomWidget *customWidget = new CustomWidget();
customWidget->move(event->globalPos());
customWidget->show();
}
}
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H