QTreeView focus issue
-
Hello,
I want to use QTreeView keyboard binding functionality. For example, hold shift and press up/down to select multiple items in the tree.
To able use this functionality. I have to press tab to set the focus for QTreeView. It is annoying. I want to press shift+up/down, after I clicked with the mouse. Each time, I don't want to press tab.
How can I do this? I tried to use setFocus() but it did not work.
Thank you.
-
Hi,
Did you already setup the selection mode on your QTreeView ?
How are you setting up that QTreeView ?
-
Did you reimplement anything ? How do you initialize it ?
-
Following virtual method were reimplemented.
void keyPressEvent(QKeyEvent *event); void mousePressEvent(QMouseEvent *event); void dragMoveEvent(QDragMoveEvent *event); void dropEvent(QDropEvent *event); void contextMenuEvent(QContextMenuEvent *event);
the way, I initialize it is I added a QTreeView to UI file and promote to my new derived class.
Thank you for your help.
-
mousePressEvent ? Do you call the base class implementation in your code ?
-
Also in the other reimplemented functions ?
-
Can you show your implementation of mousePressEvent ?
-
here is the code, I deleted some part of the code.
void TreeView::mousePressEvent(QMouseEvent *event) { //![1] Control if(event->modifiers() == Qt::ControlModifier) { QModelIndex index = indexAt(event->pos()); if(index.isValid()) { index = model()->index(index.row(), 0, index.parent()); QString name = index.data(Qt::UserRole).toString(); if(name == "xxx") { event->ignore(); return; } if(name == "yyy") { event->ignore(); return; } } } //![1] QTreeView::mousePressEvent(event); }
-
And the keyPressEvent ?
-
Sory, I was not able to access my computer. Here is the keypressevent
void TreeView::keyPressEvent(QKeyEvent *event) { if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_C) { copyAction(); } else if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_V) { pasteAction(); } QTreeView::keyPressEvent(event); }
-
Hello,
I have found my mistake, during mouse pressed event I was calling some method that changes the focus some other widget. That's why It was losing focus.
Solution.
I reimplement, mouse release event and I set the focus to the QTreeView again.
Thank you for your help!