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
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I'm creating a kind of "Framework" to do this. There are 2 approaches:
- Handle mouse messages, create a QGraphicsLineItem object, add to Scene and modify it during the creation process.
- 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.
回答2:
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.