Problem mixing QGraphicsItem and QGraphicsProxyWidget



  • Hi,

    I have a simple setup with a graphicsscene where I mix QGraphicsItem and QGraphicsProxyWidget.

    When I mix two of them as in this simple example ( just a basic MainWindow project with a simple scene and two panel widgets). The QGraphicsItem (a rect in this case) is drawing outside of the graphics view when you resize the mainwindow.

    This is the code to reproduce the error.

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    protected slots:
    
    private:
    };
    
    #endif // MAINWINDOW_H
    

    MainWindow.cpp

    #include "mainwindow.h"
    
    #include <QVBoxLayout>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsProxyWidget>
    #include <QLabel>
    #include <QPushButton>
    #include <QBrush>
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        QWidget *centralWdg = new QWidget();
        setCentralWidget(centralWdg);
    
    
        QHBoxLayout *hlayout = new QHBoxLayout();
        QWidget *leftWdg = new QWidget(this);
        leftWdg->setMinimumWidth(200);
    
        QWidget *rightWdg = new QWidget(this);
        rightWdg->setMinimumWidth(200);
    
    
        QGraphicsScene *gs = new QGraphicsScene();
    
        QGraphicsView *gv = new QGraphicsView(gs, this);
        gv->setMaximumWidth(180);
    
        gs->addWidget(new QLabel("Label"));
    
        hlayout->addWidget(leftWdg);
        hlayout->addWidget(gv);
        gv->setBackgroundBrush(QBrush(Qt::yellow, Qt::DiagCrossPattern));
        hlayout->addWidget(rightWdg);
    
        QGraphicsItem *r = gs->addRect(50,50,150,50, QColor(Qt::red), QColor(Qt::blue));
        r->setFlags(QGraphicsItem::GraphicsItemFlag::ItemIsMovable);
    
        centralWdg->setLayout(hlayout);
    }
    
    MainWindow::~MainWindow()
    {
    
    }
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    And this is the rendering i obtain:

    https://s13.postimg.io/734u4kmpz/rendering_Problem.png

    Not sure if I am missing something or is it a bug or limitation in QT QGraphicsView framework.

    Running with Qt 5.7.0 and windows 8.1

    thanks,

    Xllr


Log in to reply
 

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