Important: Please read the Qt Code of Conduct -

QListWidget::itemSelectionChanged() fired twice with keyboard arrow

  • HI,

    I'm using a QListWidget to select files within a list, on selection I read this file, in case of error I clear all selections and popup an error.

    Everything works fine using only mouse, but when using keyboard arrows, on a bad file, the signal fire twice.

    This is annoying since the error pops up twice.

    Is there any way to pop the error only once in this case?

    Code to reproduce behavior :

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMessageBox>
    #include <QMainWindow>
    #include <QListWidget>

    class MainWindow : public QMainWindow


    MainWindow(QWidget *parent = 0)
        : QMainWindow(parent)
        // Window title
        setWindowTitle("My Widget");
        // Creation of the QListWidgets
        _listWidget = new QListWidget;
        // Selection mode
        // Fill the table
        for(int i = 1; i <= 10; i++)
        // Connect signals and slots
        connect(_listWidget, SIGNAL(itemSelectionChanged()),
                this, SLOT(onSelect()));
        // Set central widget
    ~MainWindow() {}

    public slots:

    void onSelect()
        QList<QListWidgetItem*> itemsList = _listWidget->selectedItems();
        foreach (QListWidgetItem* item, itemsList)
            // If the item is not valid, I want to unselect it
                // Block signal for fire error only one
                QMessageBox::critical(0, "Error", "Item Not Valid");


    bool isValid(QListWidgetItem* item)
        int number = item->text().toInt();
        return (number%3 != 0);
    QListWidget *_listWidget;


    #endif // MAINWINDOW_H

    #include <MainWindow.h>

    #include <QApplication>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);

    MainWindow w;;
    return a.exec(&#41;;


Log in to reply