一、 实验任务(实验题目、目的)
实现二叉树
二、 任务分析
- 假设已经有了二叉树,设计可视化算法实现图像
- 学习qt
- 设置各按钮槽函数
- 将可视化算法与二叉树缝合
- 优化代码画图步骤
- Debug
- 准备发csdn和GitHub
三、 实验设计(实验环境、算法、步骤、核心代码等)
环境:QT5 使用QT CREATOR编程(内置帮助快速查询,很方便)
核心可视化代码如下 二叉树的内容较为简单,不当核心。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47//x,y start at 0 void MainWindow::drawNode(Node* node,int x_node ,int y_node,QPainter* painter){ // const int rate_height = 7; // const int rate_width = 10; // const int rec_width = this->width()/rate_width-5; // const int rec_height = this->height()/rate_height-4; if(node==nullptr) return ; QPoint point(getRecPos(y_node,x_node)); QRect rec(point,size); // QRect rec(x_node*(recWidth+5),y_node*(recHeight+2),recWidth,recHeight); painter->setPen(QPen(Qt::blue,3));//设置画笔形式 或者Qpen pen; pen.setColor(QColor(40, 115, 216)); pen.setWidth(2); painter->drawRect(rec); painter->setPen(QPen(Qt::black)); QFont font("微软雅黑",18); painter->setFont(font); painter->drawText(rec,Qt::AlignCenter,QString::number(node->value())); qDebug() << "drawNode:"<< node->value() << y_node << x_node; if(y_node >= 1){ if(x_node & 1){ QPoint p1(getRecPos(y_node,x_node)); QPoint p2(getRecPos(y_node-1,(x_node-1)/2)); p1+=QPoint(recWidth/2,0); p2+=QPoint(recWidth/2,recHeight); painter->drawLine(p1,p2); } else { QPoint p1(getRecPos(y_node,x_node)); QPoint p2(getRecPos(y_node-1,x_node/2)); p1+=QPoint(recWidth/2,0); p2+=QPoint(recWidth/2,recHeight); painter->drawLine(p1,p2); } } if(node->left!=nullptr) drawNode(node->left,x_node*2,y_node+1,painter); if(node->right!= nullptr) drawNode(node->right,x_node*2+1,y_node+1,painter); }
四、 实验结果
如图
完整项目请访问:https://github.com/gongfpp/BinaryTreeWithQt
最后
以上就是漂亮石头最近收集整理的关于【软件】[Qt\C++] 图形化二叉树——用QT5实现的全部内容,更多相关【软件】[Qt\C++]内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复