Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Clear QTreeView selection when focus is outside its geometry
Qt 6.11 is out! See what's new in the release blog

Clear QTreeView selection when focus is outside its geometry

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 525 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lurkki
    wrote on last edited by
    #1

    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?

    L 1 Reply Last reply
    0
    • L Lurkki

      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?

      L Offline
      L Offline
      Lurkki
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved