// TestWidget.h
//
#include <QWidget>
#include <QMessageBox>
#include <QListWidget>
#include <QVBoxLayout>
#include <QPushButton>
{
Q_OBJECT
public:
TestWidget
::TestWidget(QWidget* parent
= 0): {
lst->addItem("first");
lst->addItem("second");
connect(lst, SIGNAL(currentRowChanged(int)),
this, SLOT(onRowChanged(int)));
connect(btn, SIGNAL(clicked(bool)),
this, SLOT(onBtnClicked(bool)));
l->addWidget(lst);
l->addWidget(btn);
setLayout(l);
}
private slots:
void onRowChanged(int);
void onBtnClicked(bool);
private:
};
inline void TestWidget::onBtnClicked(bool)
{
delete lst->takeItem(0); // triggers rowChanged
// current row number == 0 ... Correct
QString("onBtnClicked: current row number: %1") .arg(lst->currentRow()));
}
inline void TestWidget::onRowChanged(int i)
{
// new row number == 1 ... Why?
QString("onRowChanged: new row number: %1").
arg(i
));
}
// 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));
}
To copy to clipboard, switch view to plain text mode
Bookmarks