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. QTreeView focus events and losing selection
Forum Updated to NodeBB v4.3 + New Features

QTreeView focus events and losing selection

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtreeview
4 Posts 3 Posters 1.9k Views 2 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by Dariusz
    #1

    Hello

    Say I have a programatic function that selects tree items in my view. However when I click back on view, it auto deselects all items and select the one I clicked on to...

    How can I combat this behavior? I tried focusInEvent/set flag to ignore next selection change but I had no luck...

    I'd like to control when click on tree view will cause selection "action" vs when to ignore it as I just want to bring the view to focus so I can execute say... short cut that would scroll to selected items...

    TIA

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      Hi,
      Installing an EventFilter seems to be a solution.
      This is what I'm using to prevent a right click to operate on inactive windows:

      bool Application::eventFilter(QObject *obj, QEvent *event)
      {
      	if(event->type() EQ QEvent::MouseButtonPress)
      		{
      		QMouseEvent* mouseEvent=static_cast<QMouseEvent*>(event);
      		QWidget* win=topLevelAt(mouseEvent->globalPos());
      
      		if(mouseEvent->button() EQ Qt::RightButton)
      			{
      			if( win AND win!=activeWindow())
      				{
      				// empêche de faire un clic droit sur une fenetre non active
      // prevent a right click on non active windows
                      win->activateWindow();
                      win->raise();
      				return true;
      				}
      			}
      ....
      

      In your case, replace Qt::RightButton by Qt::LeftButton
      I'm installing this filter at app level (all top level widgets are concerned)
      I think you can install this filter only for the widgets your treeview is in.

      D 1 Reply Last reply
      1
      • M mpergand

        Hi,
        Installing an EventFilter seems to be a solution.
        This is what I'm using to prevent a right click to operate on inactive windows:

        bool Application::eventFilter(QObject *obj, QEvent *event)
        {
        	if(event->type() EQ QEvent::MouseButtonPress)
        		{
        		QMouseEvent* mouseEvent=static_cast<QMouseEvent*>(event);
        		QWidget* win=topLevelAt(mouseEvent->globalPos());
        
        		if(mouseEvent->button() EQ Qt::RightButton)
        			{
        			if( win AND win!=activeWindow())
        				{
        				// empêche de faire un clic droit sur une fenetre non active
        // prevent a right click on non active windows
                        win->activateWindow();
                        win->raise();
        				return true;
        				}
        			}
        ....
        

        In your case, replace Qt::RightButton by Qt::LeftButton
        I'm installing this filter at app level (all top level widgets are concerned)
        I think you can install this filter only for the widgets your treeview is in.

        D Offline
        D Offline
        Dariusz
        wrote on last edited by
        #3

        @mpergand Interesting thanks! It sounds like a solution. I will give it a go in a few.

        TIA

        1 Reply Last reply
        0
        • CKurduC Offline
          CKurduC Offline
          CKurdu
          wrote on last edited by CKurdu
          #4

          You can subclass QTreeView and override setSelection. Firstly don't forget to set the multiselection mode.

          view->setSelectionMode(QAbstractItemView::MultiSelection);
          

          Override the setSelection method.

          #define MAX_SELECT 3
          void TreeView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
          {
              if(command & QItemSelectionModel::Deselect)
              {
                  QTreeView::setSelection(rect,command);
                  return;
              }
              if(command & QItemSelectionModel::Clear)
              {
                  //Actually purpose this "if branch" to disable clear
                  //but MultiSelection mode already do that so execute below command is normal.
                  QTreeView::setSelection(rect,command);
                  return;
              }
              QModelIndexList  ls = this->selectedIndexes();
              int total = 0;
              for(int i=0; i< ls.size();i++)
              {
                  if(ls[i].column() != 0) continue;
                  QModelIndex p = ls[i].child(0,0);
                  if(!p.isValid())
                  {
                      //I think when p is invalid ls[i] doesn't have any child. Maybe there is an another way to find this.
                      total++;
                  }else{
                      QString child_data =  p.data().toString();
                  }
              }
              if(total < MAX_SELECT )
              {
                  QTreeView::setSelection(rect,command);
              }else{
                  //You can scroll here.
              }
              return;
          }
          
          

          You reap what you sow it

          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