从子部件中删除 QGraphicsEffect

Remove QGraphicsEffect from child widget

我正在使用 QGraphics 框架制作游戏。

游戏结束后,我想模糊整个图形视图并显示一个顶部带有一些文本的小部件。

我的简化代码是:

在视图中:

void GraphicsTraxView::showGameResultWidget(bool res)
{
    blurEffect_->setEnabled(true);
    WigglyWidget *wigglyWidget = new WigglyWidget(this);
    if(res)
      wigglyWidget->setText("you win");
    else 
      wigglyWidget->setText("you loose");

    wigglyWidget->resize(150,150);
    wigglyWidget->move(QPoint(
        getScreenSize().width() / 2 -75, getScreenSize().height() / 2-75));
    wigglyWidget->show();
}

在文本小部件中:

void WigglyWidget::paintEvent(QPaintEvent * /* event */)
{
    QColor backgroundColor = palette().light().color();
    backgroundColor.setAlpha(220);
    QPainter customPainter(this);
    customPainter.fillRect(rect(), backgroundColor);
   //...

但是正如您在图像中看到的那样,下面的文字也变得模糊并且无法在视图中读取。

如何从子窗口小部件中删除 graphicsEffect 并仍然保持我的窗口小部件的背景颜色半透明?

您可以将您的 WigglyWidget 嵌入到对话框中,或使自己成为一个对话框。

然后您可以通过将 window 不透明度设置为 setWindowOpacity 来使其透明。

如果您将 WigglyWidget 嵌入到对话框中,则必须为对话框设置 Qt::WA_TranslucentBackground 属性以使背景透明。

然后你必须为它设置 Qt::FramelessWindowHint | Qt::Dialog 标志以摆脱标题栏。

举个例子:

QDialog *pop_up = new QDialog(this);
pop_up->resize(200, 200);
pop_up->setAttribute(Qt::WA_TranslucentBackground); 
pop_up->setWindowOpacity(0.5);
pop_up->setStyleSheet("QDialog{background-color: transparent;}");
pop_up->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
pop_up->setLayout(new QVBoxLayout);

QLabel *transparentLabel = new QLabel("TRANSPARENT");
transparentLabel->setAlignment(Qt::AlignCenter);
transparentLabel->setStyleSheet("QLabel{color: red; font: 20pt bold; background-color: #00baff;}");
pop_up->layout()->addWidget(transparentLabel);
pop_up->show();