[Solved] QTgraphics using Designer



  • I wold like to display a Jpeg image on a QGraphicsView objet I have created with the designer.
    I followed several examples I found here but the image window is always white.
    This is the method I call from main after the main window is displayed.

    @void ImaferensMainWindow::ShowIma()
    {
    QGraphicsView qtv = ui->graphicsView;

    qtv.setRenderHint(QPainter::Antialiasing);
    QPixmap img = QPixmap("C:\\ProgettiPC\\QT\\imaferens\\aereo.jpg");
    qtv.setBackgroundBrush(img);
    qtv.setCacheMode(QGraphicsView::CacheBackground);
    qtv.setAlignment(Qt::AlignCenter);
    qtv.setContentsMargins(QMargins::QMargins(5, 5, 5, 5));
    qtv.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Map Test"));
    qtv.resize(720, 576);
    qtv.show();
    

    }@

    Thank you.



  • Well I didn't work with the Designer before but I worked with GraphicsView framework !
    So I think that you have to use QGraphicsScene ?

    @
    scene = new QGraphicsScene(qobject_parent);
    qtv->setScene(scene);
    @

    Without a scene the window is always white !
    To display the image you have firstly add it to the scene using :

    @
    scene->addPixmap(image);
    @

    The scene is the model (data) and the view will display the items of the scene ;)



  • Thank you Issam. I tried a lot but it does not work.
    You say the code shold be something like this:

    @void ImaferensMainWindow::ShowIma()
    {

    QGraphicsScene scene = new QGraphicsScene(this);
    QGraphicsView *qtv =  ui->graphicsView;
    
    QPixmap img = QPixmap("C:\\ProgettiPC\\QT\\imaferens\\aereo.jpg");
    
    scene.addPixmap(img);
    
    qtv->setScene(&scene);
    
    qtv->setRenderHint(QPainter::Antialiasing);
    qtv->setBackgroundBrush(img);
    qtv->setCacheMode(QGraphicsView::CacheBackground);
    qtv->setAlignment(Qt::AlignCenter);
    qtv->setContentsMargins(QMargins::QMargins(5, 5, 5, 5));
    qtv->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Test"));
    qtv->resize(720, 576);
    qtv->show();
    

    }
    @

    It does not work. It seems that the view is not connected to the scene, the image pointer is not null, but the view area is always deep white.



  • You can make the following changes in your code. This works for me

    @QGraphicsScene *scene = new QGraphicsScene();
    QGraphicsView *qtv = ui->graphicsView;

    QPixmap img = QPixmap("C:/ProgettiPC/QT/imaferens/aereo.jpg"); // you can use '/' instead of '\' does the same.

    scene->addPixmap(img);
    qtv->setScene(scene);
    qtv->setRenderHint(QPainter::Antialiasing);
    qtv->setBackgroundBrush(img);
    qtv->setCacheMode(QGraphicsView::CacheBackground);
    qtv->setAlignment(Qt::AlignCenter);
    qtv->setContentsMargins(QMargins::QMargins(5, 5, 5, 5));
    qtv->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Test"));
    qtv->resize(720, 576);
    qtv->show();@



  • It works, I understand my error.
    Thank you.



  • Great, Kindly edit your first post and prepend "Solved" to it :)

    Happy Coding.


Log in to reply
 

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