我遇到了读取访问冲突的问题。
我想做的是以下内容:
我有两种形式:mainwindow.ui和dialog.ui
在 dialog.h 我编码
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
double getValue();
QString getName();
在 dialog.cpp 中
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;
}
另一方面,在 mainwindow.h
private:
Ui::MainWindow *ui;
Dialog* form_dialog;
在 mainwindow.cpp 中,我尝试
if(form_dialog->getValue() > 0)
{
double value = form_dialog->getValue();
}
我希望以下
我需要执行代码,但如果我不打开此对话框并设置任何值,我会收到此错误。
异常读取访问冲突
答案 0 :(得分:1)
这会阻止您的代码崩溃,但您必须找出form_dialog为NULL的原因。
if (form_dialog != NULL) {
if(form_dialog->getValue() > 0)
{
double value = form_dialog->getValue();
}
}