How to fix display problem when I use QGraphicsView and QGraphicsEffect together in Qt?

129 views Asked by At

I have a problem when use QGraphicsView and QGraphicsBlurEffect in my project. When I put them together, my program does not work normally. I wrote a tiny program to reproduce this problem.

The Widget class is inherited from QGraphicsView.

Widget::Widget(QWidget *parent)
    : QGraphicsView(parent)
{
    scene = new QGraphicsScene(this);
    this->setScene(scene);

    label = new QLabel;
    QPixmap pixmap = QPixmap("../partly_cloudy.png").scaledToWidth(200);
    label->setPixmap(pixmap);
    label->setGeometry(100,100, pixmap.width(), pixmap.height());
    label->setStyleSheet("border:3px;border-color: rgb(255, 100, 0); border-style:solid;");

    /* ********image won't show when adding following comment code********
    QGraphicsBlurEffect *blur = new QGraphicsBlurEffect;
    blur->setBlurRadius(10);
    label->setGraphicsEffect(blur);
    ******* */

    QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
    proxyWidget->setWidget(label);
    proxyWidget->setPos(10,10);

    scene->addItem(proxyWidget);
}

and main.cpp is as follows.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

This is a screenshot when QGraphicsBlurEffect is not used.

Without effect

However, this is a screenshot when QLabel uses setGraphicsEffect() to bind blur effect.

With effect

To solve this problem, I tried to use a QWidget to wrap QLabel. When I did this, QLabel was rendered. However, it seems to be bounded by a rectangle area.

Widget::Widget(QWidget *parent)
    : QGraphicsView(parent)
{
    scene = new QGraphicsScene(this);
    this->setScene(scene);

    /* ********/
    container = new QWidget;
    container->setStyleSheet("border:3px;border-color: blue; border-style:solid;");
    /******** */

    label = new QLabel(container);
    QPixmap pixmap = QPixmap("../partly_cloudy.png").scaledToWidth(200);
    label->setPixmap(pixmap);
    label->setGeometry(100,100, pixmap.width(), pixmap.height());
    label->setStyleSheet("border:3px;border-color: red; border-style:solid;");

    QGraphicsBlurEffect *blur = new QGraphicsBlurEffect;
    blur->setBlurRadius(10);
    label->setGraphicsEffect(blur);

    QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
    proxyWidget->setWidget(container);
    proxyWidget->setPos(80,80);

    qDebug() << proxyWidget->boundingRect();

    scene->addItem(proxyWidget);
    this->setSceneRect(0,0,640,480);
}

The screenshot of the result is.

Result of the program

I tried to set proxyWidget position to {0,0}, and it works normally. it seems that the position of effect rectangle will not influenced by proxyWidget position.

By the way, the version of Qt is 5.14.2.

I've searched for a long time on net. But no use. Please help or try to give some ideas how to achieve this.

0

There are 0 answers