劣势停止了,因为它引发了异常。异常停止在线程0中?

时间:2016-02-24 08:10:31

标签: c++ qt debugging

我有一个代码来选择一个文件,但是当对话框打开并且我没有选择任何文件时。整个程序崩溃了。  代码:

80 , 22-25,8080  ,   4444-5555
80
80-81
5555-6000,1111-2222  ,  80

我在标头void MainWindow::on_toolButton_Open_Advance_clicked() { xmlpath = QFileDialog::getOpenFileName(this,tr("Open File"),MainWindow::getWorkingDirectory()+"/0_Config",tr("XML files (*.xml *)")); ui->lineEdit_7->setText(xmlpath); } 中定义了xmlpath,并在构造函数中将其作为QString xmlpath

启动

错误:

xmlpath = "";

当我调试时:

enter image description here

编辑:根据this,我需要启动varibale,我做了。

EDIT2:



The program has unexpectedly finished.




2 个答案:

答案 0 :(得分:1)

方法' getOpenFileName()'如果用户未选择文件,则返回NULL。
因此,请检查' xmlpath'在将其传递给连续语句之前等于NULL。

答案 1 :(得分:1)

getOpenFileName()返回null字符串而不是NULL。这样做

qDebug()<< "this is before:"+xmlpath;
xmlpath = QFileDialog::getOpenFileName(this,tr("Open File"),MainWindow::getWorkingDirectory()+"/0_Config",tr("XML files (*.xml *)"));
qDebug()<<"this is after:"+xmlpath;
if (!xmlPath.isNull()) {
    ui->lineEdit_7->setText(xmlpath);
} else {
    qDebug() << "No file selected";
}