从子窗口小部件中删除QGraphicsEffect

时间:2016-03-11 07:14:28

标签: c++ qt qwidget qgraphicsview

我正在制作一个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并仍保持窗口小部件的背景颜色为半透明?

everything is blurred :(

1 个答案:

答案 0 :(得分:1)

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

然后,您可以通过使用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();
相关问题