Iván
Iván

Reputation: 67

Qt c++ Read access violation when I try to read Empty data of other dialog.ui

I have a problem of read access violation.

What I want to do is the following:

I have two forms : mainwindow.ui and dialog.ui

In dialog.h I code this

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

    double getValue();

    QString getName();

In dialog.cpp this

double Dialog::getValue()
{
    double result = 0.0;
    if(this->ui->lE_value->text().isEmpty())
    {
        result = 0.0;
    }
    else
    {
        QString set_value    = this->ui->lE_value->text();
        result = set_value.toDouble();
    }

    return result;
}

QString Dialog::getName()
{
    QString def_name = "def_name";
    if(this->ui->lE_name->text().isEmpty())
    {
        def_name = "def_name";
    }
    else
    {
        QString set_name    = this->ui->lE_name->text();
        def_name = set_name;
    }

    return def_name;
}

On the other hand in mainwindow.h

private:
    Ui::MainWindow *ui;

    Dialog* form_dialog;

And in mainwindow.cpp I try

if(form_dialog->getValue() > 0)
{
    double value = form_dialog->getValue();
}

I expect the following

I need to follow with execution of code but if I don't open this dialog and set any values i get this error.

Exception read access violation

Upvotes: 0

Views: 1023

Answers (1)

Marco
Marco

Reputation: 2020

This will prevent your code from crashing but the you have to find out why form_dialog is NULL.

if (form_dialog != NULL) {
    if(form_dialog->getValue() > 0)
    {
        double value = form_dialog->getValue();
    }
}

Upvotes: 1

Related Questions