• 2522阅读
  • 1回复

新手学QT,想先复制代码来看看,结果在文章 QT数据库XML(二) 中的照搬的一段出问题了 [复制链接]

上一主题 下一主题
离线423339589
 
只看楼主 倒序阅读 楼主  发表于: 2013-01-13
关键词: 求助信号槽
void Widget::on_pushButton_5_clicked()                          
{                                                                
                                                                
    ui->listWidget->clear(); //先清空显示                            
    QFile file("my.xml");                                        
    if (!file.open(QIODevice::ReadOnly)) return ;                
    QDomDocument doc;                                            
    if (!doc.setContent(&file))                                  
    {                                                            
    file.close();                                                
    return ;                                                    
    }                                                            
    file.close()                                                
    //返回根节点及其子节点的元素标记名                                          
    QDomElement docElem = doc.documentElement();  //返回根元素        
    QDomNode n = docElem.firstChild();   //返回根节点的第一个子节点          
    while(!n.isNull())  //如果节点不为空                                
    {                                                            
    if (n.isElement()) //如果节点是元素                                
    {                                                            
    QDomElement e = n.toElement(); //将其转换为元素                    
    ui->listWidget->addItem(e.tagName()+e.attribute(tr("编号")));  
    QDomNodeList list = e.childNodes();                          
    for(int i=0; i<list.count(); i++)                            
    {                                                            
    QDomNode node = list.at(i);                                  
    if(node.isElement())                                        
    ui->listWidget->addItem("   "+node.toElement().tagName()    
    +" : "+node.toElement().text());                            
    }                                                            
    }                                                            
    n = n.nextSibling();  //下一个兄弟节点                              
    }                                                            
}                                                                

运行结果出现
In menber function'void Widget::on_pushButton_5_clicked()':  
expected':'before'QDomElement'
'docElem' was not declared in this scope
在线uidab

只看该作者 1楼 发表于: 2013-01-15
回 楼主(423339589) 的帖子
file.close()                               <-----------------------这里没有分号啊!!                        
    //返回根节点及其子节点的元素标记名                                          
    QDomElement docElem = doc.documentElement();  //返回根元素
有时候为了工作直接获得答案,而我却失去了思考的乐趣!


飘啊飘,何时能安居!
快速回复
限100 字节
 
上一个 下一个