Solved QTreeView focus issue
-
yes, I set the tree to
setSelectionBehavior(QAbstractItemView::SelectRows); setSelectionMode(QAbstractItemView::ExtendedSelection);
I inherited QTreeView class, so do you think this could be a reason?
-
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!