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. How to use pressed() and selectionChanged together?
Forum Updated to NodeBB v4.3 + New Features

How to use pressed() and selectionChanged together?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 2.5k 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by A Former User
    #1

    Hi All
    I have following code

    QTableView * table ;
    QItemSelectionModel* qsm = table->selectionModel();
      if (qsm) {
          QObject::connect(qsm, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),this, SLOT(itemselectionChanged()));
      }
    
    // this line is require when I click the already selected row
    QObject::connect(table SIGNAL(pressed(const QModelIndex&)),this,  SLOT(itemselectionChanged()));
    

    Now my question is there is some problem in this code itemSelectionChanged is called twice when we change the selection in table .one beacause of signal selectionChanged on selectionmodel and othet becasue of pressed

    The solution to avoid the two calls is like

    mQtableView  table = new QtableView;
    mQTableView * table ;
    QItemSelectionModel* qsm = table->selectionModel();
      if (qsm) {
          QObject::connect(qsm, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),this, SLOT(itemselectionChanged()));
      }
    
    // this line is require when I click the already selected row
    QObject::connect(table SIGNAL(pressed(const QModelIndex&)),this,  SLOT(itempresse()));
    

    When we call

    mQtableView:: handleSelection() {
    }
    mQtableView:: itemselectionChanged() {
       selectChangeFlag = true
    
       handleSelection
    }
    
    mQtableView::itempressed() {
    
      if (selectionChanged)
         return
      else 
       handleSelection();
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Am not sure why you need pressed signal also.
      First time u click item, selectionChanged is fired and
      you call handleSelection.

      If you then click on the the current selected item again, why
      do you then need to call handleSelection again?
      Since it was done in selectionChanged on the first click ?

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        When we call handleSelection in window W0, we select objects in other window w1 the scenario I need to emit itempressed is

        when we deselect the objects in w1 , by clicking on an empty area in W1 , now I go again select in W0 , THE OBJECT IS not selected in W1 , since selectedChanged signal is not emittted that it is why i need to connect a slot for pressed

        DiracsbracketD 1 Reply Last reply
        0
        • Q Qt Enthusiast

          When we call handleSelection in window W0, we select objects in other window w1 the scenario I need to emit itempressed is

          when we deselect the objects in w1 , by clicking on an empty area in W1 , now I go again select in W0 , THE OBJECT IS not selected in W1 , since selectedChanged signal is not emittted that it is why i need to connect a slot for pressed

          DiracsbracketD Offline
          DiracsbracketD Offline
          Diracsbracket
          wrote on last edited by Diracsbracket
          #4

          @Qt-Enthusiast Although this is an old post, I dare to reply to it, since it is still marked as unsolved and since I faced the same problem (I wanted to run the row selection handling code even when the user clicks on an already selected row).

          You could use a flag, e.g. m_itemClicked as follows, using the fact that the clicked() event is emitted AFTER the selectionChanged() event. I'll use a QTreeView here.

          First, install an event filter on your QTreeView:

          void MainWindow::setup()
          {
                ...
                m_treeView->viewport()->installEventFilter(this);
          }
          

          In the event filter, set the m_itemClicked flag when the viewport is clicked:

          bool MainWindow::eventFilter(QObject *obj, QEvent *event)
          {
              const QEvent::Type type = event->type();
              if (type == QEvent::MouseButtonPress)
              {
                  QWidget* w = qobject_cast<QWidget*>(obj);
                  QMouseEvent* mouseEvent = static_cast <QMouseEvent *> (event);
                  Qt::MouseButton button = mouseEvent->button();
          
                  if (button == Qt::LeftButton)
                  {
                      if (w == m_treeView->viewport()
                      {
                          m_itemClicked = true;
                      }
                  }
                 ...
                  QMainWindow::event(event);
              }
          }
          

          Then, in the selectionChanged() handler, do something like this:

          void MainWindow::onRowSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
          {
          Q_UNUSED(deselected);
          
              if (selected.indexes().length()>0)
              {
                  const QModelIndex index = selected.indexes()[0];
                  if (index.isValid())
                        handleRowSelection(index); 
          
                  //Avoid double execution of handleRowSelection() by the click handler
                  if (m_itemClicked)
                      m_itemClicked = false;
          
              } //if (selected.indexes()
          }
          

          The above will thus execute handleRowSelection() both upon selection change by key navigation, but also when clicking on a different row (because that also changes the selection). To handle the case where the user clicks on the same row (i.e. the selection does not change), you can do the following in the click handler:

          void MainWindow::on_treeView_clicked(const QModelIndex &index)
          {
              //This means that handleRowSelection() was already
              //executed by onRowSelectionChanged()
              if (!m_itemClicked)
                  return;
          
              handleRowSelection(index);
              m_itemClicked = false;
          }
          
          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            to add my 2cent here, I'd probably just simplify it using a "isScheduled" variable:

            private: 
            bool m_isSelectionScheduled = false;
            Q_SLOT void scheduleItemSelection(){
            if(m_isSelectionScheduled) return;
            m_isSelectionScheduled=true;
            QTimer::singleShot(0,this,[=]()->void{itemselectionChanged(); m_isSelectionScheduled=false;}
            };
            

            then in the constructor

            // assuming this is of type MyClass*
            QObject::connect(qsm,&QItemSelectionModel::selectionChanged,this,&MyClass::scheduleItemSelection);
            QObject::connect(table,&QTableView::pressed,this,  &MyClass::scheduleItemSelection);
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            DiracsbracketD 1 Reply Last reply
            1
            • VRoninV VRonin

              to add my 2cent here, I'd probably just simplify it using a "isScheduled" variable:

              private: 
              bool m_isSelectionScheduled = false;
              Q_SLOT void scheduleItemSelection(){
              if(m_isSelectionScheduled) return;
              m_isSelectionScheduled=true;
              QTimer::singleShot(0,this,[=]()->void{itemselectionChanged(); m_isSelectionScheduled=false;}
              };
              

              then in the constructor

              // assuming this is of type MyClass*
              QObject::connect(qsm,&QItemSelectionModel::selectionChanged,this,&MyClass::scheduleItemSelection);
              QObject::connect(table,&QTableView::pressed,this,  &MyClass::scheduleItemSelection);
              
              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by Diracsbracket
              #6

              @VRonin I haven't tried your solution, but if it works, it is a nice one indeed.

              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