void keyevent::qst(){
QCustomPlot *pCustomPlot = new QCustomPlot(this);
//pCustomPlot->resize(300, 300);
pCustomPlot->setGeometry(200,200,300,300
//ui.horizontalLayout->addWidget(pCustomPlot);
// 可变数组存放绘图的坐标的数据,分别存放x和y坐标的数据,101为数据长度
QVector<double> x(101), y(101);
// 添加数据,这里演示y = x^3,为了正负对称,x从-10到+10
for (int i = 0; i < 101; ++i)
{
if (i < 30)
{
x[i] = i / 5 - 10;
y[i] = qPow(x[i], 3); // x的y次方;
}
else {
x[i] = i + 3;
y[i] = i * 8;
}
}
// 向绘图区域QCustomPlot添加一条曲线
QCPGraph *pGraph = pCustomPlot->addGraph();
// 添加数据
pCustomPlot->graph(0)->setData(x, y);
// 设置坐标轴名称
pCustomPlot->xAxis->setLabel("x");
pCustomPlot->yAxis->setLabel("y");
// 设置背景色
pCustomPlot->setBackground(QColor(50, 50, 50));
pGraph->setPen(QPen(QColor(32, 178, 170)));
// 设置x/y轴文本色、轴线色、字体等
pCustomPlot->xAxis->setTickLabelColor(Qt::blue);
pCustomPlot->xAxis->setLabelColor(QColor(0, 160, 230));
pCustomPlot->xAxis->setBasePen(QPen(QColor(32, 178, 170)));
pCustomPlot->xAxis->setTickPen(QPen(QColor(128, 0, 255)));
pCustomPlot->xAxis->setSubTickPen(QColor(255, 165, 0));
QFont xFont = pCustomPlot->xAxis->labelFont();
xFont.setPixelSize(20);
pCustomPlot->xAxis->setLabelFont(xFont);
pCustomPlot->yAxis->setTickLabelColor(Qt::white);
pCustomPlot->yAxis->setLabelColor(QColor(0, 160, 230));
pCustomPlot->yAxis->setBasePen(QPen(QColor(32, 178, 170)));
pCustomPlot->yAxis->setTickPen(QPen(QColor(128, 0, 255)));
pCustomPlot->yAxis->setSubTickPen(QColor(255, 165, 0));
QFont yFont = pCustomPlot->yAxis->labelFont();
yFont.setPixelSize(20);
pCustomPlot->yAxis->setLabelFont(yFont);
// 设置坐标轴显示范围,否则只能看到默认范围
pCustomPlot->xAxis->setRange(0, 30);
pCustomPlot->yAxis->setRange(-1100, 1100);
}
原文地址:http://blog.51cto.com/12892173/2124659