Solved QTableView: How does one prohibit changing rows when data is lacking in one of the columns?
-
I can connect to the
currentRowChanged
signal of the table view'sQItemSelectionModel
, but this is only emitted AFTER the user clicks on a cell in a different row or column of the table view. If the new column is in the same row being edited, this is OK; but I would like to cancel any row change if data is missing from one of the columns in the row currently being edited.The underlying model for the table view has two columns which can both be edited. Initially, the table can be empty. The user can add a new row by clicking a button, and rows can be removed by clicking a different button. Both columns in the new row should then be filled in by the user before adding another row. I can prohibit adding a new row by code running in the slot connected to the button's
clicked
signal, checking the data in the existing row, but the user can also click on a previous row without completing the new row. This kind of row change should also be prevented.What is the standard approach to this presumably very common scenario?
-
@Robert-Hairgrove
Since I cannot see how to do this either: would having yourcurrentRowChanged
slot check whether itsprevious
row is incompletely-edited and callselect(previous)
be viable?The nearest I can find to your (reasonable) question is https://stackoverflow.com/questions/50218443/qtreeview-how-to-abort-selection-change. Have a read through, I think you'll see it's taking my suggested approach, see how it relates to yours. Similarish https://stackoverflow.com/questions/54233639/qtableview-prevent-a-user-from-navigating-away-from-a-spesific-row. It looks like you cannot cancel the change, you have to revert to the previous (yuck), and then they describe workarounds to make that change back happen just after it has changed row, since apparently it will not work if called while still inside the slot....
-
What model are you using?
I would discourage you to implement what you describe as it may become very frustrating for the user. Imagine if the guy wants to copy a value from a cell above to fill in the data of a new row -
@VRonin
Agreed. Then how can you put aQTableView
into "row edit mode", and not allow it to leave that until all columns are valid? -
@JonB I wouldn't reinvent the wheel and just do what MS Access and Libre Office do. See Insert Proxy Model of this library
-
Thanks, everyone. I think I will do this differently. The table view is actually part of a custom editor which is opened on a cell in another table view. I will do validation only when I commit the data as an entire table. Seems like a much cleaner design and more simple to implement.