PDA

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



moviemax
24th June 2011, 07: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, 08: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?

moviemax
24th June 2011, 09:26
Yes:


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


mapper_r1 = QtGui.QDataWidgetMapper(this)
mapper_r1.setModel(otpmodel)
mapper_r1.addMapping(label_row1, 0, "text")
mapper_r1.addMapping(spinbox_row1, 1)
mapper_r1.setCurrentIndex(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.
Thanks

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

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

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


mapper_r2 = QtGui.QDataWidgetMapper(this)
mapper_r2.setModel(otpmodel)
mapper_r2.addMapping(label_row2, 0, "text")
mapper_r2.addMapping(spinbox_row2, 1)
mapper_r2.setCurrentIndex(1)
connect(
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, 11:35
There is no such SLOT in QAbstractItemModel