Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ qwt示例程序意外完成_C++_Qt_Qt Creator_Qwt - Fatal编程技术网

C++ qwt示例程序意外完成

C++ qwt示例程序意外完成,c++,qt,qt-creator,qwt,C++,Qt,Qt Creator,Qwt,我的代码出了问题,当我执行我的程序时,一个QWT示例我得到了这个错误程序意外地完成了为什么我得到了这个错误消息,我如何修复它 谢谢 这是我的代码: main.cpp #include "mainwindow.h" #include <QtGui> #include <QApplication> int main(int argc, char *argv[]) { Q

我的代码出了问题,当我执行我的程序时,一个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
在主窗口中

CreateGui不是一个插槽,也许它可以,但现在它与任何connectsignal、插槽都没有关联

MainWindowQWidget*父项=0;我不知道你想说什么s 由于@frank,显式很好

在mainwindow.cpp中

代替centralWidget,您将添加一个this关键字,并尝试在MainWindow小部件上呈现,wich继承自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);


}
在那之后,它也起作用了。我以前从未使用过这些方法,但显然最后一种方法是使用它的首选方法


你好

无论如何,“显式”和“插槽”并不是程序失败的原因。错误在CreateGui中。其中,任何方法名称的第一个C也应该是小写的。但是其他两个关键字只是让一个非常简单的例子过于复杂。显式是很好的风格,应该保留。centralWidget也比这更可取,因为它不会破坏整个主窗口布局,尽管一开始的setCentralwidgetnew QWidget可能确实是导致崩溃的原因。在调试器中运行它会有所帮助。@Frankosterfield,你是对的,但我在评论中指出这两件事并没有导致seg故障,但是centralWidget方法。我从未使用过centralWidget,但现在我已经查看了文档,确实有一个setCentralWidget,正如您所提到的,当设置时,centralWidget会返回一个有效指针。如果使用centralWidget,则应始终设置它?我添加了一行this->setCentralWidgetNewQWidget;谢谢Frank和rccursach,我的代码现在可以工作了,但当我尝试添加任何按钮时,按钮仍保留在左上角,我添加了如下按钮QGridLayout*layout=new QGridLayout;布局->addWidgetmyPlot,0,1;布局->addWidgettest,1,0;QPushButton*StartButton=新的QPushButton开始,此;QHBoxLayout*layout1=新的QHBoxLayout;布局1->addWidgetStartButton;QVBoxLayout*layoutprincipale=新的QVBoxLayout;布局原则->添加布局布局;布局原则->添加布局1;如果我没有错,那是因为你做了:新建QPushButtonStart,这是你的主窗口,但是你的绘图和布局是在另一个QWidget上设置在setCentralWidget上的,所以你也应该新建QPushButtonStart,centralWidget,所以centralWidget的指针QWidget是你添加的按钮的父项,就像布局一样。祝你好运
this->setCentralWidget(new QWidget);