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

QItemSelectionModel signals are not emitted



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


  • Qt Champions 2017

    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;
    }
    }



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

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



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


  • Qt Champions 2017

    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()


  • Lifetime Qt Champion

    @starryeyed said in QItemSelectionModel signals are not emitted:

    QObject::connect: Cannot connect (null)

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



  • 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



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


Log in to reply