开发者

how to use QT graphic view for drawing

im trying to use the graphic veiw of QT to开发者_如何转开发 draw lines it's possible to draw a multiple objects in the scene but is it possible to do a (real time lines ) drawing inside the Qt scene , and how ?

a sample code would be highly appreciated

thanks in advance


I'm creating a kind of "Framework" to do this. There are 2 approaches:

  1. Handle mouse messages, create a QGraphicsLineItem object, add to Scene and modify it during the creation process.
  2. Derive QGraphicsScene, create a QGraphicsLineItem but NOT added to scene, draw it when drawForeground, added it to scene after finished the creation.

Because QGraphicsScene will index objects in a BSP tree by default, and it will impact the performance when changing items frequently, you can get higher performance when using the 2nd approach during creation, but more code work.


1) Create GraphicsView and Scene

m_graphScen = new QGraphicsScene;
m_graphScen->setSceneRect(0,0,790,290);

m_graphView = new QGraphicsView;
m_graphView->setFixedSize(800, 300);
m_graphView->setScene(m_graphScen);

2) Create a slot which is doing the following by handling the mouse events:

m_graphScen->addLine(0, 250, 700, 250, QPen(QBrush(Qt::black),1));
m_graphView->show();

Also if you need to write or draw text see here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜