我是 Qt 的新手。将从 XML 读取的项目添加到 QTableView 时,我的程序崩溃了。
XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Accounts>
<Host>host1</Host>
<Login>dawid1</Login>
<Password>pass1</Password>
<Status>status1</状态>
<Host>host2</Host>
<Login>dawid2</Login>
<Password>pass2</Password>
<Status>status2</Status>
</Accounts>
添加项目代码:
QXmlStreamReader xmlReader;
QAbstractItemModel *model = ui->tableView->model();
xmlReader.setDevice(&file);
while(!xmlReader.atEnd())
{
QXmlStreamReader::TokenType token = xmlReader.readNext();
if(token == QXmlStreamReader::StartDocument)
{
continue;
}
if(token == QXmlStreamReader::StartElement)
{
if(xmlReader.name() == "Accounts")
continue;
if(xmlReader.name() == "Host")
{
model->insertRow(model->rowCount());
model->setData(model->index(model->rowCount(),0), xmlReader.readElementText()); //probably here crash
}
if(xmlReader.name() == "Login")
{
model->insertRow(model->rowCount());
model->setData(model->index(model->rowCount(),1), xmlReader.readElementText());
}
if(xmlReader.name() == "Password")
{
model->insertRow(model->rowCount());
model->setData(model->index(model->rowCount(),2), xmlReader.readElementText());
}
if(xmlReader.name() == "Status")
{
model->insertRow(model->rowCount());
model->setData(model->index(model->rowCount(),3), xmlReader.readElementText());
}
}
if(xmlReader.hasError())
{
QMessageBox::critical(this, "XML Parse Error",xmlReader.errorString(), QMessageBox::Ok);
}
}
xmlReader.clear();
file.close();
我的问题是:
1. 为什么程序崩溃以及如何修复它?
2. 未来:如何在 QtCreator 中定位崩溃原因并修复它?
问候,大卫