Bad QGraphicsView rendering by combining QGraphicsProxyWidget and QGraphicsDropShadowEffect



  • I have stumbled upon a problem of bad rendering the QGraphicsView when a drop shadow effect is used on its containing widget combined with the fact that its scene contains custom QGraphicsItem with embedded widget. Here is an example especially prepared to demonstrate this issue (it is not the code of my application, but a minimal one to show what happens). Please note, that commenting of either or both of the designated lines removes this undesired effect. How do you think - is this a bug in Qt, or something is missing in the code to make it work properly? Any ideas will be much appreciated.

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QApplication>
    #include <QDesktopWidget>
    #include <QVBoxLayout>
    #include <QWidget>
    #include <QGraphicsDropShadowEffect>
    #include <QGraphicsProxyWidget>
    #include <QGraphicsRectItem>
    #include <QGraphicsView>
    #include <QLineEdit>
    #include <QBrush>
    
    class MyRect : public QGraphicsRectItem
    {
    public: MyRect();
    
    protected:
    QLineEdit *w;
    QGraphicsProxyWidget *g;
    
    };
    
    class Form : public QWidget
    {
    public:
        explicit Form(QWidget *parent = 0);
        void setScene(QGraphicsScene *scene);
    
    private: QGraphicsView *view;
    };
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public: explicit MainWindow(QWidget *parent = 0);
    };
    
    #endif // MAINWINDOW_H
    

    MainWindow.cpp

    #include "MainWindow.h"
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        QGraphicsScene *scene = new QGraphicsScene(this);
    
        QGraphicsView *view = new QGraphicsView(this);
        view->setScene(scene);
    
        setCentralWidget(view);
    
        QRect g = QApplication::desktop()->screenGeometry();
        int h = g.height(), w = g.width();
        setGeometry((w-800)/2, (h-600)/2, 800, 600);
    
        Form *form = new Form(this);
        form->setGeometry(0, 0, 198, 763);
        form->setScene(scene);
    
        for (int n = 0; n < 7; n++)
        {
            MyRect *r = new MyRect();
            r->setPos(20, 70*n + 50);
            scene->addItem(r);
        }
    }
    
    MyRect::MyRect()
    {
        setFlags(ItemIsSelectable | ItemIsMovable);
        setBrush(QBrush(QColor(Qt::blue)));
        setRect(0, 0, 90, 60);
    
        w = new QLineEdit();
        w->setGeometry(0, 0, 90, 20);
        g = new QGraphicsProxyWidget(this);
        g->setWidget(w); // Comment this: problem is gone, but no Line Edit
    }
    
    Form::Form(QWidget *parent) : QWidget(parent)
    {
        view = new QGraphicsView(this);
        view->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    
        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(view);
    
        setLayout(layout);
    
        QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect();
        effect->setOffset(0);
        effect->setColor(QColor(150, 150, 150));
        effect->setBlurRadius(5);
        setGraphicsEffect(effect); // Comment this: problem is gone, but no shadow
    }
    
    void Form::setScene(QGraphicsScene *scene)
    {
        view->setScene(scene);
    }
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    From your description, it looks like QTBUG-44355, isn't it ?



  • Hi, and thank You for the answer!

    Seems similar, but to be honest, I don't quite understand the post from the link. Anyway, if it is an already known issue and will be corrected - super!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.