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

QListWidget, itemSelectionChanged problem



  • I have an instance of QListWidget, I've created an object based on this:

        class clsQtListWidget : public QListWidget, public clsXMLinterface {
        Q_OBJECT
    

    I've connected all the available signals to internal slots which I call repeaters:

        private slots:
            void rptrCurrentItemChanged(QListWidgetItem* pobjCurrent
                                       ,QListWidgetItem* pobjPrevious);
            void rptrCurrentRowChanged(int intCurrentRow);
            void rptrCurrentTextChanged(const QString& crstrCurrentText);
            void rptrIndexesMoved(const QModelIndexList& crobjIndexes);
            void rptrItemActivated(QListWidgetItem* pobjItem);
            void rptrItemChanged(QListWidgetItem* pobjItem);
            void rptrItemClicked(QListWidgetItem* pobjItem);
            void rptrItemDoubleClicked(QListWidgetItem* pobjItem);
            void rptrItemEntered(QListWidgetItem* pobjItem);
            void rptrItemPressed(QListWidgetItem* pobjItem);
            void rptrItemSelectionChanged();
    

    Connections in constructor:

        QMetaObject::Connection cn;
        cn = QObject::connect(this, &clsQtListWidget::currentItemChanged
                             ,this, &clsQtListWidget::rptrCurrentItemChanged);
        Q_ASSERT_X(cn, metaObject()->className()
            ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                .arg(clsQtListWidget::mscszQtSignalCurItemChgd)
                        .toLatin1().data());
        cn = QObject::connect(this, &clsQtListWidget::currentRowChanged
                             ,this, &clsQtListWidget::rptrCurrentRowChanged);
        Q_ASSERT_X(cn, metaObject()->className()
            ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                .arg(clsQtListWidget::mscszQtSignalCurRowChgd)
                        .toLatin1().data());
        cn = QObject::connect(this, &clsQtListWidget::currentTextChanged
                             ,this, &clsQtListWidget::rptrCurrentTextChanged);
        Q_ASSERT_X(cn, metaObject()->className()
            ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                .arg(clsQtListWidget::mscszQtSignalTextChgd)
                        .toLatin1().data());
        cn = QObject::connect(this, &clsQtListWidget::indexesMoved
                             ,this, &clsQtListWidget::rptrIndexesMoved);
        Q_ASSERT_X(cn, metaObject()->className()
            ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                .arg(clsQtListWidget::mscszQtSignalIndxesMoved)
                        .toLatin1().data());
        cn = QObject::connect(this, &clsQtListWidget::itemActivated
                             ,this, &clsQtListWidget::rptrItemActivated);
        Q_ASSERT_X(cn, metaObject()->className()
            ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                .arg(clsQtListWidget::mscszQtSignalItemActvd)
                        .toLatin1().data());
        cn = QObject::connect(this, &clsQtListWidget::itemChanged
                             ,this, &clsQtListWidget::rptrItemChanged);
        Q_ASSERT_X(cn, metaObject()->className()
            ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                .arg(clsQtListWidget::mscszQtSignalItemChgd)
                        .toLatin1().data());
        cn = QObject::connect(this, &clsQtListWidget::itemClicked
                             ,this, &clsQtListWidget::rptrItemClicked);
        Q_ASSERT_X(cn, metaObject()->className()
            ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                .arg(clsQtListWidget::mscszQtSignalItemClicked)
                        .toLatin1().data());
        cn = QObject::connect(this, &clsQtListWidget::itemDoubleClicked
                             ,this, &clsQtListWidget::rptrItemDoubleClicked);
        Q_ASSERT_X(cn, metaObject()->className()
            ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                .arg(clsQtListWidget::mscszQtSignalItemDblClicked)
                        .toLatin1().data());
        cn = QObject::connect(this, &clsQtListWidget::itemEntered
                             ,this, &clsQtListWidget::rptrItemEntered);
        Q_ASSERT_X(cn, metaObject()->className()
            ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                .arg(clsQtListWidget::mscszQtSignalItemEntered)
                        .toLatin1().data());
        cn = QObject::connect(this, &clsQtListWidget::itemPressed
                             ,this, &clsQtListWidget::rptrItemPressed);
        Q_ASSERT_X(cn, metaObject()->className()
            ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                .arg(clsQtListWidget::mscszQtSignalItemPressed)
                            .toLatin1().data());
    QObject::connect(this, &clsQtListWidget::itemSelectionChanged
                     ,[]() {
        qdbg() << "Item Selection Changed!";
    });
        cn = QObject::connect(this, &clsQtListWidget::itemSelectionChanged
                             ,this, &clsQtListWidget::rptrItemSelectionChanged);
        Q_ASSERT_X(cn, metaObject()->className()
            ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                .arg(clsQtListWidget::mscszQtSignalItemSelChgd)
                            .toLatin1().data());
        clsXMLnode* pobjWindow(mpobjNode->pobjGetWindow());
        if ( pobjWindow != nullptr ) {
            cn = QObject::connect(pobjWindow, &clsXMLnode::blankControls
                                 ,this, &clsQtListWidget::blankControl);
            Q_ASSERT_X(cn, metaObject()->className()
                      ,QString(clsXMLnode::mscszAssertCannotConnect)
                        .arg(clsXMLnode::mscszSignalBlankControls)
                                  .toLatin1().data());
        }
    

    I added the lambda connect just to fault find:

    QObject::connect(this, &clsQtListWidget::itemSelectionChanged
                     ,[]() {
        qdbg() << "Item Selection Changed!";
    });
    

    When a change the list selection I don't see this signal emitted in that neither the lambda or my repeater connected to the same signal do not get called. What could be the issue? I can see that currentItemChanged does occur.


  • Lifetime Qt Champion

    Works fine for me:

    int main(int argc, char **argv)
    {
      QApplication app(argc, argv);
    
      QListWidget w;
      w.addItem("Item 1");
      w.addItem("Item 2");
      w.addItem("Item 3");
      QObject::connect(&w, &QListWidget::itemSelectionChanged, [&]() { qDebug() << "Item Selection Changed!"; });
      w.show();
    
      return app.exec();
    }
    

    You're doing something in your code. Simplify it until the error no longer occurs or you can show us (and no - showing code with hundreds of unrelated connects is not a valid testcase where we can look on it.)



  • @Christian-Ehrlicher , Thank you, I will do just that.



  • @Christian-Ehrlicher , Found the issue, it works now after commenting out this from the constructor:

        //Use "clsQtAskBeforeChange" to prompt user for confirmation if
        //changes in child nodes before allowing change
        //ABC = Ask Before Change
        clsQtAskBeforeChange* pobjABC(new clsQtAskBeforeChange(model(), this));
        setSelectionMode(QAbstractItemView::ExtendedSelection);
        setSelectionModel(pobjABC);
        //Now the widget should be set-up, finalise set-up
        clsXMLinterface::setup();
    

    This is a bit of a work in progress as I want to prompt the user for confirmation before allowing the new selection.


Log in to reply