Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved How to use pressed() and selectionChanged together?

    General and Desktop
    4
    6
    1903
    Loading More Posts
    • 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
      Qt Enthusiast last edited by A Former User

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • Q
          Qt Enthusiast last edited by

          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

          Diracsbracket 1 Reply Last reply Reply Quote 0
          • Diracsbracket
            Diracsbracket @Qt Enthusiast last edited by Diracsbracket

            @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 Reply Quote 0
            • VRonin
              VRonin last edited by

              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

              Diracsbracket 1 Reply Last reply Reply Quote 1
              • Diracsbracket
                Diracsbracket @VRonin last edited by Diracsbracket

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

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post