Kamalpreet Grewal
Kamalpreet Grewal

Reputation: 852

Painting in QGraphicsScene in qt

I am trying to paint a line in a QGraphicsScene. I know how to add items in a scene but not how to add items in the scene that are drawn using painter. Below is the code I am trying to implement:

void MainWindow::drawLine(){
    painter->drawLine(100,10,200,20);
    item = ??
    scene->addItem(item);
    ui->graphicsView->setScene(scene);
    qDebug() << "Line Created";
}

As you may see, the item is not defined above. How to create an item for the same so that it is painted in the scene?

Upvotes: 2

Views: 5345

Answers (1)

kobigurk
kobigurk

Reputation: 741

You should inherit from QGraphicsItem and override the paint method. I found a nice tutorial regarding it: tutorial.

Keep in mind that in your inherited item, you must implement at least the two pure virtual methods paint and boundingRect. You can see that these functions are pure virtual by looking in the documentation of QGraphicsItem : QGraphicsItem.

Upvotes: 3

Related Questions