Sorry for the bulky title.
I have a class containing a QListWidget
.
I connected it's itemSelectionChanged()
signal to a custom slot.
When I call QListWidget::clear()
, the slot gets called (as expected) but a call to QListWidget::count()
in this slot returns the number of items the QListWidget
had before.
A call to count()
right after the call to clear()
(when the signal was processed as described) returns the correct number 0
.
I prepared a complete demo project. Most important is this source file:
#include "ListWidgetTest.hpp"
#include "ui_ListWidgetTest.h"
#include <QDebug>
ListWidgetTest::ListWidgetTest(QWidget* parent)
: QWidget(parent), ui(new Ui::ListWidgetTest)
{
ui->setupUi(this);
for (int i = 0; i < 5; ++i) {
QListWidgetItem* item = new QListWidgetItem(QString("Item %1").arg(i));
ui->listWidget->addItem(item);
}
QObject::connect(ui->pushButton, SIGNAL(clicked()),
this, SLOT(clearList()));
QObject::connect(ui->listWidget, SIGNAL(itemSelectionChanged()),
this, SLOT(selectionChanged()));
}
ListWidgetTest::~ListWidgetTest()
{
delete ui;
}
void ListWidgetTest::clearList()
{
qDebug() << "void ListWidgetTest::clearList()";
ui->listWidget->clear();
qDebug() << "clearList: ui->listWidget->count() is " << ui->listWidget->count();
}
void ListWidgetTest::selectionChanged()
{
qDebug() << "void ListWidgetTest::selectionChanged()";
qDebug() << "selectionChanged: ui->listWidget->count() is " << ui->listWidget->count();
}
Output
void ListWidgetTest::clearList()
void ListWidgetTest::selectionChanged()
selectionChanged: ui->listWidget->count() is 5
clearList: ui->listWidget->count() is 0
What happens
- The list gets populated.
- Every click on an item calls
selectionChanged()
- A click on the button calls
clearList()
- The call to
QListWidget::clear()
also emits the signal and the slot gets called - The number of items has not changed yet