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 :
MainWindow.h
@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMessageBox>
#include <QMainWindow>
#include <QListWidget>class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
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();
}
@