qtbitmapqgraphicsviewqgraphicsitempixmap

Displaying a Pixmap on a Graphics View Widget Object in Qt


I am trying to display a pixmap on the Graphic View widget. I believe what it is missing is a way to actually link the QGraphicsScene "scene" to the widget I have on the form, seeing that you can create multiple Graphics View Objects there must be a way to explicitly state which Graphics View item you want the pixmap to be displayed on. The Graphics View widget object is called PixMapView, using the same name as my GraphicsView object in my code did not change the functionality of my program. The code below compiles but just shows a blank white Graphics View object. My goal is to create a diagonal line on the pixmap and display it on the Graphics View Object. Previously the first section of code, all the way to the start of the while loop, worked with fillRect and some Auto-fill backround code. I believe the problem is purely in the section of code after the while loop *The while loop is essentially the only part not from Qt documentation. So in short could someone look at this code and see why my pixmap is not displaying on my Graphics View Widget.

 CanvasTest::CanvasTest(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CanvasTest)
{
    ui->setupUi(this);
    QPixmap pm(200,200);
    QPainter pmp(&pm);
    pmp.setPen(Qt::black);
    int counter = 0;
    while (counter < 200)
    {
        pmp.drawPoint(counter,counter);
        counter++;
    }

    pmp.end();
    QGraphicsScene scene;
    QGraphicsPixmapItem item(pm);
    scene.addItem(&item);
    QGraphicsView view(&scene);
    view.show();
}

What the form looks like with the Graphic View Widget

I would like to display the diagonal line on the widget presented in the image


Solution

  • ui->(whatever name your widget has)->( any method your widget is associated with)
    

    Note: this is due to the automatic code ui->setupUI(this);