mainwindow.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "qwdialogpen.h"
  4. #include <QRandomGenerator>
  5. // 若MSVC 编译版本错误,修改 msvc-version.conf 文件
  6. // 解决MSVC编译时,界面汉字乱码的问题
  7. #if _MSC_VER >= 1600 //MSVC2015>1899, MSVC_VER= 14.0
  8. #pragma execution_character_set("utf-8")
  9. #endif
  10. void MainWindow::createChart()
  11. {//创建图表的各个部件
  12. QChart *chart = new QChart();
  13. chart->setTitle(tr("简单函数曲线"));
  14. // chart->setAcceptHoverEvents(true);
  15. ui->chartView->setChart(chart);
  16. ui->chartView->setRenderHint(QPainter::Antialiasing);
  17. QLineSeries *series0 = new QLineSeries();
  18. QLineSeries *series1 = new QLineSeries();
  19. series0->setName("Sin曲线");
  20. series1->setName("Cos曲线");
  21. curSeries=series0; //当前序列
  22. QPen pen;
  23. pen.setStyle(Qt::DotLine);//Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine
  24. pen.setWidth(2);
  25. pen.setColor(Qt::red);
  26. series0->setPen(pen); //折线序列的线条设置
  27. pen.setStyle(Qt::SolidLine);//Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine
  28. pen.setColor(Qt::blue);
  29. series1->setPen(pen);//折线序列的线条设置
  30. chart->addSeries(series0);
  31. chart->addSeries(series1);
  32. QValueAxis *axisX = new QValueAxis;
  33. curAxis=axisX; //当前坐标轴
  34. axisX->setRange(0, 10); //设置坐标轴范围
  35. axisX->setLabelFormat("%.1f"); //标签格式
  36. axisX->setTickCount(11); //主分隔个数
  37. axisX->setMinorTickCount(4);
  38. axisX->setTitleText("time(secs)"); //标题
  39. // axisX->setGridLineVisible(false);
  40. QValueAxis *axisY = new QValueAxis;
  41. axisY->setRange(-2, 2);
  42. axisY->setTitleText("value");
  43. axisY->setTickCount(5);
  44. axisY->setLabelFormat("%.2f"); //标签格式
  45. // axisY->setGridLineVisible(false);
  46. axisY->setMinorTickCount(4);
  47. // 下面4行语句在Qt5.12.1里编译时提示过时(deprecated)了,应使用另外一种方法添加坐标轴
  48. // chart->setAxisX(axisX, series0); //添加X坐标轴
  49. // chart->setAxisX(axisX, series1); //添加X坐标轴
  50. // chart->setAxisY(axisY, series0); //添加Y坐标轴
  51. // chart->setAxisY(axisY, series1); //添加Y坐标轴
  52. //另一种实现设置坐标轴的方法,在Qt 5.12.1里编译时不会提示过时
  53. chart->addAxis(axisX,Qt::AlignBottom); //坐标轴添加到图表,并指定方向
  54. chart->addAxis(axisY,Qt::AlignLeft);
  55. series0->attachAxis(axisX); //序列 series0 附加坐标轴
  56. series0->attachAxis(axisY);
  57. series1->attachAxis(axisX);//序列 series1 附加坐标轴
  58. series1->attachAxis(axisY);
  59. foreach (QLegendMarker* marker, chart->legend()->markers()) {
  60. QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(on_LegendMarkerClicked()));
  61. QObject::connect(marker, SIGNAL(clicked()), this, SLOT(on_LegendMarkerClicked()));
  62. }
  63. }
  64. void MainWindow::prepareData()
  65. {//为序列生成数据
  66. QLineSeries *series0=(QLineSeries *)ui->chartView->chart()->series().at(0);
  67. QLineSeries *series1=(QLineSeries *)ui->chartView->chart()->series().at(1);
  68. series0->clear(); //清除数据
  69. series1->clear();
  70. // qsrand(QTime::currentTime().second());//随机数初始化
  71. qreal t=0,y1,y2,intv=0.1;
  72. qreal rd;
  73. int cnt=100;
  74. for(int i=0;i<cnt;i++)
  75. {
  76. // rd=(qrand() % 10)-5; //随机数,-5~+5
  77. rd= QRandomGenerator::global()->bounded(-5,6); //
  78. y1=qSin(t)+rd/50;
  79. *series0<<QPointF(t,y1); //序列添加数据点
  80. // series0->append(t,y1); //序列添加数据点
  81. // rd=(qrand() % 10)-5; //随机数,-5~+5
  82. rd= QRandomGenerator::global()->bounded(-5,6); //
  83. y2=qCos(t)+rd/50;
  84. // series1->append(t,y2); //序列添加数据点
  85. *series1<<QPointF(t,y2); //序列添加数据点
  86. t+=intv;
  87. }
  88. }
  89. void MainWindow::updateFromChart()
  90. { //从图表上获取数据更新界面显示
  91. QChart *aChart;
  92. aChart=ui->chartView->chart(); //获取chart
  93. ui->editTitle->setText(aChart->title()); //图表标题
  94. QMargins mg=aChart->margins(); //边距
  95. ui->spinMarginLeft->setValue(mg.left());
  96. ui->spinMarginRight->setValue(mg.right());
  97. ui->spinMarginTop->setValue(mg.top());
  98. ui->spinMarginBottom->setValue(mg.bottom());
  99. }
  100. MainWindow::MainWindow(QWidget *parent) :
  101. QMainWindow(parent),
  102. ui(new Ui::MainWindow)
  103. {
  104. ui->setupUi(this);
  105. createChart();//创建图表
  106. prepareData();//生成数据
  107. updateFromChart(); //从图表获得属性值,刷新界面显示
  108. }
  109. MainWindow::~MainWindow()
  110. {
  111. delete ui;
  112. }
  113. void MainWindow::on_LegendMarkerClicked()
  114. {
  115. QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
  116. switch (marker->type())
  117. {
  118. case QLegendMarker::LegendMarkerTypeXY:
  119. {
  120. marker->series()->setVisible(!marker->series()->isVisible());
  121. marker->setVisible(true);
  122. qreal alpha = 1.0;
  123. if (!marker->series()->isVisible())
  124. alpha = 0.5;
  125. QColor color;
  126. QBrush brush = marker->labelBrush();
  127. color = brush.color();
  128. color.setAlphaF(alpha);
  129. brush.setColor(color);
  130. marker->setLabelBrush(brush);
  131. brush = marker->brush();
  132. color = brush.color();
  133. color.setAlphaF(alpha);
  134. brush.setColor(color);
  135. marker->setBrush(brush);
  136. QPen pen = marker->pen();
  137. color = pen.color();
  138. color.setAlphaF(alpha);
  139. pen.setColor(color);
  140. marker->setPen(pen);
  141. break;
  142. }
  143. default:
  144. break;
  145. }
  146. }
  147. void MainWindow::on_actDraw_triggered()
  148. { //重新生成数据
  149. prepareData();//重新生成数据
  150. }
  151. void MainWindow::on_btnSetTitle_clicked()
  152. { //设置图标标题文字
  153. QString str=ui->editTitle->text();
  154. ui->chartView->chart()->setTitle(str);
  155. }
  156. void MainWindow::on_btnSetTitleFont_clicked()
  157. { //设置图标标题文字的字体
  158. QFont font=ui->chartView->chart()->titleFont();
  159. bool ok=false;
  160. font=QFontDialog::getFont(&ok,font);
  161. if (ok)
  162. ui->chartView->chart()->setTitleFont(font);
  163. }
  164. void MainWindow::on_btnSetMargin_clicked()
  165. {//设置图标的4个边距
  166. QMargins mgs;
  167. mgs.setLeft(ui->spinMarginLeft->value());
  168. mgs.setRight(ui->spinMarginRight->value());
  169. mgs.setTop(ui->spinMarginTop->value());
  170. mgs.setBottom(ui->spinMarginBottom->value());
  171. ui->chartView->chart()->setMargins(mgs);
  172. }
  173. void MainWindow::on_chkPointVisible_clicked(bool checked)
  174. {//序列的数据点是否可见
  175. curSeries->setPointsVisible(checked);
  176. }
  177. void MainWindow::on_chkPointLabelVisible_clicked(bool checked)
  178. { //序列的数据点标签是否可见
  179. curSeries->setPointLabelsVisible(checked);
  180. }
  181. void MainWindow::on_btnSeriesName_clicked()
  182. { //设置序列名称
  183. curSeries->setName(ui->editSeriesName->text());
  184. if (ui->radioSeries0->isChecked())
  185. ui->radioSeries0->setText(ui->editSeriesName->text());
  186. else
  187. ui->radioSeries1->setText(ui->editSeriesName->text());
  188. }
  189. void MainWindow::on_btnSeriesColor_clicked()
  190. { //序列的曲线颜色
  191. QColor color=curSeries->color();
  192. color=QColorDialog::getColor(color);
  193. if (color.isValid())
  194. curSeries->setColor(color);
  195. }
  196. void MainWindow::on_chkLegendVisible_clicked(bool checked)
  197. {//图例是否可见
  198. ui->chartView->chart()->legend()->setVisible(checked);
  199. }
  200. void MainWindow::on_btnSetAxisRange_clicked()
  201. { //设置坐标轴的坐标范围
  202. curAxis->setRange(ui->spinAxisMin->value(),ui->spinAxisMax->value());
  203. }
  204. void MainWindow::on_spinTickCount_valueChanged(int arg1)
  205. {
  206. curAxis->setTickCount(arg1);
  207. }
  208. void MainWindow::on_spinMinorTickCount_valueChanged(int arg1)
  209. {
  210. curAxis->setMinorTickCount(arg1);
  211. }
  212. void MainWindow::on_radioX_clicked()
  213. { //获取当前坐标轴
  214. // 在Qt 5.12.1中编译时提示 QChart::axisX()和QChart::axisY()函数过时,应使用 QChart::axes()函数
  215. // if (ui->radioX->isChecked())
  216. // curAxis=(QValueAxis*)ui->chartView->chart()->axisX();
  217. // else
  218. // curAxis=(QValueAxis*)ui->chartView->chart()->axisY();
  219. // 下面是针对Qt 5.12.1修改的代码
  220. QList<QAbstractAxis*> axes;
  221. if (ui->radioX->isChecked())
  222. axes=ui->chartView->chart()->axes(Qt::Horizontal);
  223. else
  224. axes=ui->chartView->chart()->axes(Qt::Vertical);
  225. curAxis=(QValueAxis*)axes[0];
  226. //获取坐标轴的各种属性,显示到界面上
  227. ui->spinAxisMin->setValue(curAxis->min());
  228. ui->spinAxisMax->setValue(curAxis->max());
  229. ui->editAxisTitle->setText(curAxis->titleText());
  230. ui->chkBoxAxisTitle->setChecked(curAxis->isTitleVisible());
  231. ui->editAxisLabelFormat->setText(curAxis->labelFormat());
  232. ui->chkBoxLabelsVisible->setChecked(curAxis->labelsVisible());
  233. ui->chkGridLineVisible->setChecked(curAxis->isGridLineVisible());
  234. ui->chkAxisLineVisible->setChecked(curAxis->isLineVisible());
  235. ui->spinTickCount->setValue(curAxis->tickCount());
  236. ui->chkAxisLineVisible->setChecked(curAxis->isLineVisible());
  237. ui->spinMinorTickCount->setValue(curAxis->minorTickCount());
  238. ui->chkMinorTickVisible->setChecked(curAxis->isMinorGridLineVisible());
  239. }
  240. void MainWindow::on_radioY_clicked()
  241. {
  242. on_radioX_clicked();
  243. }
  244. void MainWindow::on_chkGridLineVisible_clicked(bool checked)
  245. { //轴的网格线是否可见
  246. curAxis->setGridLineVisible(checked);
  247. }
  248. void MainWindow::on_chkMinorTickVisible_clicked(bool checked)
  249. { //次级刻度是否可见
  250. curAxis->setMinorGridLineVisible(checked);
  251. }
  252. void MainWindow::on_chkBoxLegendBackground_clicked(bool checked)
  253. {//图例的背景是否可见
  254. ui->chartView->chart()->legend()->setBackgroundVisible(checked);
  255. }
  256. void MainWindow::on_radioButton_clicked()
  257. {//图例的位置
  258. ui->chartView->chart()->legend()->setAlignment(Qt::AlignTop);
  259. }
  260. void MainWindow::on_radioButton_2_clicked()
  261. {//图例的位置
  262. ui->chartView->chart()->legend()->setAlignment(Qt::AlignBottom);
  263. }
  264. void MainWindow::on_radioButton_3_clicked()
  265. {//图例的位置
  266. ui->chartView->chart()->legend()->setAlignment(Qt::AlignLeft);
  267. }
  268. void MainWindow::on_radioButton_4_clicked()
  269. {//图例的位置
  270. ui->chartView->chart()->legend()->setAlignment(Qt::AlignRight);
  271. }
  272. void MainWindow::on_btnLegendFont_clicked()
  273. { //图例的字体设置
  274. QFont font=ui->chartView->chart()->legend()->font();
  275. bool ok=false;
  276. font=QFontDialog::getFont(&ok,font);
  277. if (ok)
  278. ui->chartView->chart()->legend()->setFont(font);
  279. }
  280. void MainWindow::on_btnLegendlabelColor_clicked()
  281. {//图例的文字颜色设置
  282. QColor color=ui->chartView->chart()->legend()->labelColor();
  283. color=QColorDialog::getColor(color);
  284. if (color.isValid())
  285. ui->chartView->chart()->legend()->setLabelColor(color);
  286. }
  287. void MainWindow::on_chkBoxVisible_clicked(bool checked)
  288. { //坐标轴是否可见
  289. curAxis->setVisible(checked);
  290. }
  291. void MainWindow::on_btnAxisSetTitle_clicked()
  292. { //设置坐标轴的标题
  293. curAxis->setTitleText(ui->editAxisTitle->text());
  294. }
  295. void MainWindow::on_btnAxisSetTitleFont_clicked()
  296. { //设置坐标轴的标题的字体
  297. QFont font=curAxis->titleFont();
  298. bool ok=false;
  299. font=QFontDialog::getFont(&ok,font);
  300. if (ok)
  301. curAxis->setTitleFont(font);
  302. }
  303. void MainWindow::on_chkBoxAxisTitle_clicked(bool checked)
  304. { //轴标题是否可见
  305. curAxis->setTitleVisible(checked);
  306. }
  307. void MainWindow::on_pushButton_clicked()
  308. {//设置坐标轴刻度标签的文字格式
  309. curAxis->setLabelFormat(ui->editAxisLabelFormat->text());
  310. }
  311. void MainWindow::on_btnAxisLabelColor_clicked()
  312. {//设置坐标轴刻度标签的文字颜色
  313. QColor color=curAxis->labelsColor();
  314. color=QColorDialog::getColor(color);
  315. if (color.isValid())
  316. curAxis->setLabelsColor(color);
  317. }
  318. void MainWindow::on_btnAxisLabelFont_clicked()
  319. {//设置坐标轴刻度标签的文字字体
  320. QFont font=curAxis->labelsFont();
  321. bool ok=false;
  322. font=QFontDialog::getFont(&ok,font);
  323. if (ok)
  324. curAxis->setLabelsFont(font);
  325. }
  326. void MainWindow::on_chkBoxLabelsVisible_clicked(bool checked)
  327. {//轴的刻度标签是否可见
  328. curAxis->setLabelsVisible(checked);
  329. }
  330. void MainWindow::on_btnGridLineColor_clicked()
  331. { //网格线的颜色设置
  332. QColor color=curAxis->gridLineColor();
  333. color=QColorDialog::getColor(color);
  334. if (color.isValid())
  335. curAxis->setGridLineColor(color);
  336. }
  337. void MainWindow::on_pushButton_10_clicked()
  338. { //网格线的Pen设置
  339. QPen pen;
  340. pen=curAxis->gridLinePen();
  341. bool ok=false;
  342. pen=QWDialogPen::getPen(pen,ok);
  343. if (ok)
  344. curAxis->setGridLinePen(pen);
  345. }
  346. void MainWindow::on_chkAxisLineVisible_clicked(bool checked)
  347. {//刻度是否可见
  348. curAxis->setLineVisible(checked);
  349. }
  350. void MainWindow::on_btnAxisLinePen_clicked()
  351. {
  352. QPen pen;
  353. pen=curAxis->linePen();
  354. bool ok=false;
  355. pen=QWDialogPen::getPen(pen,ok);
  356. if (ok)
  357. curAxis->setLinePen(pen);
  358. }
  359. void MainWindow::on_btnAxisLinePenColor_clicked()
  360. {
  361. QColor color=curAxis->linePenColor();
  362. color=QColorDialog::getColor(color);
  363. if (color.isValid())
  364. curAxis->setLinePenColor(color);
  365. }
  366. void MainWindow::on_btnMinorColor_clicked()
  367. {//次级刻度网格线颜色
  368. QColor color=curAxis->minorGridLineColor();
  369. color=QColorDialog::getColor(color);
  370. if (color.isValid())
  371. curAxis->setMinorGridLineColor(color);
  372. }
  373. void MainWindow::on_btnMinorPen_clicked()
  374. {//次级刻度线Pen设置
  375. QPen pen;
  376. pen=curAxis->minorGridLinePen();
  377. bool ok=false;
  378. pen=QWDialogPen::getPen(pen,ok);
  379. if (ok)
  380. curAxis->setMinorGridLinePen(pen);
  381. }
  382. void MainWindow::on_radioSeries0_clicked()
  383. {//获取当前数据序列
  384. if (ui->radioSeries0->isChecked())
  385. curSeries=(QLineSeries *)ui->chartView->chart()->series().at(0);
  386. else
  387. curSeries=(QLineSeries *)ui->chartView->chart()->series().at(1);
  388. //获取序列的属性值,并显示到界面上
  389. ui->editSeriesName->setText(curSeries->name());
  390. ui->chkSeriesVisible->setChecked(curSeries->isVisible());
  391. ui->chkPointVisible->setChecked(curSeries->pointsVisible());
  392. ui->sliderSeriesOpacity->setValue(curSeries->opacity()*10);
  393. ui->chkPointLabelVisible->setChecked(curSeries->pointLabelsVisible());
  394. }
  395. void MainWindow::on_radioSeries1_clicked()
  396. {
  397. on_radioSeries0_clicked();
  398. }
  399. void MainWindow::on_chkSeriesVisible_clicked(bool checked)
  400. {//序列是否可见
  401. this->curSeries->setVisible(checked);
  402. }
  403. void MainWindow::on_btnSeriesPen_clicked()
  404. {//序列线条的Pen设置
  405. bool ok=false;
  406. QPen pen;
  407. pen=curSeries->pen();
  408. pen=QWDialogPen::getPen(pen,ok);
  409. if (ok)
  410. curSeries->setPen(pen);
  411. }
  412. void MainWindow::on_sliderSeriesOpacity_valueChanged(int value)
  413. {//序列的透明度
  414. curSeries->setOpacity(value/10.0);
  415. }
  416. void MainWindow::on_btnSeriesLabColor_clicked()
  417. {//序列数据点标签颜色
  418. QColor color=curSeries->pointLabelsColor();
  419. color=QColorDialog::getColor(color);
  420. if (color.isValid())
  421. curSeries->setPointLabelsColor(color);
  422. }
  423. void MainWindow::on_btnSeriesLabFont_clicked()
  424. {//序列数据点标签字体
  425. QFont font;
  426. font=curSeries->pointLabelsFont();
  427. bool ok=false;
  428. font=QFontDialog::getFont(&ok,font);
  429. if (ok)
  430. curSeries->setPointLabelsFont(font);
  431. }
  432. void MainWindow::on_radioSeriesLabFormat0_clicked()
  433. { //序列数据点标签的显示格式
  434. curSeries->setPointLabelsFormat("@yPoint");
  435. }
  436. void MainWindow::on_radioSeriesLabFormat1_clicked()
  437. {//序列数据点标签的显示格式
  438. curSeries->setPointLabelsFormat("(@xPoint,@yPoint)");
  439. }
  440. void MainWindow::on_cBoxAnimation_currentIndexChanged(int index)
  441. {//动画效果
  442. ui->chartView->chart()->setAnimationOptions(QChart::AnimationOptions(index));
  443. // ui->chartView->chart()->setAnimationOptions(QChart::SeriesAnimations);
  444. // ui->chartView->chart()->setAnimationOptions(QChart::AllAnimations);
  445. }
  446. void MainWindow::on_cBoxTheme_currentIndexChanged(int index)
  447. { //图标的主题
  448. ui->chartView->chart()->setTheme(QChart::ChartTheme(index));
  449. }
  450. void MainWindow::on_actZoomIn_triggered()
  451. {//放大
  452. ui->chartView->chart()->zoom(1.2);//zoomIn();
  453. }
  454. void MainWindow::on_actZoomOut_triggered()
  455. {//缩小
  456. ui->chartView->chart()->zoom(0.8);//zoomOut();
  457. }
  458. void MainWindow::on_actZoomReset_triggered()
  459. {//复位
  460. ui->chartView->chart()->zoomReset();
  461. }