Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 问题4:制造;“简单Dom模型”;可编辑;插入行_Xml_Qt_Dom_Qtreeview - Fatal编程技术网

Xml 问题4:制造;“简单Dom模型”;可编辑;插入行

Xml 问题4:制造;“简单Dom模型”;可编辑;插入行,xml,qt,dom,qtreeview,Xml,Qt,Dom,Qtreeview,基本上,我试图实现的是结合“可编辑树模型”和“简单Dom模型”示例。因此,我以后者为基础,复制了那里的编辑函数,更改了标志,setData,等等 我已经成功地编辑了条目 现在,我在向模型中添加行时遇到了一个问题 具体来说,当我在QDomNode::parentNode()中使用insertAfter函数时,模型只在内部更新。折叠并展开父节点时,添加的节点是最后一个节点的副本,并插入到末尾 唯一正确的方法是将XML保存到QString,然后再次将其加载到QDomModel和QTreeView。然后

基本上,我试图实现的是结合“可编辑树模型”和“简单Dom模型”示例。因此,我以后者为基础,复制了那里的编辑函数,更改了标志,
setData
,等等

我已经成功地编辑了条目

现在,我在向模型中添加行时遇到了一个问题

具体来说,当我在
QDomNode::parentNode()
中使用insertAfter函数时,模型只在内部更新。折叠并展开父节点时,添加的节点是最后一个节点的副本,并插入到末尾

唯一正确的方法是将XML保存到
QString
,然后再次将其加载到
QDomModel
QTreeView
。然后,插入的所有内容都应该在其中。但是所有扩展的状态都丢失了!不是很人性化


插入以下版本:

// Get current index
QModelIndex currentTreeIdx = ui->treeView->selectionModel()->currentIndex();
// Get the node corresponding to that index
DomItem *itemRef = static_cast(currentTreeIdx.internalPointer());

QDomElement newParentTag;
QDomElement newTextTag;
QDomText newText;
// Create tags
newParentTag = model->domDocument.createElement("Parent");
newTextTag = model->domDocument.createElement("Child");
newText = model->domDocument.createTextNode("Child text data");
newTextTag.appendChild(newText);
newSubtitleTag.appendChild(newTextTag);
// Attempt to insert a new tag after the currently selected item
itemRef->node().parentNode().insertAfter(newParentTag, itemRef->node());

现在,我尝试添加这样的行,而不是
insertAfter

model->insertRows(itemRef->row(), 1, currentTreeIdx.parent());
正在调用的函数是:

bool DomModel::insertRows(int position, int rows, const QModelIndex &parent){

    DomItem *parentItem = static_cast<DomItem*>(parent.internalPointer());   // reference item

    bool success;

    beginInsertRows(parent, position, position + rows - 1);
    success = parentItem->insertChildren(position, rows, 4);
    endInsertRows();

    return success;
}
booldommodel::insertRows(int位置、int行、常量QModelIndex&parent){
DomItem*parentItem=static_cast(parent.internalPointer());//引用项
成功;
开始插入行(父、位置、位置+行-1);
成功=父项->插入子项(位置,行,4);
endInsertRows();
回归成功;
}
这个应该创建一行。它确实创建了一个不可见的父对象,我可以单击它进行扩展。但此父项现在包含整个模型的副本


看来我的insertChildren功能有问题。与现在一样,它将当前选定的行替换为空行

bool DomItem::insertChildren(int position, int count, int columns){
    if (position < 0 || position > childItems.size()){
        return false;
    }

    for (int row = 0; row < count; row++){
        DomItem *item = new DomItem(*(new QDomNode()), row, this);   // this doesn't seem to be correct...
        childItems.insert(position, item);
    }

    return true;
}
bool domintem::insertChildren(int位置、int计数、int列){
if(位置<0 | |位置>childItems.size()){
返回false;
}
for(int行=0;行<计数;行++){
DomItem*item=new domitm(*(new QDomNode()),行,this);//这似乎不正确。。。
子项。插入(位置、项);
}
返回true;
}