1

基本的に、私が達成しようとしているのは、「編集可能なツリー モデル」と「単純な Dom モデル」の例を組み合わせることです。だから私は後者をベースにしていて、そこに編集機能をコピーしたり、フラグを変更したりしましたsetData

私はすでにエントリの編集に成功しています。

現在、モデルに行を追加する際に問題があります。

具体的には、 で insertAfter 関数を使用するQDomNode::parentNode()と、モデルは内部的にのみ更新されます。親を折りたたんで展開すると、追加されたノードは最後のノードのコピーであり、最後に挿入されます。

私が正しく取得できる唯一の方法は、XML を に保存し、それをandQStringに再度ロードすることです。その後、挿入されたものはすべて、あるべき場所にあります。しかし、その後、すべての展開された状態が失われます! あまり使い勝手が良くない…QDomModelQTreeView


InsertAfter バージョン:

// 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;
}

これで行が作成されます。クリックして展開できる非表示の親が作成されます。しかし、この親にはモデル全体のコピーが含まれています...


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;
}
4

0 に答える 0