我必须通过使用Windows资源管理器选择文件来加载XML文件中的数据。如果我选择的是另一种格式,那么除了XML之外,catch块应该得到异常。但它没有,应用程序失败。这是我到目前为止编写的代码。我错过了什么?
我在xml方法的readVehicles(QString fileName)
中抛出了execption。
QList<Vehicle> VehicleHelper::readVehicles(QString fileName){
QList<Vehicle> vehicles;
Vehicle newVehicle;
QFile file(fileName);
QDomDocument document;
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
throw FileCannotBeOpenException();
}
else{
if(!document.setContent(&file)){
throw InvalidXMLContentException();
}
file.close();
}
QDomElement root = document.firstChildElement();
QDomNodeList carElements = root.elementsByTagName("Car");
for(int i = 0; i < carElements.size(); i++){
QDomNode carNode = carElements.at(i);
QDomElement carElement = carNode.toElement();
QString carID = carElement.attribute("ID");
//if the idNumber is null, generate one
if(carID.isEmpty()){
QUuid newId = Vehicle::generateID();
newVehicle.setVehicleId(newId);
}
else {
QUuid id;
try{
id = QUuid::QUuid(carID);
} catch(QException &ex){
continue;
}
newVehicle.setVehicleId(id);
}
//Owner
//****************************************************************
QDomElement ownerElement = carNode.firstChildElement("Owner");
//FirstName
QDomElement fNameElement = ownerElement.firstChildElement("FirstName");
QString fName = fNameElement.text();
//LastName
QDomElement lNameElement = fNameElement.nextSiblingElement("LastName");
QString lName = lNameElement.text();
//Address
QDomElement addressElement = lNameElement.nextSiblingElement("Address");
QString address = addressElement.text();
//append vehicle to the list
vehicles.append(newVehicle);
}
}
并且try-catch块在on_actionOpen_triggered()
方法
void MainWindow::on_actionOpen_triggered()
{
if(isModified){
QMessageBox msgBox;
QString message = "There are unsaved changes! Do you proceed? ";
msgBox.setWindowTitle("Save Changes");
msgBox.setText(message);
msgBox.addButton("Don't save",QMessageBox::NoRole);
msgBox.addButton(QMessageBox::Save);
msgBox.addButton(QMessageBox::Cancel);
msgBox.setIcon(QMessageBox::Question);
int result = msgBox.exec();
if(result == QMessageBox::Save){
VehicleHelper::writeVehicles(cars,filename);
msgBox.close();
} else {
QString fileName = QFileDialog::getOpenFileName(this,"Open file");
if(!fileName.isEmpty()){
setFileName(fileName);
QFile file(fileName);
try {
cars = VehicleHelper::readVehicles(fileName);
} catch(FileCannotBeOpenException &ex) {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle("Message!");
msgBox.setText("Failed to open file");
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
return;
} catch(InvalidXMLContentException &ex){
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle("Message!");
msgBox.setText("Failed to load data!");
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
return;
}
setTakenNumbersList(cars);
//set data to the table view
populate(cars);
}
}
} else {
QString fileName = QFileDialog::getOpenFileName(this,"Open file");
if(!fileName.isEmpty()){
setFileName(fileName);
QFile file(fileName);
if(file.open(QFile::ReadOnly | QFile::Text)){
cars = VehicleHelper::readVehicles(fileName);
setTakenNumbersList(cars);
file.close();
//set data to the table view
populate(cars);
ui->actionAdd->setEnabled(true);
ui->actionBy_name->setEnabled(true);
ui->actionBy_registration_date->setEnabled(true);
ui->actionBy_registration_number->setEnabled(true);
ui->actionBy_revision_date->setEnabled(true);
ui->actionBy_type->setEnabled(true);
ui->actionClear_Search->setEnabled(true);
ui->actionDelete->setEnabled(true);
ui->actionEdit->setEnabled(true);
ui->actionSave->setEnabled(true);
ui->actionSave_As->setEnabled(true);
}
}
}
}
答案 0 :(得分:0)
在将".xml"
发送给QString
之前,您必须检查QFile
中的最后四个字符是{{1}}。