Important: Please read the Qt Code of Conduct -

How do I pass a QGraphicsView from inside a widget to an external widget?

  • I have a QGraphicsView inside a custom class and I want to send the QGraphicsView out of the class. The idea being that I have a class that I load a QString with the location of an image file, and it displays the image on the QGraphicsView that was placed in an external widget.
    I got this working with a QLabel using a function like

    QLabel* ImgdatReader::getDateLabel()
    return dateLabel;

    but when I do the same with a QGraphicsView the view shows up completely empty. I am at a complete loss here.

    //class header
    QGraphicsView *imgView;
    QGraphicsScene *imgScene;
    QGraphicsPixmapItem *imgGraphicsPixmapItem;
    QPixmap *imgPixmap;

    //class constructor:
    imgView = new QGraphicsView (this);
    imgScene = new QGraphicsScene(this);
    imgPixmap = new QPixmap;

    //class member function:
    imgPixmap = new QPixmap(QPixmap::fromImage(tmpImage));

    //class member function delivering imgView
    QGraphicsView* ImgdatReader::getImgView()
    return imgView;

    //External function
    QGraphicsView *myImageGV = imageReader.getImgView();

    I can't see what I am doing wrong. It seems like the same exact approach works with QLabels. dateLabel even properly displays the image, but due to design requirements, I need it to show up in a QGraphicsView, not a QLabel.
    I could really use some help, Thanks

  • @bdmontz
    Oh yeah, using Qt creator 3.5.0 (opensource), based on Qt 5.5.0

  • I found if I pass out the QPixmap and load it into an external QGraphicsView and QGraphicsScene it works. I'm not sure why passing the QGraphicsView doesn't work. Can anyone explain what is going on? I've only been using C++ and Qt since June.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Before anything else, why did you place these GUI classes in ImageReader ? It looks like you don't use them in ImageReader

  • Hi SGaist,

    I have only been using C++ and Qt since june, and have been mostly teaching myself, so some of my understanding of how things work are probably incorrect. Let me start with the purpose of the class, and then explain why I have used those gui classes.

    The purpose of the class is to load a custom format image file from a directory path that is passed in a function call. The class is supposed to load the custom image into a QByteArray, extract data from it, which it then updates labels with the current values, and updates a graphics view with the actual image.

    The way that I wanted to use this class is to have an external widget, lets say a MainWindow, and that widget creates an object from the ImageReader class. It then calls the ImageReader to get pointers to the QLabels and QGraphicsView to place in the central widget of the MainWindow object. The idea being that when I load a new image in the ImageReader object, those objects on the MainWindow will automatically show the values corresponding to the new image.

    The reason I used the QGraphicsView, QGraphicsScene, and QPixmap are because those are what I've seen used in examples and tutorials, and I'm not exactly sure how they work together. I know I can make it work by placing it in a QLabel, but I also need to zoom in and out in my program, so I would like to use the scale functionality of the QGraphicsScene.

    I found that when I keep the View and Scene on the external widget(the MainWindow in the above example) and simply pass the QPixmap through, it works. But I do not understand why it doesn't work when I just pass the QGraphicsView.

    So there is my long answer to your simple question.

  • Lifetime Qt Champion

    Ok, things are clearer.

    Your ImageReader class currently has too many responsibilities. Basically, that's not his role to manage whatever will show the image it loaded or its information. It should rather be a provider of them i.e. trigger signals that notifies whatever is connected that it should update their status like a label updating its text.

    I'd recommend taking the time to get the C++ basics in (like encapsulation) and then the following rules:

    • K.I.S.S.
    • D.R.Y.
    • Single responsibility

    then move on to patterns.

Log in to reply