当前位置:网站首页>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 .
边栏推荐
- 冰冰学习笔记:类与对象(上)
- Rambus announces ddr5 memory interface chip portfolio for data centers and PCs
- How does uxdb extract hours, minutes and seconds from date values?
- [notes] search rotation sort array
- Common cross domain scenarios
- 《游戏机图鉴》:一份献给游戏玩家的回忆录
- Beijing internal promotion | Microsoft STCA recruits nlp/ir/dl research interns (remote)
- 华为无线设备配置WPA2-802.1X-AES安全策略
- Wei Lai: what is the difference between multithreaded join and detach?
- Can interface debugging still play like this?
猜你喜欢

First, how about qifujin

用VS Code搞Qt6:编译源代码与基本配置

曼哈顿距离简介

Configuring WAPI certificate security policy for Huawei wireless devices

Pads export Gerber file

RPC通信原理与项目技术选型

Box horse "waist cut", blame Hou Yi for talking too much?

Rambus announces ddr5 memory interface chip portfolio for data centers and PCs

list的模拟实现

Vscode saves setting configuration parameters to the difference between users and workspaces
随机推荐
Purpose of SQL square brackets
Statistical learning -- naive Bayesian method
BOM概述
runtimecompiler 和 runtimeonly是什么
Teach you to use cann to convert photos into cartoon style
Leetcode118. Yanghui triangle
Flinkcdc2.0 uses flinksql to collect MySQL
List derivation
Wechat applet wx.request interface
[semidrive source code analysis] [drive bringup] 38 - norflash & EMMC partition configuration
[OBS] DTS sent by video packet_ USEC calculation
[cloud native] the ribbon is no longer used at the bottom of openfeign, which started in 2020.0.x
集群聊天服务器:项目问题汇总
Million level element optimization: real-time vector tile service based on PG and PostGIS
【程序员2公务员】关于体制调研的一些常见问题总结
3. Promise
Robot framework mobile terminal Automation Test ----- 01 environment installation
做好项目管理的10个关键点和5大措施
[computer explanation] NVIDIA released geforce RTX Super Series graphics cards, and the benefits of game players are coming!
How to use network installation to deploy multiple virtual servers in KVM environment