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. Clearing selection in three view & next selection not executed issues...
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 491 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    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
    0
    • D Offline
      D Offline
      Dariusz
      wrote on last edited by
      #2

      Hey

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

      JonBJ 1 Reply Last reply
      0
      • D Dariusz

        Hey

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @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
        0
        • D Offline
          D Offline
          Dariusz
          wrote on last edited by
          #4

          Bump, can any1 help ?

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            D 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              @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

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

              @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
              0

              • Login

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