Clear QTreeView selection when focus is outside its geometry
Unsolved
General and Desktop
-
I have the following code inside my QTreeView derived class. Its purpose is to clear the QTreeView's selection when an outside widget is focused, but not when the widget is a delegate editor of the QTreeView.
auto app = static_cast<QApplication *>(QApplication::instance()); connect(app, &QApplication::focusChanged, [=](QWidget *old, QWidget *now) { if (now && !this->isAncestorOf(now)) { clearSelection(); } });
This code works fine when the delegate is a slider, but when the delegate is a QComboBox, the popup is not considered a child of the QTreeView and the selection is cleared unintentionally. Is there some other way to achieve this?
-
Came up with the following hack to identify when a delegate QComboBox gets focus:
auto app = static_cast<QApplication *>(QApplication::instance()); connect(app, &QApplication::focusChanged, [=](QWidget *old, QWidget *now) { auto newTypeStr = now->metaObject()->className(); auto isComboBox = QString{newTypeStr} == QString{"QComboBoxListView"}; if (now && !this->isAncestorOf(now) && !isComboBox) { clearSelection(); } });
This works for my purposes but there's probably a less hacky way to do this.