Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Unsolved Clearing selection in three view & next selection not executed issues...

    General and Desktop
    3
    6
    157
    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.
    • D
      Dariusz last edited by

      Hey

      I'm doing some tests with selection in tree views, I want to have only 1 selection active from my tree views, when I run this command, even tho it will clear selection in other view, when I press, initially there is no selection. It feels like bug/glitch? Any ideas how to bite it ? I have to click on item 2x, 1 to clear sel, second to actually select...

      void getSelectedItemList(QTreeView *view) {
          qDebug() << view->selectionModel()->selectedIndexes();
      }
      
      int main(int argc, char *argv[]) {
          QApplication a(argc, argv);
      
          QTreeView *view = new QTreeView();
          QStandardItemModel *model = new QStandardItemModel();
      
          for (int x = 0; x < 10; x++) {
              model->appendRow(new QStandardItem(QString::number(x)));
          }
          view->setModel(model);
          QWidget w;
          QGridLayout *lay = new QGridLayout(&w);
          lay->addWidget(view);
      
          QPushButton *btnA = new QPushButton("Print sel top");
          QObject::connect(btnA, &QPushButton::released, [=]() { getSelectedItemList(view); });
      
          QTreeView *view2 = new QTreeView();
          QStandardItemModel *model2 = new QStandardItemModel();
          for (int x = 0; x < 10; x++) {
              model2->appendRow(new QStandardItem(QString::number(x)));
          }
          view2->setModel(model2);
      
          QPushButton *btnB = new QPushButton("Print sel bottom");
          QObject::connect(btnB, &QPushButton::released, [=]() { getSelectedItemList(view2); });
          lay->addWidget(view2);
          lay->addWidget(btnA);
          lay->addWidget(btnB);
      
          auto selModelA = view->selectionModel();
          auto selModelB = view2->selectionModel();
      
          QObject::connect(selModelA, &QItemSelectionModel::selectionChanged, [=]() { view2->clearSelection(); });
          QObject::connect(selModelB, &QItemSelectionModel::selectionChanged, [=]() { view->clearSelection(); });
      
          w.show();
          return a.exec();
      }
      
      1 Reply Last reply Reply Quote 0
      • D
        Dariusz last edited by

        Hey

        Another issue I found, if I do view->currentIndex(), after deselection this still returns a valid item!

        JonB 1 Reply Last reply Reply Quote 0
        • JonB
          JonB @Dariusz last edited by JonB

          @Dariusz said in Clearing selection in three view & next selection not executed issues...:

          Another issue I found, if I do view->currentIndex(), after deselection this still returns a valid item!

          For that one I think I know, though I would welcome anyone correcting me!

          Current item and selected item are not the same thing. I don't find the docs explain much, but isn't current item the last one selected? Regardless of deselection? I think I understand selected item, not so much "current" item.

          Once again, if any expert would care to clarify this I should be most interested.

          1 Reply Last reply Reply Quote 0
          • D
            Dariusz last edited by

            Bump, can any1 help ?

            1 Reply Last reply Reply Quote 0
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion last edited by

              @Dariusz said in Clearing selection in three view & next selection not executed issues...:

              Bump, can any1 help ?

              Since the selectionChanged signal is called also when you clear the selection you created an endless loop I would guess. Put some debug output when you call clearSelection() to see if I'm correct.

              @JonB: yes, selection and current index are two independent things

              Qt has to stay free or it will die.

              D 1 Reply Last reply Reply Quote 2
              • D
                Dariusz @Christian Ehrlicher last edited by

                @Christian-Ehrlicher said in Clearing selection in three view & next selection not executed issues...:

                @Dariusz said in Clearing selection in three view & next selection not executed issues...:

                Bump, can any1 help ?

                Since the selectionChanged signal is called also when you clear the selection you created an endless loop I would guess. Put some debug output when you call clearSelection() to see if I'm correct.

                @JonB: yes, selection and current index are two independent things

                Hey

                Hmmmmmmmmmmmmmmmmmmmmmmmmm I think I might see the issues. Clearing selection on view2 emits signal of selectionChanged for view1 o.O ok I think I got it o.o

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