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. QListWidget::itemSelectionChanged() fired twice with keyboard arrow
Qt 6.11 is out! See what's new in the release blog

QListWidget::itemSelectionChanged() fired twice with keyboard arrow

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 899 Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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 :

    MainWindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

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

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:

    MainWindow(QWidget *parent = 0)
        : QMainWindow(parent)
    {
        // Window title
        setWindowTitle("My Widget");
        setMinimumSize(400,500);
    
        // Creation of the QListWidgets
        _listWidget = new QListWidget;
    
        // Selection mode
        _listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    
        // Fill the table
        for(int i = 1; i <= 10; i++)
            _listWidget->addItem(QString::number(i));
    
        // Connect signals and slots
        connect(_listWidget, SIGNAL(itemSelectionChanged()),
                this, SLOT(onSelect()));
    
        // Set central widget
        setCentralWidget(_listWidget);
    }
    
    ~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
            if(!isValid(item))
            {
                // Block signal for fire error only one
                _listWidget->blockSignals(true);
                _listWidget->clearSelection();
                _listWidget->setCurrentItem(0);
                _listWidget->blockSignals(false);
                QMessageBox::critical(0, "Error", "Item Not Valid");
            }
        }
    }
    

    private:

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

    };

    #endif // MAINWINDOW_H
    @

    main.cpp
    @
    #include <MainWindow.h>

    #include <QApplication>

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

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

    }
    @

    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