[SOLVED] How to set an item in a QTreeView as selected?
-
wrote on 9 Nov 2011, 08:49 last edited by p3c0
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.
-
wrote on 9 Nov 2011, 09:01 last edited by p3c0
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 )
-
wrote on 9 Nov 2011, 09:14 last edited by p3c0
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.
-
wrote on 9 Nov 2011, 09:33 last edited by p3c0
As I know this set the item as selected:
setCurrentIndex(indexAt(event->pos())); or treeView->setCurrentIndex(somemodelindex);
-
wrote on 9 Nov 2011, 09:38 last edited by p3c0
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?
-
wrote on 9 Nov 2011, 09:41 last edited by
@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.
-
wrote on 9 Nov 2011, 18:13 last edited by
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. -
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?
-
@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()