简体   繁体   English

在Qt中的QGraphicsScene中绘画

[英]Painting in QGraphicsScene in qt

I am trying to paint a line in a QGraphicsScene . 我正在尝试在QGraphicsScene绘制一条线。 I know how to add items in a scene but not how to add items in the scene that are drawn using painter. 我知道如何在场景中添加项目,但不知道如何在场景中添加使用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. 您应该继承QGraphicsItem并重写paint方法。 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 . 请记住,在继承的项目中,您必须至少实现两个纯虚拟方法paintboundingRect You can see that these functions are pure virtual by looking in the documentation of QGraphicsItem : QGraphicsItem. 通过查看QGraphicsItemQGraphicsItem的文档,可以看到这些函数是纯虚拟的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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