2011-02-06 22 views
2

我只是用Qt弄湿我的脚,我试图从QlineEdit中拉出字符串,并在单击按钮后将它附加到QTextBrowser(为了简单/错误检查,我我只是把它附加在此刻的单词)。在QT中设置新的公共信号后出现Seg故障

该程序运行,并在屏幕上显示GUI,但每当我点击按钮,我的程序段故障。

这里是我的代码,我砍掉了很多了,这是不必要的:

部首:

#ifndef TCD2_GUI_H 
#define TCD2_GUI_H 
//bunch of includes 

class TCD2_GUI : public QWidget 
{ 
    Q_OBJECT 

public: 
    TCD2_GUI(QWidget *window = 0); 
    //bunch of other members 
    QLineEdit *a1_1; 
    QTextBrowser *stdoutput; 

public slots: 
    void applySettings(void); 

private: 

}; 
#endif // TCD2_GUI_H 

,这里是其中的导致故障的cpp的代码片段

QTextBrowser *stdoutput = new QTextBrowser(); 

    stdoutput->append("Welcome!"); 

    QObject::connect(apply, SIGNAL(clicked()), this, SLOT(applySettings())); 

    //------------------------------------------------------Standard Output END 
    //layout things 

} 

void TCD2_GUI::applySettings() 
{ 
    stdoutput->append("appended"); 
} 

回答

3

stdoutputapplySettings()功能指的是TCD2_GUI类的成员,而在你的代码在崩溃发生是一个局部变量stdoutput。 尝试通过例如在构造函数中添加:

stdoutput = new QTextBrowser(); 

andremove以下行从您的代码:

QTextBrowser stdoutput = new QTextBrowser(); 
+0

谢谢你,也对。 – 2011-02-07 01:38:28

1

看着提供的代码,我的猜测将是stdoutput被宣告两次。一次作为* TCD2_GUI *类的成员,第二次作为布局的方法(类构造函数?)中的局部变量。 ApplySettings使用未初始化的类成员,因此分段错误。

你的代码更改为:

stdoutput = new QTextBrowser(); 
stdoutput->append("Welcome!"); 
QObject::connect(apply, SIGNAL(clicked()), this, SLOT(applySettings())); 

可能会解决问题。

希望这会有所帮助,至于

+0

非常感谢。是的,我已经将所有变量声明了两次。 – 2011-02-07 01:37:32