mattc
28th July 2010, 20:40
Hello,
I am experiencing a problem with the QListWidget::currentRowChanged signal when I remove the first element from a QListWidget. Please see my TestWidget class:
// TestWidget.h
//
#include <QWidget>
#include <QMessageBox>
#include <QListWidget>
#include <QVBoxLayout>
#include <QPushButton>
class TestWidget: public QWidget
{
Q_OBJECT
public:
TestWidget::TestWidget(QWidget* parent = 0):
QWidget(parent)
{
lst = new QListWidget();
lst->addItem("first");
lst->addItem("second");
connect(lst, SIGNAL(currentRowChanged(int)),
this, SLOT(onRowChanged(int)));
QPushButton* btn = new QPushButton("Remove first row");
connect(btn, SIGNAL(clicked(bool)),
this, SLOT(onBtnClicked(bool)));
QVBoxLayout* l = new QVBoxLayout();
l->addWidget(lst);
l->addWidget(btn);
setLayout(l);
}
private slots:
void onRowChanged(int);
void onBtnClicked(bool);
private:
QListWidget* lst;
};
inline void TestWidget::onBtnClicked(bool)
{
delete lst->takeItem(0); // triggers rowChanged
// current row number == 0 ... Correct
QMessageBox::information(this, "Debug",
QString("onBtnClicked: current row number: %1")
.arg(lst->currentRow()));
}
inline void TestWidget::onRowChanged(int i)
{
// new row number == 1 ... Why?
QMessageBox::information(this, "Debug",
QString("onRowChanged: new row number: %1").arg(i));
}
The program starts and current row is 0. Then I click the button to remove the first row and rowChanged is triggered with a value of 1. Why? Back from the rowChanged event and the current row is 0 (correct)
Any clues?
Qt 4.6.3
Visual Studio 2008 SP1
I am experiencing a problem with the QListWidget::currentRowChanged signal when I remove the first element from a QListWidget. Please see my TestWidget class:
// TestWidget.h
//
#include <QWidget>
#include <QMessageBox>
#include <QListWidget>
#include <QVBoxLayout>
#include <QPushButton>
class TestWidget: public QWidget
{
Q_OBJECT
public:
TestWidget::TestWidget(QWidget* parent = 0):
QWidget(parent)
{
lst = new QListWidget();
lst->addItem("first");
lst->addItem("second");
connect(lst, SIGNAL(currentRowChanged(int)),
this, SLOT(onRowChanged(int)));
QPushButton* btn = new QPushButton("Remove first row");
connect(btn, SIGNAL(clicked(bool)),
this, SLOT(onBtnClicked(bool)));
QVBoxLayout* l = new QVBoxLayout();
l->addWidget(lst);
l->addWidget(btn);
setLayout(l);
}
private slots:
void onRowChanged(int);
void onBtnClicked(bool);
private:
QListWidget* lst;
};
inline void TestWidget::onBtnClicked(bool)
{
delete lst->takeItem(0); // triggers rowChanged
// current row number == 0 ... Correct
QMessageBox::information(this, "Debug",
QString("onBtnClicked: current row number: %1")
.arg(lst->currentRow()));
}
inline void TestWidget::onRowChanged(int i)
{
// new row number == 1 ... Why?
QMessageBox::information(this, "Debug",
QString("onRowChanged: new row number: %1").arg(i));
}
The program starts and current row is 0. Then I click the button to remove the first row and rowChanged is triggered with a value of 1. Why? Back from the rowChanged event and the current row is 0 (correct)
Any clues?
Qt 4.6.3
Visual Studio 2008 SP1