我有一个选择文件的代码,但是当对话框打开时,如果我没有选择任何文件,则可以。整个程序崩溃。
码:
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 = "";
错误:
The program has unexpectedly finished.
当我调试时:
编辑:根据this,我需要启动变量,这是我做的。
编辑2:
void MainWindow::on_toolButton_Open_Advance_clicked()
{
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;
ui->lineEdit_7->setText(xmlpath);
}
最佳答案
getOpenFileName()
返回空字符串,而不是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";
}