在构造函数外部访问时,ui会导致SIGSEGV

时间:2011-03-09 09:05:31

标签: qt qwidget

当我尝试从构造函数外部访问ui时,我得到一个SIGSEGV信号 错误是在cpp文件的底线引起的 在prepareQuestions函数的底部

我尝试将其减少到尽可能小的代码:

头:

#ifndef QUESTIONSDIALOG_H
#define QUESTIONSDIALOG_H

#include <QDialog>
#include <QHash>

#include "question.h"

namespace Ui {
    class QuestionsDialog;
}

class QuestionsDialog : public QDialog
{
    Q_OBJECT

public:
    explicit QuestionsDialog(QWidget *parent = 0);
    ~QuestionsDialog();
    void prepareQuestions(QString category);

private:
    Ui::QuestionsDialog *ui;
    QHash<QString, Question>* questions; //question id + question object
};

#endif // QUESTIONSDIALOG_H

cpp文件:

#include <QtDebug>
#include "questionsdialog.h"
#include "ui_questionsdialog.h"
#include "question.h"

QuestionsDialog::QuestionsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QuestionsDialog)
{
    ui->setupUi(this);
    ui->lbl_question1->setText(QString("this is the constructor")); //works
    ui->txt_input1->setText(QString("this is the constructor")); //works
}

QuestionsDialog::~QuestionsDialog()
{
    delete ui;
}

void QuestionsDialog::prepareQuestions(QString category){
    ui->lbl_question1->setText(QString("this is prepareQuestions method")); //doesn't work
//    ui->txt_input1->setText(QString("test1")); //creates an exception (no error message)
}

我在主

中构建
QStackedWidget *dialogStack = new QStackedWidget;
dialogStack->addWidget(new LoginDialog());     //index 0
dialogStack->addWidget(new CallDialog());      //index 1
dialogStack->addWidget(new CategoryDialog());  //index 2
dialogStack->addWidget(new QuestionsDialog()); //index 3

dialogStack->setCurrentIndex(0);

我将QuestionDialog称为最佳:

QStackedWidget* dialogStack = (QStackedWidget*)this->parentWidget();
QuestionsDialog* questionDialog = ((QuestionsDialog*)dialogStack->widget(1));
//set type of call + properties
dialogStack->setCurrentIndex(3);

questionDialog->prepareQuestions(((QToolButton*)obj)->statusTip());
错误行被注释时

gui(注意更改lbl_question1不起作用) http://i77.photobucket.com/albums/j74/bertyhell/gui.png

使用调试器时出错:

http://i77.photobucket.com/albums/j74/bertyhell/seg_fault.png

1 个答案:

答案 0 :(得分:1)

您使用错误的索引从对话框堆栈中获取QuestionDialog对象:

  

QuestionsDialog * questionDialog =   ((QuestionsDialog *)dialogStack-&GT;微件(1));

而不是“1”,你应该使用“3”。