开发者

ui causes SIGSEGV when accessed outside the constructor

开发者 https://www.devze.com 2023-02-15 21:43 出处:网络
when i try to access the ui from outside the constructor i get a SIGSEGV signal the error is cause at the bottom line in cpp file

when i try to access the ui from outside the constructor i get a SIGSEGV signal the error is cause at the bottom line in cpp file at the bottom of the prepareQuestions function

i tried reducing it to the smallest possible code:

header:

#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 file:

#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)
}

where i construct in the main

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);

where i call the QuestionDialog to be on top:

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 when error line is commented (notice that the change lbl_question1 doesn't work)

ui causes SIGSEGV when accessed outside the constructor

error using the debugger:

ui causes SIGSEGV when accessed outside the constructor


You are using the wrong index to get the QuestionDialog object from the dialog stack:

QuestionsDialog* questionDialog = ((QuestionsDialog*)dialogStack->widget(1));

Instead of "1" you should use "3".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号