Home > Software engineering >  How to make Toolbar to Expand the Qtreeview in DOM?
How to make Toolbar to Expand the Qtreeview in DOM?

Time:12-15

I want to Add another toolbar Icon next to the first one. The First Icon loads the XML file into Treeview:

QToolBar *fileToolBar = addToolBar(tr("FIND"));
     const QIcon newIcon = QIcon::fromTheme("FIND", QIcon("Image\\FIND.png"));
     QAction *newAct = new QAction(newIcon, tr("&FIND"), this);
     newAct->setShortcuts(QKeySequence::Open);
     connect(newAct, &QAction::triggered, this, &MainWindow::openFile);
     fileToolBar->addAction(newAct);

I want that Second tool item Expands the Treeview. So far I have tried: But It didn't worked!

 QToolBar *ExpandToolBar = addToolBar(tr("Expand"));
     const QIcon newIcon2 = QIcon::fromTheme("Expand", QIcon("\Image\\Expand.png"));
     QAction *newAct2 = new QAction(newIcon2, tr("&Expand"), this);
     newAct2->setShortcuts(QKeySequence::SelectStartOfLine);
     connect(newAct2, &QAction::isVisible, this, &MainWindow::expandChildren);
     ExpandToolBar->addAction(newAct2);

Where expandChildren Function is as Follow:

void MainWindow::expandChildren(const QModelIndex &index, QTreeView *view)
{
    if (!index.isValid()) {
        return;
    }

    int childItem = index.model()->rowCount(index);
    for (int i = 0; i < childItem; i  ) {
        const QModelIndex &child = index.sibling(i, 0);
        // Recursively call the function for each child node.
        expandChildren(child, view);
    }

    if (!view->isExpanded(index)) {
        view->expand(index);
    }
}

In the Image I have shown Where I want that Icon!

Please Help with Expand ToolBar creation! Errors I am getting: enter image description here

With expandAll(): Errors are: enter image description here

CodePudding user response:

The call...

connect(newAct2, &QAction::isVisible, this, &MainWindow::expandChildren);

can't work for a number of reasons: 1) QAction::isVisible is not a signal and 2) since MainWindow::expandChildren has the signature...

void MainWindow::expandChildren(const QModelIndex &index, QTreeView *view)

you need to supply the arguments somehow. The easiest way would be to connect to the triggered signal and use a lambda rather than invoking the function directly. Assuming your QTreeView member is named view that might look something like...

connect(newAct2, &QAction::triggered, this,
        [this]{
            MainWindow::expandChildren(view->rootIndex(), view);
        });

Better still, just use QTreeView::expandAll...

connect(newAct2, &QAction::triggered, this, [this]{ view->expandAll(); });
  • Related