0 提前准备工作
- 修改窗口标题栏的图片
在xx.pro工程文件中增加 RC_ICONS = title.ico - 对高分屏的支持问题
int main(int argc, char *argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
1 QCustomplot基本配置
- 添加qcustomplot.cpp和qcustomplot.h
- 这时pro文件会自动添加上qcustomplot.cpp和qcustomplot.h,这时还需要添加printsupport。由于使用到打印相关,所以需要加入printsupport,在原有的widgets 后面加入即可
QT += widgets printsupport
- 打开界面文件,进入图形化设计界面,向主窗口中添加一个widget区域,对着所添加的widget区域点击右键,选择“提升为”按钮,提升类名称输入“QCustomPlot”,点击添加。在之后的界面中选中QCustomPlot,点击提升按钮,我们创建的widget就被提升为QCustomPlot类了。现在我们运行一下程序,就会出现一个简单的坐标系了。
- 在ui文件中,将添加的widget的objectName改为customPlot,进而在mainwindow的构造函数中即可添加静态曲线
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVector<double> x(101),y(101);
for(int i=0;i<101;i++){
x[i] = i/5 - 10;
y[i] = x[i] * x[i] * x[i];
}
ui->customPlot->addGraph();
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setData(x,y);
ui->customPlot->xAxis->setLabel("x");
ui->customPlot->yAxis->setLabel("y");
ui->customPlot->xAxis->setRange(-11,11);
ui->customPlot->yAxis->setRange(-1100,1100);
参考链接
2 动态曲线绘制
- 设置图层,曲线的属性
QStringList lineNames;
lineNames << "波形1" << "波形2" << "波形3" << "波形4" << "波形5" << "波形6" << "波形7" << "波形8" << "波形9" << "波形10"\
<< "波形11" << "波形12" << "波形13" << "波形14" << "波形15" << "波形16" << "波形17" << "波形18" << "波形19" << "波形20";
QColor initColor[20] = {QColor(0,146,152), QColor(162,0,124), QColor(241,175,0), QColor(27,79,147), QColor(229,70,70),\
QColor(0,140,94), QColor(178,0,31), QColor(91,189,43), QColor(0,219,219), QColor(172,172,172),\
QColor(0,178,191), QColor(197,124,172), QColor(243,194,70), QColor(115,136,193), QColor(245,168,154),\
QColor(152,208,185), QColor(223,70,41), QColor(175,215,136), QColor(157,255,255), QColor(0,0,0)};
for(int i=0; i<20; i++){
pCurve[i] = customPlot->addGraph();
pCurve[i]->setPen(QPen(QColor(initColor[i])));
pCurve[i]->setName(lineNames.at(i));
}
customPlot->setBackground(QColor(255,255,255));
customPlot->xAxis->setLabel("X");
customPlot->yAxis->setLabel("Y");
pointCountX = ui->txtPointCountX->text().toUInt();
pointCountY = ui->txtPointCountY->text().toUInt();
customPlot->xAxis->setRange(0,pointCountX);
customPlot->yAxis->setRange(pointCountY/2*-1,pointCountY/2);
customPlot->xAxis->ticker()->setTickCount(ui->txtMainScaleNumX->text().toUInt());
customPlot->yAxis->ticker()->setTickCount(ui->txtMainScaleNumY->text().toUInt());
customPlot->xAxis->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);
customPlot->yAxis->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);
- 定时器设定、connect()函数连接信号与槽函数;
timer = new QTimer(this);
timer->setInterval(10);
connect(timer,SIGNAL(timeout()),this,SLOT(TimeData_Update()));
- 槽函数中数据更新、曲线重新绘制;
cnt++;
for(int i=0; i<10; i++){
pCurve[i]->addData(cnt, num-i*10);
}
for(int i=10; i<20; i++){
pCurve[i]->addData(cnt, num+(i-9)*10);
}
pPlot1->replot(QCustomPlot::rpQueuedReplot);
3 多个y轴设置