一块qcustomplot代码没有工作(小例子代码)

时间:2014-04-23 12:37:08

标签: c++ qt qcustomplot

我有一段没有工作的qt代码(即保持显示空白窗口)

mainwindow.h:

#include <QMainWindow>
#include <QTimer>
#include "qcustomplot.h"
#include <qcustomplot.h>

class MainWindow : public QMainWindow
{
public:
    MainWindow(QWidget *parent = 0);
};

mainwindow.cpp :

#include "mainwindow.h"
#include <QDebug>
#include <QDesktopWidget>
#include <QScreen>
#include <QMessageBox>
#include <QMetaEnum>
#include <iostream>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)

{
    QCustomPlot *customPlot = new QCustomPlot;
    cout<<"srsly wtf!!!!"<<endl;
    // generate some data:
    QVector<double> x(101), y(101); // initialize with entries 0..100
    for (int i=0; i<101; ++i)
    {
    x[i] = i/50.0 - 1; // x goes from -1 to 1
    y[i] = x[i]*x[i]; // let's plot a quadratic function
    }
    // create graph and assign data to it:
    customPlot->addGraph();
    customPlot->graph(0)->setData(x, y);
    // give the axes some labels:
    customPlot->xAxis->setLabel("x");
    customPlot->yAxis->setLabel("y");
    // set axes ranges, so we see all data:
    customPlot->xAxis->setRange(-1, 1);
    customPlot->yAxis->setRange(0, 1);
    customPlot->replot();

}

main.cpp:

#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

,结果应该是这样的 http://www.qcustomplot.com/images/examples/quadraticdemo.png

我使用的QCustomPlot库可以在这里找到 http://www.qcustomplot.com/index.php/download

1 个答案:

答案 0 :(得分:1)

你必须:
1 - 创建一个小部件
2 - 将其提升为QCustomPlot类 2 - 将小部件用作QCustomPlot或创建QCustomPlot指针并将其设置为指向小部件:

QCustomPlot *customPlot = ui->widget;

ui->widget->addGraph(); .....

而不是像你那样继续。
* QCustomPlot需要小部件在其上绘制。