PDA

View Full Version : QTableWidget - problem when editing multiple items



xportation
18th October 2011, 19:09
Hi,

The QTableWidget is not notifying a change when an item is "changed" for the same value.
It seems silly, but it's important when editing multiple items.



QTableWidgetTest::QTableWidgetTest(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
ui.setupUi(this);
//...
connect(ui.tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(multipleItemsChanged(QTableWidgetItem*)));
}

//...

void QTableWidgetTest::multipleItemsChanged( QTableWidgetItem *item )
{
ui.tableWidget->blockSignals(true);
QList<QTableWidgetItem*> selectedItems = ui.tableWidget->selectedItems();
foreach(QTableWidgetItem* selectItem, selectedItems)
{
selectItem->setText(item->text());
}
ui.tableWidget->blockSignals(false);
}


Sample project (http://dl.dropbox.com/u/18906327/QTableWidgetTest.zip)
It's important to use the same value as the last item selected.
If you test with only one item, the result is the same.


Does somebody know another way to solve this?

norobro
18th October 2011, 23:53
Try connecting the signal QAbstractItemDelegate::closeEditor(QWidget *) (http://doc.qt.nokia.com/latest/qabstractitemdelegate.html#closeEditor) to your slot. You'll need to change the signature of the slot and cast "QWidget *" to "QLineEdit *" to get the text.

xportation
20th October 2011, 19:20
Thanks norobro, it works fine and I'm using it.
I have tested another way by making this little "good blood" hack:



class MyTableWidgetItem : public QTableWidgetItem
{
public:
explicit MyTableWidgetItem(const QString & text, int type = Type) : QTableWidgetItem(text, type) {}

virtual void setData(int role, const QVariant &value)
{
if (QAbstractTableModel *model = (this->tableWidget() ? qobject_cast<QAbstractTableModel*>(this->tableWidget()->model()) : 0)) {
model->blockSignals(true);
QVariant nullValue;
QTableWidgetItem::setData(role,nullValue);
model->blockSignals(false);
}
QTableWidgetItem::setData(role,value);
}
};

//--

QTableWidgetTest::QTableWidgetTest(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
ui.setupUi(this);

int value= 0;
for (int row= 0; row < table->rowCount(); row++) {
for (int col= 0; col < table->columnCount(); col++) {
QTableWidgetItem *item= new MyTableWidgetItem(QString::number(value));
table->setItem(row,col,item);
value++;
}
}

connect(ui.tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(multipleItemsChanged(QTableWidgetItem*)));
}

xportation
21st October 2011, 10:38
[Solved]
this thread is solved