【问题标题】:qwt example The program has unexpectedly finishedqwt 示例 程序意外结束
【发布时间】:2014-02-08 02:40:46
【问题描述】:

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

【问题讨论】:

    标签: c++ qt qt-creator qwt


    【解决方案1】:

    在 mainwindow.h 中

    CreateGui 不是一个插槽,也许它可以是,bot 目前它没有关联到任何连接(信号,插槽)

    MainWindow(QWidget *parent=0);没有那个明确的,我不知道你想说什么明确的。 :S (明确的很好,感谢@frank)

    在 mainwindow.cpp 中

    您可以添加 this 关键字而不是 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*) 调用,正如@frank 命名的那样。

    文档说centralWidget() 如果之前未设置,将返回零。 并向您显示setCentralWidget(qwidget*) 方法的链接。

    我在构造函数中添加了@frank 所说的那一行。

    this->setCentralWidget(new QWidget);
    

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

    您好!

    【讨论】:

    • 无论如何,'explicit' 和 'slot' 并不是你程序失败的原因。错误在 CreateGui() 中。 (其中(任何)方法名称的第一个 C 也应该是小写的)。但是其他两个关键字只是为了使一个非常简单的示例过于复杂。
    • explicit 风格不错,应该保留。 centralWidget() 也是首选(不破坏整个主窗口布局),尽管一开始的 setCentralwidget(new QWidget) 可能确实是导致崩溃的原因。在调试器中运行它会有所帮助。
    • @FrankOsterfeld,你是对的,但我确实在评论中指出,这两件事不是导致 seg 错误,而是 centralWidget() 方法。我从未使用过centralWidget,但现在我查看了文档link,确实有一个setCentralWidget(),当你提到设置时,centralWidget() 返回一个有效的指针。如果你使用centralWidget(),它应该总是设置吗?我添加了行“this->setCentralWidget(new QWidget);”在构造函数上,然后工作。
    • 谢谢 Frank 和 rccursach,我的代码现在可以工作了,但是当我尝试添加任何按钮时,按钮仍然在左上角,我添加了像这样的按钮 QGridLayout *layout = new QGridLayout;布局->addWidget(myPlot, 0, 1);布局->addWidget(test,1,0); QPushButton *StartButton= new QPushButton("开始",this); QHBoxLayout *layout1 =new QHBoxLayout;布局1->addWidget(StartButton); QVBoxLayout *layoutprincipale=new QVBoxLayout; layoutprincipale->addLayout(layout); layoutprincipale->addLayout(layout1);
    • 如果我没记错的话,那是因为你这样做了:"new QPushButton("Start",this)",这是你的 MainWindow,但你的绘图和布局在 setCentralWidget 上设置的另一个 QWidget 上(),所以你也应该做"new QPushButton("Start",centralWidget())",所以centralWidget的指针QWidget是你添加的按钮的父级,就像布局一样。祝你好运!
    猜你喜欢
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    相关资源
    最近更新 更多