简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM