View Full Version : QAbstractItemModel / QStyledItemDelegate howto update data on value changed?

24th June 2011, 08:23
Hi there,
I have QAbstractItemModel and displaying its data in a QTableView.
A derived QStyledItemDelegate class creates an QSpinBox to edit values in
column 1.
A QSpinbox on some other place on the widget where the QTableView is nested,
shows the same data eg. (2/1), the QSpinbox is connected through QWidgetMapper.
Actually it works fine: I can change both value and they update each other if
I change the focus.
But thats the point! The changed values have to immediately update the model
data. How can I force that the valueChange() signal update the model data.

Therefore if I insert a number directly in the spinbox and press enter,
the number jumps back to the old value. Only the up/down button works to edit
the value.

Thanks for help

Santosh Reddy
24th June 2011, 09:00
You should be connecting the outer / other QSpinBox to QAbstractItemModel, and not to the QTableView's or QStyledItemDelegate's QSpinBox, are you doing it this way?

24th June 2011, 10:26

otpTableView = QTableView(this)
otpTableView.setItemDelegate( OtpDelegate_C(this) )

mapper_r1 = QtGui.QDataWidgetMapper(this)
mapper_r1.addMapping(label_row1, 0, "text")
mapper_r1.addMapping(spinbox_row1, 1)

Anyway the problem is that the data from the editor will be send to the model only after the editor lost the focus. I think that is normal behavior for models indeed.
But if you connect eg. a slider with spinbox, you can see intstandly the changing values. That`s what I have to achieve.

Santosh Reddy
24th June 2011, 11:07
In that case use QSpinBox::valueChanged() signal

24th June 2011, 21:49
Ups, some post get lost somewhere ???
Thanks Santosh, but I dosn`t connect anything it goes autmaticaly by
Maybe I have to derive QTableView and QDataWidgetMapper to force some signals

25th June 2011, 12:28
I improved the code to

mapper_r2 = QtGui.QDataWidgetMapper(this)
mapper_r2.addMapping(label_row2, 0, "text")
mapper_r2.addMapping(spinbox_row2, 1)
spinbox_row2, SIGNAL(valueChanged(), otpmodel, SLOT(dataChanged()))
mapper_r2.setSubmitPolicy(QtGui.QDataWidgetMapper. AutoSubmit)

But as expected, the value isn`t set to the model already , so an dataChanged() isn`t enough :(

Santosh Reddy
25th June 2011, 12:35
There is no such SLOT in QAbstractItemModel