Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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();
    }
    


  • Hey

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



  • @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.



  • Bump, can any1 help ?


  • Lifetime Qt Champion

    @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



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


Log in to reply