0

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();
}
Giacogiac
  • 189
  • 1
  • 11
  • I don't know why the signal is emitted twice. Use the debugger, set a breakpoint and find out where the calls come from. – Silicomancer Oct 18 '14 at 07:51
  • The emit comes from Qt, my code don't fire any signals, I can't put breakpoint in Qt code. – Giacogiac Oct 20 '14 at 07:49
  • Yes, look into the Qt sources using the call stack and try to find out why these emits are done. – Silicomancer Oct 20 '14 at 07:56
  • I don't know how to do that, the Qt code that is firing the signal is already compiled and the callstack just take me through some disassembled code – Giacogiac Oct 21 '14 at 15:51

1 Answers1

0

There is an easy workround for your dialog problem. Instead of using QMessageBox::critical() you can create a dialog on the heap that is automatically destroyed when closed, store its address in a QPointer and check the pointer before opening the dialog again.

Silicomancer
  • 8,604
  • 10
  • 63
  • 130
  • This would be if I want my dialog to apear once in all the application life, but it can apper multiple times if the user keep selecting bad files, i just do'nt want it to appear twice for a single action. – Giacogiac Oct 20 '14 at 07:46
  • No. That workround prevents the dialog from appearing twice at the same time. Not twice in the application life time. Why do you think that? – Silicomancer Oct 20 '14 at 07:55
  • The second Dialog appears after the first one is closed. It doesn't appears twice at the same time but twice in a Row. – Giacogiac Oct 21 '14 at 15:46