[SOLVED] How to set an item in a QTreeView as selected?
-
Hi,
I'm having this function to open a context menu in a QTreeView:
void MyTreeView::mousePressEvent(QMouseEvent* event) { // get the buttons type Qt::MouseButtons mouseButton = event->buttons(); // only the right mouse buton if( mouseButton == Qt::RightButton ) { //remember previous selection and then clear selection QPersistentModelIndex prevIndex = selectedIndexes()[0]; clearSelection(); //select item at cursor position QPersistentModelIndex nextIndex = indexAt(QCursor::pos()); QItemSelection prevSelection (prevIndex, prevIndex); QItemSelection nextSelection (nextIndex, nextIndex); selectionChanged(prevSelection, nextSelection); // start the context menu QModelIndexList sel (selectedIndexes()); if ( sel.size() > 0 && sel[0].isValid() ) { m_pContextMenu->exec(QCursor::pos()); } } else { //call the parents function QTreeView::mousePressEvent(event); } }
I've tried various other methods to set the item at the cursor position to be selected but have yet failed to achieve my goal.
-
Look to
QItemSelectionModel * QAbstractItemView::selectionModel () const
and
_QItemSelectionModel_
methodsvirtual void select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command ) virtual void select ( const QItemSelection & selection, QItemSelectionModel::SelectionFlags command )
-
Thanks for the answer Vass. But i've also tried that before. See below.
void MyTreeView::mousePressEvent(QMouseEvent* event) { // get the buttons type Qt::MouseButtons mouseButton = event->buttons(); // only the right mouse buton if( mouseButton == Qt::RightButton ) { //select item at cursor position QPersistentModelIndex nextIndex = indexAt(QCursor::pos()); selectionModel()->select(nextIndex, QItemSelectionModel::SelectCurrent); // start the context menu QModelIndexList sel (selectedIndexes()); if ( sel.size() > 0 && sel[0].isValid() ) { m_pContextMenu->exec(QCursor::pos()); } } else { //call the parents function QTreeView::mousePressEvent(event); } }
The result is that the previously selected item is deselected but the item at the cursor position is not selected and since no item is selected the context menu doesn't open.
-
As I know this set the item as selected:
setCurrentIndex(indexAt(event->pos())); or treeView->setCurrentIndex(somemodelindex);
-
Thanks broadpeak @setCurrentIndex(indexAt(event->pos()));@ worked.
I tried thisQPersistentModelIndex nextIndex = indexAt(QCursor::pos()); setCurrentIndex(nextIndex);
before but that didn't work. Any ideas why?
-
@Vass
Your suggestion also worked when I used event->pos() instead of QCursor::pos().Guess I forgot to map the Cursor position from global to widget coordinates. Stupid me.
-
before but that didn’t work. Any ideas why?
Because the index comes from the model and the mousePressEvent comes from the event object (through view).
The two is not the same. -
@KA51O Hi ka. I have a treewidget upon a mainwindow ,how to select the first item when app starts to run?
in mainwindow::() constructed function I don't have event->pos() -