当前位置:网站首页>Analysis of difficulties in diagramscene project
Analysis of difficulties in diagramscene project
2022-07-25 07:21:00 【Jingchu idlers】
1. Engineering effect
This project is very similar miscrosoft visio Software flow chart components :

2. Project storage path
Examples\Qt-XX.XX.XX\widgets\graphicsview\diagramscene Under the table of contents ,XX.XX.XX by Qt Version number of , Such as :5.14.1.
3. Analysis of engineering difficulties
There are two difficult points in this project , One of the codes is as follows :
void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (line != nullptr && myMode == InsertLine) {
QList<QGraphicsItem *> startItems = items(line->line().p1());
if (startItems.count() && startItems.first() == line)
startItems.removeFirst();
QList<QGraphicsItem *> endItems = items(line->line().p2());
if (endItems.count() && endItems.first() == line)
endItems.removeFirst();
removeItem(line);
delete line;
//! [11] //! [12]
if (startItems.count() > 0 && endItems.count() > 0 &&
startItems.first()->type() == DiagramItem::Type &&
endItems.first()->type() == DiagramItem::Type &&
startItems.first() != endItems.first()) {
DiagramItem *startItem = qgraphicsitem_cast<DiagramItem *>(startItems.first());
DiagramItem *endItem = qgraphicsitem_cast<DiagramItem *>(endItems.first());
Arrow *arrow = new Arrow(startItem, endItem);
arrow->setColor(myLineColor);
startItem->addArrow(arrow);
endItem->addArrow(arrow);
arrow->setZValue(-1000.0);
addItem(arrow);
arrow->updatePosition();
}
}
//! [12] //! [13]
line = nullptr;
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}The first 4~13 That's ok : Detect dragging the mouse to scribe 、 When you release the mouse , The first item corresponding to the items below the starting and ending points of the line is not the line object itself , If yes, the line object is removed from startItems 、endItems Delete , Because the original intention of dragging the mouse to draw a line is to draw two non-linear item Connect . It's meaningless for a straight line to connect itself .
The first 16~28 That's ok : If you remove startItems 、endItems after ,startItems 、endItems The container is not empty , prove startItems 、endItems Non linear objects are stored in (item), Then take them out separately startItems 、endItems First of all , That is, take out two non-linear item, Create an arrow object (Arrow ), Set up Arrow Object color 、z order , And set it to the non-linear line just taken out item Go to the object . Place the arrow object (Arrow ) Add to the scene and call Arrow::updatePosition() Function to update the arrow object (Arrow ) Location . When calling Arrow::updatePosition() when , Will be in these two again item Create a line object between .
Another difficulty is as follows :
void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
QWidget *)
{
if (myStartItem->collidesWithItem(myEndItem))
return;
QPen myPen = pen();
myPen.setColor(myColor);
qreal arrowSize = 20;
painter->setPen(myPen);
painter->setBrush(myColor);
//! [4] //! [5]
QLineF centerLine(myStartItem->pos(), myEndItem->pos());
QPolygonF endPolygon = myEndItem->polygon();
QPointF p1 = endPolygon.first() + myEndItem->pos();
QPointF intersectPoint;
for (int i = 1; i < endPolygon.count(); ++i) {
QPointF p2 = endPolygon.at(i) + myEndItem->pos();
QLineF polyLine = QLineF(p1, p2);
QLineF::IntersectionType intersectionType =
polyLine.intersects(centerLine, &intersectPoint);
if (intersectionType == QLineF::BoundedIntersection)
break;
p1 = p2;
}
setLine(QLineF(intersectPoint, myStartItem->pos()));
//! [5] //! [6]
double angle = std::atan2(-line().dy(), line().dx());
QPointF arrowP1 = line().p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + M_PI / 3) * arrowSize);
QPointF arrowP2 = line().p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + M_PI - M_PI / 3) * arrowSize);
arrowHead.clear();
arrowHead << line().p1() << arrowP1 << arrowP2;
//! [6] //! [7]
painter->drawLine(line());
painter->drawPolygon(arrowHead);
if (isSelected()) {
painter->setPen(QPen(myColor, 1, Qt::DashLine));
QLineF myLine = line();
myLine.translate(0, 4.0);
painter->drawLine(myLine);
myLine.translate(0,-8.0);
painter->drawLine(myLine);
}
}The first 14~26 That's ok : take myStartItem Item location point and myEndItem Create a line object at the item location point centerLine. take myEndItem Each vertex of the polygon where the item is located is taken out and converted into scene coordinates , Then judge whether each edge of the polygon is consistent with centerLine The intersection , If it intersects , Then take out the intersection , And use setLine function , Starting from the intersection ,myStartItem The position point of the item is the end point , change Arrow Line of class object .
The first 31~42: Draw a straight arrow .
The first 43~49: When the straight arrow is selected , On the top of the straight line drawn in the previous step 、 Draw below 1 A dotted line with a pen width of pixels , This gives the impression that the straight arrow is selected .
After this operation , will myStartItem、myEndItem Connected with straight arrows , And can realize the feeling of selection .
3 There are bug
This project DiagramScene Class setLineColor、setTextColor、setItemColor Functions exist bug, Now setLineColor Give an example of , Other similar .
void DiagramScene::setLineColor(const QColor &color)
{
myLineColor = color;
if (isItemChange(Arrow::Type)) {
Arrow *item = qgraphicsitem_cast<Arrow *>(selectedItems().first());
item->setColor(myLineColor);
update();
}
}isItemChange Function as follows :
bool DiagramScene::isItemChange(int type) const
{
const QList<QGraphicsItem *> items = selectedItems();
const auto cb = [type](const QGraphicsItem *item) { return item->type() == type; };
return std::find_if(items.begin(), items.end(), cb) != items.end();
}setLineColor Function means to check whether there is an arrow type item in the selected item , If there is , Then take out the first item in the selected item list , And set it to setLineColor Color of function parameters , The problem is : The first item in the selected item list may not be an arrow type item , This is the following code :
Arrow *item = qgraphicsitem_cast<Arrow *>(selectedItems().first());Back to item May be nullptr, namely qgraphicsitem_cast<Arrow *> switch views , Lead to item by nullptr, This will cause the following code to crash . Try a box on the left , A box on the right , Then the two boxes are connected by straight arrows , Then check the box on the left , Then select the arrow , Then it collapsed .
边栏推荐
- 解密NumPy求解梯度的一个关键难点
- CTF Crypto---RSA KCS1_ Oaep mode
- list的模拟实现
- Expandablelistview nested GridView display incomplete problem
- [programmer 2 Civil Servant] summary of some common problems about system research
- 用VS Code搞Qt6:编译源代码与基本配置
- Servlet常用类剖析
- A domestic open source redis visualization tool that is super easy to use, with a high-value UI, which is really fragrant!!
- 华为无线设备STA黑白名单配置命令
- 12 combination methods and risk interpretation of database architecture optimization (books available)
猜你喜欢

Configuring WAPI certificate security policy for Huawei wireless devices

Xinku online | cnopendata shareholder information data of A-share listed companies

Day by day, month by month | Shenzhen potential technology released the extreme accelerated version of molecular docking engine uni docking

第一启富金怎么样

Leetcode118. Yanghui triangle

"Game illustrated book": a memoir dedicated to game players

Teach you to use cann to convert photos into cartoon style

Beijing internal promotion | Microsoft STCA recruits nlp/ir/dl research interns (remote)

一日千里,追风逐月 | 深势科技发布极致加速版分子对接引擎Uni-Docking

用VS Code搞Qt6:编译源代码与基本配置
随机推荐
File operation-
新库上线| CnOpenDataA股上市公司股东信息数据
做好项目管理的10个关键点和5大措施
Huawei wireless device sta black and white list configuration command
diagramscene工程难点分析
DJI内推码(一码一用,限时内推)
Elasticserach里delete_by_query的机制是什么?
Common cross domain scenarios
Summary of differences between data submission type request payload and form data
With apple not making money, the 2trillion "fruit chain" abandons "fruit" and embraces "special"
The relationship between Informatics, mathematics and Mathematical Olympiad (July 19, 2022) C
论文阅读:UNET 3+: A FULL-SCALE CONNECTED UNET FOR MEDICAL IMAGE SEGMENTATION
MATLAB自编程系列(1)---角分布函数
【SemiDrive源码分析】【驱动BringUp】39 - Touch Panel 触摸屏调试
Box horse "waist cut", blame Hou Yi for talking too much?
Special analysis of data security construction in banking industry
[programmer 2 Civil Servant] IV. common problems
vulnhub CyberSploit: 1
【刷题笔记】搜索旋转排序数组
How does uxdb extract hours, minutes and seconds from date values?