qwt示例该程序意外完成

时间:2014-02-08 02:40:46

标签: c++ qt qt-creator qwt

我的代码出错了,当我执行我的程序时(QWT的一个例子)我收到此错误(程序意外完成)为什么我收到此错误消息以及如何解决?

由于

这是我的代码:

    main.cpp
        #include "mainwindow.h"
        #include <QtGui>
        #include <QApplication>

        int main(int argc, char *argv[])
        {

        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        w.resize(400, 450);

        return a.exec();
        }
    mainwindow.cpp

      #include "mainwindow.h"


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

    {

    CreateGui();

    }

    MainWindow::~MainWindow()
    {

    }


    void MainWindow::CreateGui()
    {

        QwtPlot *myPlot = new QwtPlot(centralWidget());
            QwtPlotCurve *courbe = new QwtPlotCurve("Courbe 1");
            QLineEdit *test = new QLineEdit;

            QVector<double> x(5);
            QVector<double> y(5);

            // On entre des valeurs
            for(int i=0;i<5;i++)
            {
                x.append((double)i);
                y.append((double)(5-i));
            }
            courbe->setSamples(x.data(),y.data(),x.size());
            myPlot->replot();

            courbe->attach(myPlot);
            QGridLayout *layout = new QGridLayout;
            layout->addWidget(myPlot, 0, 1);
            layout->addWidget(test,1,0);
            centralWidget()->setLayout(layout);


        }

      and mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QObject>
#include <QMainWindow>
#include<QLineEdit>
#include<QGridLayout>

#include <qwt_plot.h>
#include <qwt_plot_curve.h>

class MainWindow: public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent=0);
    ~MainWindow();
private:
 private slots:
    void CreateGui();
};







#endif // MAINWINDOW_H

1 个答案:

答案 0 :(得分:-1)

主页窗口

中的

CreateGui不是一个插槽,也许它可以,机器人现在它不会被连接到任何连接(信号,插槽)

<击> MainWindow(QWidget * parent = 0);没有那么明确,我不知道你想用明确的说法。 :S (显然很好,多亏了@frank)

在mainwindow.cpp中

而不是centralWidget()你会添加一个this关键字,并尝试在MainWindow小部件上呈现,它继承自QWidget。

类似的东西:

在mainwindow.cpp上:

void MainWindow::CreateGui()
{

    QwtPlot *myPlot = new QwtPlot(this);
    QwtPlotCurve *courbe = new QwtPlotCurve("Courbe 1");
    QLineEdit *test = new QLineEdit;

    QVector<double> x(5);
    QVector<double> y(5);

    // On entre des valeurs
    for(int i=0;i<5;i++)
    {
        x.append((double)i);
        y.append((double)(5-i));
    }
    courbe->setSamples(x.data(),y.data(),x.size());
    myPlot->replot();

    courbe->attach(myPlot);
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(myPlot, 0, 1);
    layout->addWidget(test,1,0);
    this->setLayout(layout);


}



或者将centralWidget设置为某个东西,因为在任何地方都没有setCentralWidget(QWidget *)调用,因为@frank将其命名为。

该文档说如果以前没有设置centralWidget()将返回零。 并显示setCentralWidget(qwidget*)方法的链接。

我在构造函数中添加了@frank行。

this->setCentralWidget(new QWidget);

之后它也有效。我之前从未使用过这些方法,但显然最后一种是使用它的首选方式。

问候!

相关问题