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. QItemSelectionModel signals are not emitted
Qt 6.11 is out! See what's new in the release blog

QItemSelectionModel signals are not emitted

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 3.8k Views
  • 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.
  • starryeyedS Offline
    starryeyedS Offline
    starryeyed
    wrote on last edited by
    #1

    ...Or I can't catch them.
    As long as I try to catch clicked() event of QTableView, it goes fine.
    The funny thing is that I don't even need index (or QItemSelection of indexes), as I have getSelectedCell method in this form.
    No matter how I try to connect, to selectionChanged or to currentChanged, it's to no avail. I change selection with arrow keys in debugging mode, and the debugger doesn't even stop on a breakpoint inside connected lambda (with aforementioned clicked() slot of table view it works).
    Here's the first connect I've tried

    connect(view->selectionModel(),
            QItemSelectionModel::selectionChanged,
            [=] (const QItemSelection &selected, const QItemSelection & deselected) {
        QModelIndex index = selected.indexes().first();
        QString flt = index.sibling(index.row(), 0).data().toString();
        db->addrbook->setFilter("client_id="+flt);
    });
    

    Here's the other connect (also to no avail)

    connect(view->selectionModel(),QItemSelectionModel::currentChanged,
            [=] (const QModelIndex &index) {
        QString flt = index.sibling(index.row(), 0).data().toString();
        db->addrbook->setFilter("client_id="+flt);
    });
    

    Literally same lambda as last one normally connects to clicked().
    What am I missing?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      Just try like this. Not sure about lambda. It works.

      {
      this->view = new QTableView(this);
      QStandardItemModel *model = new QStandardItemModel(14, 14);
      for (int row = 0; row < 14; ++row) {
      for (int column = 0; column < 14; ++column) {
      QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
      model->setItem(row, column, item);
      }
      }
      this->view->setModel(model);
      this->view->resize(this->size());
      connect(this->view->selectionModel(),&QItemSelectionModel::selectionChanged,
      this,&MyWidget::itemSel);
      connect(this->view->selectionModel(),&QItemSelectionModel::currentChanged,
      this,&MyWidget::curChanged);
      }

      void MyWidget::curChanged(const QModelIndex &current, const QModelIndex &previous){
      qDebug() << " Current " << current.row() << " Prev="<<previous.row() << endl;
      }

      void MyWidget::itemSel(const QItemSelection &selected, const QItemSelection &deselected)
      {
      qDebug() << Q_FUNC_INFO << " Item selected"<<endl;
      foreach(QModelIndex ind, selected.indexes()){
      qDebug() << ind.row() << " Col="<<ind.column() << endl;
      }
      }

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        You forgot an &. QItemSelectionModel::selectionChanged should be &QItemSelectionModel::selectionChanged

        I'm surprised your compiler didn't even warn you about this

        "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

        starryeyedS 1 Reply Last reply
        2
        • VRoninV VRonin

          You forgot an &. QItemSelectionModel::selectionChanged should be &QItemSelectionModel::selectionChanged

          I'm surprised your compiler didn't even warn you about this

          starryeyedS Offline
          starryeyedS Offline
          starryeyed
          wrote on last edited by starryeyed
          #4

          @VRonin I tried both what you and @dheerendra suggested, got this in debug output

          QObject::connect: Cannot connect (null)::currentChanged(QModelIndex,QModelIndex) to ClientEd::curChanged(QModelIndex,QModelIndex)
          

          Then I've remembered I actually have a working slot that shows a widget if something is selected and hides it if selection is blank, and tried to make something similar. I have also tried adding a & even despite the fact that slot works normally without &.
          So, I tried this

              connect(view->selectionModel(),
                              SIGNAL(&selectionChanged(QItemSelection,QItemSelection)),
                              this,   SLOT(selChanged(QItemSelection,QItemSelection)));
          

          With and without the &. Still nothing. I get kinda the same thing in debug output.

          QObject::connect: Cannot connect (null)::selectionChanged(QItemSelection,QItemSelection) to ClientEd::selChanged(QItemSelection,QItemSelection)
          

          To completely confuse me, I get the same warning (QObject::connect: Cannot connect (null)) with the working slot that I've just talked about.

          jsulmJ VRoninV 2 Replies Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
            wrote on last edited by dheerendra
            #5

            I have tried with lambda expression as well. It works perfectly fine. Looks like you are doing something wrong with view object. Can you show us sample code piece where u r creating the view object ? Are you removing the selection model by chance ? or you setting some other object for selection model ? Did you try with sample code which I gave ?

            Can you check the return value of the following API in your program ?
            this->view->selectionModel()

            Dheerendra
            @Community Service
            Certified Qt Specialist
            https://www.pthinks.com

            1 Reply Last reply
            0
            • starryeyedS starryeyed

              @VRonin I tried both what you and @dheerendra suggested, got this in debug output

              QObject::connect: Cannot connect (null)::currentChanged(QModelIndex,QModelIndex) to ClientEd::curChanged(QModelIndex,QModelIndex)
              

              Then I've remembered I actually have a working slot that shows a widget if something is selected and hides it if selection is blank, and tried to make something similar. I have also tried adding a & even despite the fact that slot works normally without &.
              So, I tried this

                  connect(view->selectionModel(),
                                  SIGNAL(&selectionChanged(QItemSelection,QItemSelection)),
                                  this,   SLOT(selChanged(QItemSelection,QItemSelection)));
              

              With and without the &. Still nothing. I get kinda the same thing in debug output.

              QObject::connect: Cannot connect (null)::selectionChanged(QItemSelection,QItemSelection) to ClientEd::selChanged(QItemSelection,QItemSelection)
              

              To completely confuse me, I get the same warning (QObject::connect: Cannot connect (null)) with the working slot that I've just talked about.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @starryeyed said in QItemSelectionModel signals are not emitted:

              QObject::connect: Cannot connect (null)

              To me it looks like view->selectionModel() returns null...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • starryeyedS starryeyed

                @VRonin I tried both what you and @dheerendra suggested, got this in debug output

                QObject::connect: Cannot connect (null)::currentChanged(QModelIndex,QModelIndex) to ClientEd::curChanged(QModelIndex,QModelIndex)
                

                Then I've remembered I actually have a working slot that shows a widget if something is selected and hides it if selection is blank, and tried to make something similar. I have also tried adding a & even despite the fact that slot works normally without &.
                So, I tried this

                    connect(view->selectionModel(),
                                    SIGNAL(&selectionChanged(QItemSelection,QItemSelection)),
                                    this,   SLOT(selChanged(QItemSelection,QItemSelection)));
                

                With and without the &. Still nothing. I get kinda the same thing in debug output.

                QObject::connect: Cannot connect (null)::selectionChanged(QItemSelection,QItemSelection) to ClientEd::selChanged(QItemSelection,QItemSelection)
                

                To completely confuse me, I get the same warning (QObject::connect: Cannot connect (null)) with the working slot that I've just talked about.

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                SIGNAL(&selectionChanged(QItemSelection,QItemSelection))

                Do not mix connection syntaxes.

                either use &QItemSelectionModel::selectionChanged or SIGNAL(selectionChanged(QItemSelection,QItemSelection)). If you use the second it might be useful to wrap the connect statement inside a QASSUME() to have a hard check at runtime

                "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

                1 Reply Last reply
                3
                • starryeyedS Offline
                  starryeyedS Offline
                  starryeyed
                  wrote on last edited by
                  #8

                  Yes, it was null. And it was null because supposedly, I need to set a model to the view, it initializes selection model. As soon as I placed connect below model setting, all connects worked.

                  1 Reply Last reply
                  1

                  • Login

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