Qt - Expand QTreeView in one click?

By double-clicking on the QTreeView text, the child elements expand, one click does not. An icon defined in CSS (and placed to the left of the text) expands the capabilities of children with a single click. How to make a single click (or touch event) of the text expand the child elements?

bookTreeView->setModel(standardModel); bookTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers); bookTreeView->setWordWrap(true); bookTreeView->sizeHint(); //bookTreeView->mousePressEvent(QMouseEvent()); bookTreeView->setTextElideMode(Qt::ElideNone); bookTreeView->setExpandsOnDoubleClick(true); bookTreeView->setUniformRowHeights(true); bookTreeView->setHeaderHidden(true); bookTreeView->setStyleSheet("QTreeView { font-size: 27px; show-decoration-selected: 0; } QTreeView::branch:has-siblings:!adjoins-item { border-image: none; } QTreeView::branch:has-siblings:adjoins-item { border-image: none; } QTreeView::branch:!has-children:!has-siblings:adjoins-item { border-image: none;} QTreeView::branch:has-children:!has-siblings:closed, QTreeView::branch:closed:has-children:has-siblings { border-image: none; image: url(':images/images/right_arrow.png'); } QTreeView::branch:open:has-children:!has-siblings, QTreeView::branch:open:has-children:has-siblings { border-image: none; image: url(':images/images/down_arrow.png'); } "); 
+4
source share
3 answers

Something along the lines

 QObject::connect( tree, SIGNAL(clicked(const QModelIndex &)), tree, SLOT(expand(const QModelIndex &)) ); 

The clicked signal may not do what you want. You can also look at the currentChanged signal, which may be what you want. I have never used Qt in a mobile context :)

+8
source

The same Grund answer, but I close the click if it is already open.

 QObject::connect( tree, SIGNAL(clicked(const QModelIndex &)), this, SLOT(expandItem(const QModelIndex &)) ); void MainWindow::expandItem(const QModelIndex &index) { tree->isExpanded(index)? tree->collapse(index) : tree->expand(index); } 

in mainwindow.h:

 private slots: void expandItem(const QModelIndex &index) 
+3
source

Have you tried disabling the double-click extension?

 bookTreeView->setExpandsOnDoubleClick(false); 
-1
source

All Articles