2013-04-13 88 views
0

这很混乱。我的程序工作可靠。然后,我匆匆做了一些改变,他们没有工作,重新缠绕他们,去显示我的程序,它不再有效。我的错误是每10分钟不做新的复印件。然而事情是,该程序崩溃在一个没有道理的地方。QDomElement.setAttribute崩溃程序

QDomElement Expense::toNode() 
{ 
    QDomElement e=Entry::toNode(); //Entry is parent of Expense 
    QString temp; 
    //std::string getThis=e.nodeName().toStdString(); 
    temp=QString::fromStdString(Category); //Category is string field 

    //e.hasAttribute("category"); //this works 
    //e.setAttribute("ha","hi"); //this crashes program 
    //e.setAttribute("category",temp); //this also crashes program 
    return e; 
} 

我想,也许在我匆忙修改了一些图书馆,但如果我创建一个新的QDomElement,并编辑它的属性,存在一点问题都没有。然后我想,也许我的节点根本不是节点,但我可以使用许多其他功能(例如e.hasAttribute)。我们可以设置的属性数量是否有限制?什么可能是错误?

在情况下,它可以帮助:

QDomElement Entry::toNode() 
{ 
    QDomDocument d("EzXpns"); 
    QDomElement e=d.createElement("entry"); 
    QString temp; 
    temp=QString::fromStdString(Name); 
    e.setAttribute("name",temp); 
    temp=QString::fromStdString(to_string(static_cast<long double>(Amount))); 
    e.setAttribute("amount",temp); 
    temp=QString::fromStdString(to_string(static_cast<long long>(Date[0]))); 
    e.setAttribute("dateyear",temp); 
    temp=QString::fromStdString(to_string(static_cast<long long>(Date[1]))); 
    e.setAttribute("datemonth",temp); 
    temp=QString::fromStdString(to_string(static_cast<long long>(Date[2]))); 
    e.setAttribute("dateday",temp); 
    temp=QString::fromStdString(Comment); 
    e.setAttribute("comment",temp); 
    return e; 
} 

编辑:我应该规定,如果我尝试调试这是消息我得到:

TestBuild.exe引发了断点

然后

TestBuild.exe中0x77d415de未处理的异常:0xC0000005:访问冲突读取位置0x13fb8ff8。

然后

0x77d3016e在TestBuild.exe:00000000:操作成功完成。

EDIT2:示例XML

<!DOCTYPE data> 
<EzXpns> 
    <account> 
    <login name="b" password="1"/> 
    <expHis> 
     <entry comment="9" dateday="1" name="k" dateyear="0" amount="9" datemonth="1"/> 
     <entry comment="9" dateday="1" name="b" dateyear="0" amount="9" datemonth="1"/> 
     <entry comment="9" dateday="1" name="b" dateyear="0" amount="9" datemonth="1"/> 
     <entry comment="9" dateday="1" name="b" dateyear="0" amount="9" datemonth="1"/> 
     <entry comment="9" dateday="1" name="b" dateyear="0" amount="9" datemonth="1"/> 
    </expHis> 
    <incomeHis/> 
    </account> 
</EzXpns> 

回答

0

溶液Qt文档中。

看看我如何创建新元素,我通过调用QDomDocument d("EzXpns");这是一个错误。在Qt中,QDomDocument被销毁后,所有的孩子都没有。它仅仅因为纯粹的运气才奏效。创建一个QDomDocument,然后传递它,解决了我的问题。