0

I'm making a game whith QGraphics framework.

When the game finish I want to blur the whole graphicsview and show a widget with some text on top of it.

My simplified code is :

In view :

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();
}

In text widget :

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

But as you can see in image bellow text become blur and unreadable with view too.

How can I remove graphicsEffect from child widget and still keep background color of my widget semi transparent ?

everything is blurred :(

uchar
  • 2,552
  • 4
  • 29
  • 50
  • Does it have to be a child widget? Why can't you use `WigglyWidget *wigglyWidget = new WigglyWidget;`? Just make sure you destroy the widget. – thuga Mar 11 '16 at 08:17
  • @thuga for 3 reason : 1_ If it's not child widget then making it semi transparent doesn't work ! 2_showing it make a section of view unblurred 3_ clicking on view widget hide it – uchar Mar 11 '16 at 08:22
  • @thuga see the result : http://i.stack.imgur.com/wQL3O.png – uchar Mar 11 '16 at 08:23
  • What should happen after `you loose` is shown? By the way, it should be you lose :) – thuga Mar 11 '16 at 08:38
  • @thuga "you lose text " is animating in a sin shape and this isn't complete yet . I have to add a button and score text or stars ... . then clicking on button go back to main menu or restart the game – uchar Mar 11 '16 at 08:47
  • Well maybe you could embed your `WigglyWidget` in a dialog (or make itself a dialog), make the dialog transparent with [`setWindowOpacity`](http://doc.qt.io/qt-5/qwidget.html#windowOpacity-prop) and set `Qt::WA_TranslucentBackground` attribute to it, if you choose to embed `WigglyWidget` into a dialog, and set `Qt::FramelessWindowHint | Qt::Dialog` window flags to it. Would that work for you? – thuga Mar 11 '16 at 08:53
  • @thuga I still have the second problem http://i.stack.imgur.com/yVvWb.png – uchar Mar 11 '16 at 09:05
  • I add `update()` after showing dialog , and now it works fine. thank,please make it an answer and I will accept/upvote it – uchar Mar 11 '16 at 09:17

1 Answers1

1

You can embed your WigglyWidget in a dialog, or make itself a dialog.

You can then make it transparent by setting the window opacity with setWindowOpacity.

If you embedded your WigglyWidget into a dialog, you have to set the Qt::WA_TranslucentBackground attribute to the dialog to make the background transparent.

Then you have to set the Qt::FramelessWindowHint | Qt::Dialog flags to it to get rid off the title bar.

Heres an example:

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();
thuga
  • 12,601
  • 42
  • 52