How to get QModelIndex of a model which stores pointers to an external model

771 Views Asked by At

I have a model of 2 lists of bookmark nodes (each one containing names and possibly a url) from 2 different browsers.

class Bookmark_Node {
           QString name, 
           QString url, 
           Bookmark_node* parent}

The lists of bookmarks:

    std::vector<Bookmark_Nodes *>bookmarkList_1
    std::vector<Bookmark_Nodes *>bookmarkList_2

This model also has a compare method, which figures out which bookmarks are only included in one of the bookmark list. As a result I have 2 additional lists in the model:

std::vector<Bookmark_Nodes *>bookmarkOnlyInList_1
std::vector<Bookmark_Nodes *>bookmarkOnlyInList_2

With Qt I am using 2 QTreeview to show bookmarkList_1 and bookmarkList_2, where QModelIndex points internally to Bookmark_Nodes.

Now, since I have bookmarkOnlyInList_1/bookmarkOnlyInList_1, I want to scroll to a Bookmark_Node by a given integer var (e.g. 3rd unique Bookmark_Node from bookmarkOnlyInList_1.

In Order to find the QModelIndex in the tree view, I am trying to use QAbstractIem::match().

But I am lost:

How to set the startIndex (1st parameter) to the the top-level root of the tree?

How to pass my Bookmark_Node pointer to QVariant (3rd parameter)?

Or more generally: How to get a QModelIndex for passing to QTreeView::scrollTo, having the internal pointer Bookmark_Node pointer.

I can enrich bookmarkOnlyInList_1/2 by QModelIndices, but that would mix the model with the view, which I want to avoid. Any suggestions?

Some more information:

When the program launches, it looks like: enter image description here

After pressing the button it should search for "Mike" and expand this node, like in the following image. The problem is, it is not expanded. enter image description here

If I expand everything, I get the full model: enter image description here

Here the code which will run after launching the button. The line QModelIndexList includes the QModelIndex for Mike, but row and count are set to zero. Maybe this is the cause of the problem.

void MainWindow::on_pushButton_clicked()
{
    TreeNode::T_Ptr tn = model->root();
    TreeNode* searchNode = tn->child(1)->child(1).get();// Should be Mike
    qDebug() << "SearchNode = "
             << QString::fromStdString(searchNode->name());

    QModelIndex start = model->index(0, 0);
    QVariant dat = QVariant(QString::fromStdString(searchNode->name()));

    QModelIndexList l = model->match(start, Qt::DisplayRole,
                                    dat, 10,
                                    Qt::MatchRecursive);
    for (QModelIndex i : l) {
        qDebug() << "Row = " << i.row() << " Column = " << i.column();
        TreeNode* s = static_cast<TreeNode*>(i.internalPointer());
        qDebug() << "Search Name = " << QString::fromStdString(s->name());
        if (s == searchNode) {
            qDebug() << "From Model = " << QString::fromStdString(s->name());
            ui->treeView->expand(i);
            qDebug() << "expanded = " << ui->treeView->isExpanded(i);
            // ui->treeView->expandAll();
            ui->treeView->scrollTo(i, QAbstractItemView::EnsureVisible);
        }
    }
}
0

There are 0 best solutions below