c++ - 一段 qcustomplot 代码不起作用(小示例代码)

标签 c++ qt qcustomplot

我有一段 qt 代码不起作用(即一直显示空白窗口)

主窗口.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-创建一个小部件
2-将其提升为 QCustomPlot
2-将小部件用作 QCustomPlot 或创建一个 QCustomPlot 指针并将其设置为指向小部件:

QCustomPlot *customPlot = ui->widget;

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

然后像你一样继续。
* QCustomPlot 需要小部件在其上绘制。

关于c++ - 一段 qcustomplot 代码不起作用(小示例代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23244497/

相关文章:

c++ - 如何让 char[] 与 std::map 一起工作

c++ - 正在无限调用二叉搜索树的复制构造函数

android - 在 iOS 和 Android 之间共享代码的最佳方式

c++ - 已部署的 Mac 应用程序无法在 2014 年底之前的 Mac 上打开

c++ - 如何在我的代码中使用createWindow [Qt5.8]

c++ - 用于批量断点的 Visual Studio 插件

c++ - 具有 3 种可能值的图像

c++/qt - 没有这样的插槽 - 继承

c++ - QCPItemLine 正在自动绘制

c++ - ECG 风格的实时 QCustomPlot