当前位置:网站首页>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 .
边栏推荐
- Wechat applet request requests to carry cookies to verify whether it has logged in
- JS cannot get content disposition in headers
- [notes for question brushing] search the insertion position (flexible use of dichotomy)
- Incremental crawler in distributed crawler
- 关于GBase 自动关闭连接问题
- [300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (V)
- Leetcode118. Yanghui triangle
- BOM概述
- OpenAtom XuperChain 开源双周报 |2022.7.11-2022.7.22
- Huawei wireless device sta black and white list configuration command
猜你喜欢

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

微信小程序switchTab传参以及接收参数

论文阅读:UNET 3+: A FULL-SCALE CONNECTED UNET FOR MEDICAL IMAGE SEGMENTATION

vulnhub CyberSploit: 1

Ideal L9, can't cross a pit on the road?

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

【terminal】x86 Native Tools Command Prompt for VS 2017

Luo min's backwater battle in qudian

冰冰学习笔记:类与对象(上)

Luo min from qudian, prefabricate "leeks"?
随机推荐
OpenAtom XuperChain 开源双周报 |2022.7.11-2022.7.22
paddlepaddle 34 调整模型的layer结构与forward流程(实现layer的增删改与forward的修改)
The relationship between Informatics, mathematics and Mathematical Olympiad (July 19, 2022) C
【PyTorch】最常见的view的作用
Summary of differences between data submission type request payload and form data
Openatom xuprechain open source biweekly report | 2022.7.11-2022.7.22
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (V)
Electronic Association C language level 2 60, integer parity sort (real question in June 2021)
从ACL 2022 Onsite经历看NLP热点
Incremental crawler in distributed crawler
2022天工杯CTF---crypto1 wp
华为无线设备配置WPA2-802.1X-AES安全策略
【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了
阿里云镜像地址&网易云镜像
9大最佳工程施工项目管理系统
[programmer 2 Civil Servant] III. resource collection
【程序员2公务员】关于体制调研的一些常见问题总结
NLP hotspots from ACL 2022 onsite experience
How to do a good job in safety development?
Scavenging vultures or woodpeckers? How to correctly understand short selling