QWT示例该程序已经意外完成

qwt example The program has unexpectedly finished

本文关键字:意外 程序 QWT      更新时间:2023-10-16

我的代码没有什么问题,当我执行程序(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

在mainwindow.h

creategui不是一个插槽,也许可以是,bot暂时还没有与任何连接(信号,插槽)

相关

<罢工>mainwindow(qwidget *parent = 0);没有那个明确的话,我不知道您想明确说什么。:S (明确是可以的,多亏了@frank)

在mainwindow.cpp

而不是centralwidget()您将添加此关键字,并尝试在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*)在任何地方都调用,kerfrank名称。

DOC说,如果CentralWidget()在未设置之前将返回零。并向您显示指向SetCentralWidget(Qwidget*)方法的链接。

我添加了@frank在构造函数上说的线。

this->setCentralWidget(new QWidget);

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

问候!