当前位置:网站首页>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 .
边栏推荐
- 30 times performance improvement -- implementation of MyTT index library based on dolphin DB
- Robot Framework移动端自动化测试----01环境安装
- Expandablelistview nested GridView display incomplete problem
- Wechat applet request requests to carry cookies to verify whether it has logged in
- JS cannot get content disposition in headers
- [semidrive source code analysis] [drive bringup] 38 - norflash & EMMC partition configuration
- [semidrive source code analysis] [drive bringup] 39 - touch panel touch screen debugging
- Huawei wireless device configuration wpa2-802.1x-aes security policy
- error: redefinition of
- CTF Crypto---RSA KCS1_ Oaep mode
猜你喜欢

New tea, start "fighting in groups"

Luo min's backwater battle in qudian

Boiling short drama Jianghu: nine of the ten production teams are shooting, with a head sharing fund of more than 30million, and users are addicted to paying routines

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

Openatom xuprechain open source biweekly report | 2022.7.11-2022.7.22

Oracle19采用自动内存管理,AWR报告显示SGA、PGA设置的过小了?

12 combination methods and risk interpretation of database architecture optimization (books available)

数据提交类型 Request Payload 与 Form Data 的区别总结

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

Pads export Gerber file
随机推荐
QT学习日记20——飞机大战项目
用VS Code搞Qt6:编译源代码与基本配置
3. Promise
Wei Lai: what is the difference between multithreaded join and detach?
Meta is in a deep quagmire: advertisers reduce spending and withdraw from the platform
[programmer 2 Civil Servant] I. Basic Knowledge
[computer explanation] NVIDIA released geforce RTX Super Series graphics cards, and the benefits of game players are coming!
MATLAB自编程系列(1)---角分布函数
Ask the bosses: MySQL CDC stores configuration data, and Kafka has history
[semidrive source code analysis] [drive bringup] 38 - norflash & EMMC partition configuration
How to use network installation to deploy multiple virtual servers in KVM environment
Tp5.1 foreach adds a new field in the controller record, and there is no need to write all the other fields again without changing them (not operating in the template) (paging)
How does uxdb extract hours, minutes and seconds from date values?
How to do a good job in safety development?
Robot framework mobile terminal Automation Test ----- 01 environment installation
【刷题笔记】搜索旋转排序数组
error: redefinition of
Openatom xuprechain open source biweekly report | 2022.7.11-2022.7.22
RPC通信原理与项目技术选型
华为无线设备配置WAPI-证书安全策略