QTableWidget: scrolling causes data loss
-
I've put a QLineEdit into a table cell.
When the user is editing in that line edit, if he scrolls with the mouse wheel, the data in the line edit is lost.
How can this be prevented?In addition, if I try to read out the data from the line editor, e.g. by obtaining the item for the cell that was being edited, I get a null pointer:
QTableWidgetItem *item = table->itemAt(row, column); if (item==NULL) { uh oh }
-
I've put a QLineEdit into a table cell.
When the user is editing in that line edit, if he scrolls with the mouse wheel, the data in the line edit is lost.
How can this be prevented?In addition, if I try to read out the data from the line editor, e.g. by obtaining the item for the cell that was being edited, I get a null pointer:
QTableWidgetItem *item = table->itemAt(row, column); if (item==NULL) { uh oh }
-
@clarify said in QTableWidget: scrolling causes data loss:
I've put a QLineEdit into a table cell.
With what code?
-
@clarify
So if you are doing your own item delegate for editing you are responsible somewhere for creating theQTableWidgetItem
returned byQTableWidgetItem *item = table->itemAt(row, column);
.I don't know how mouse wheel behaves in a
QLineEdit
. I don't know whether the table widget or the line edit receives wheel events. And I don't know if there are any issues/gotchas here for aQTableWidget
versus aQTableView
.Try
QTableWidget::setCellWidget()
to set aQLineEdit
and compare how that behaves to your delegate. -
@clarify
So if you are doing your own item delegate for editing you are responsible somewhere for creating theQTableWidgetItem
returned byQTableWidgetItem *item = table->itemAt(row, column);
.I don't know how mouse wheel behaves in a
QLineEdit
. I don't know whether the table widget or the line edit receives wheel events. And I don't know if there are any issues/gotchas here for aQTableWidget
versus aQTableView
.Try
QTableWidget::setCellWidget()
to set aQLineEdit
and compare how that behaves to your delegate.@JonB I think I originally tried that, but it didn't work and people recommended using the delegate approach. The use of a delegate for this always seemed unseemly. The life cycle of the editor was hidden behind it and I had to just take it on faith that it would work well but it really hasn't.
-
@JonB I think I originally tried that, but it didn't work and people recommended using the delegate approach. The use of a delegate for this always seemed unseemly. The life cycle of the editor was hidden behind it and I had to just take it on faith that it would work well but it really hasn't.
@clarify said in QTableWidget: scrolling causes data loss:
and people recommended using the delegate approach.
I never said otherwise. That is preferable. What I suggested is you compare your delegate implementation's behaviour against what happens with
setCellWidget()
. If that gets it "right" you can always look at source code as to why.In addition, if I try to read out the data from the line editor, e.g. by obtaining the item for the cell that was being edited, I get a null pointer:
Where do you create the
QTableWidgetItem
? -
@clarify said in QTableWidget: scrolling causes data loss:
and people recommended using the delegate approach.
I never said otherwise. That is preferable. What I suggested is you compare your delegate implementation's behaviour against what happens with
setCellWidget()
. If that gets it "right" you can always look at source code as to why.In addition, if I try to read out the data from the line editor, e.g. by obtaining the item for the cell that was being edited, I get a null pointer:
Where do you create the
QTableWidgetItem
?@JonB I'm using the QTableWidgetItems that are automatically created by the table.
OK just now I tried switching back to using setCellWidget and that seems to work just fine (in Qt 5).
I also needed to respond to scrolling by saving the text that was entered. I had to keep a QString containing that and update it with every keystroke.
Thanks.