用qt画了个很简单的柱状图,横坐标长度不变,个数越多,每个柱状图的宽度越窄。没什么难度,写过的就记录一下,以后可能用到类似的直接拿来用。
头文件
复制代码
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#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMap> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); protected: void paintEvent(QPaintEvent *e); private: int getMaxVal(QList<int> list); private: QMap<QString, int> m_answerMap; QList<int> m_numList; QList<QString> m_answerStrList; int m_nPeopleCount; }; #endif // MAINWINDOW_H
cpp文件
复制代码
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119#include "mainwindow.h" #include <QPainter> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->resize(500, 570); m_answerMap.insert("A", 2); m_answerMap.insert("ABCD", 1); m_answerMap.insert("AD", 3); m_answerMap.insert("BD", 10); m_answerMap.insert("AC", 2); m_answerMap.insert("BCD", 1); m_nPeopleCount = 0; m_answerStrList = m_answerMap.keys(); m_numList = m_answerMap.values(); foreach (int num, m_numList) { m_nPeopleCount += num; } } MainWindow::~MainWindow() { } void MainWindow::paintEvent(QPaintEvent *e) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); painter.setPen(Qt::NoPen); painter.setBrush(Qt::NoBrush); if(m_numList.isEmpty()) { return; } painter.setPen(Qt::black); int lineWidth = 360; int lineX = this->width()/2 - lineWidth/2; int lineY = 402; painter.drawLine(lineX, lineY, lineX + lineWidth, lineY); int answerCount = m_answerStrList.count(); int maxWidth = 41; int maxHeight = 280; int curWidth = maxWidth - (answerCount - 1)*3; int unitWidth = lineWidth/answerCount; //获取选项中值最大的那个,以这个为基准高度,来计算其他值的高度 int maxVal = getMaxVal(m_numList); int unitHeight = 0; if(maxVal) { unitHeight = maxHeight/maxVal; } QFont ft; ft.setPixelSize(13); for(int i = 0; i < m_answerStrList.count(); ++i) { QString answerStr = m_answerStrList.at(i); int selectNum = m_numList.at(i); QString selectNumStr = QString::number(selectNum); int x = lineX + i*unitWidth + unitWidth/2 - curWidth/2; //绘制柱状图 int GraphHight = selectNum*unitHeight; QRect r = QRect(x, lineY - GraphHight, curWidth, GraphHight); QPainterPath path; path.addRect(r); painter.fillPath(path, QColor(185, 185, 185)); //绘制答案 QFontMetrics metrics(ft); int charWitdh = metrics.width(answerStr); QRect answerRect; /** 对比文字长度和柱状图的宽度,比柱状图宽的以文字的长度为基准会画,反之以 * 柱状图的宽度来画,保证显示全并居中,下面的也是一样 */ if(charWitdh < curWidth) { answerRect = QRect(x, lineY, curWidth, 30); } else { answerRect = QRect(x - (charWitdh - curWidth)/2, lineY, charWitdh, 30); } painter.drawText(answerRect, Qt::AlignCenter, answerStr); //绘制每个选项对应的值 QRect percentRect; charWitdh = metrics.width(selectNumStr); if(charWitdh < curWidth) { percentRect = QRect(x, lineY - GraphHight - 30, curWidth, 30); } else { percentRect = QRect(x - (charWitdh - curWidth)/2, lineY - GraphHight - 40, charWitdh, 30); } painter.drawText(percentRect, Qt::AlignCenter, selectNumStr); } } int MainWindow::getMaxVal(QList<int> list) { qSort(list.begin(), list.end()); return list.last(); }
最后
以上就是危机耳机最近收集整理的关于qt画柱状图的全部内容,更多相关qt画柱状图内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复