QTableWidget, how to pick up changes?
-
I have a QTableWidget, it contains fields form a database. I'm sure there is a better way to do what I am trying to do....
I have the following JSON:
{"cid":"demoTable" ,"columns":[{"intPri":{"align":"right" ,"hdg":"ID" ,"width":80}} ,{"vcFirstName":{"align":"left" ,"hdg":"First Name" ,"width":100}} ,{"vcSurName":{"align":"left" ,"hdg":"Surname" ,"width":100}} ,{"vcAddress":{"align":"left" ,"hdg":"Address" ,"width":340}} ,{"vcSex":{"align":"center" ,"hdg":"Sex" ,"width":80}}] ,"alias":"s1" ,"module":"mdSQL" ,"command":"query" ,"key":"intPri" ,"handler":"simon.js@setupTable" ,"sql":"SELECT" + " intPri, vcFirstName, vcSurName, vcAddress, vcSex " + "FROM" + " test.tblForm ORDER BY vcSurName, vcFirstName"}
My code processes the JSON and creates the table then populates it with the results after executing the SQL query.
When I create the table an initially populate it I call:
const QSignalBlocker blocker(*pobjTable);
To prevent any signals. I then create a property with keyed items that contains the original data, each item in the JSON is keyed using the primary key (intPri) and the data field name, the value associate with this is the value as read from the database.
When I edit the data in the table, a signal is emitted when the cellChanged signal is emitted.
This is where I am stuck, is it possible for me to get the data item from the table using the primary key and field name ?
I can only see item by row and column which doesn't help me since rows and columns can change. If there is a better way to check for changes can someone please educate me ?
-
@SPlatten
I don't follow: when you have a row and column you can look in the model at that index and retrieve whatever value you want/need. If you e.g. put the key field value in the row somewhere you can get at that.You realise
QTableWidget
is only a convenience widget aimed at simple cases? You have no control over the model it uses. Knowing you and your requirements, sooner or later wouldn't you be much better off with aQTableView
and your own model, where you can do much more?If all you want to do is access a SQL database you would be better of with a
QSql...
model. You might also impose a proxy model to add further stuff if required.QTableWidget
model is not ideal. -
@JonB , I originally started out with using row and column but they can't be used when sorting is enabled, because the data you think they point to changes.
QTableWidget has a function which has several variants item one of these allows you to pass the row and column: https://doc.qt.io/qt-6/qtablewidget.html#item
If you are trying to locate a specific cell in the table, it can move around depending on the sorting if enabled....
-
@SPlatten
What can I say? Don't useQTableWidget
. With your own model (and view) you can do things like access the row, column in the base, unsorted model, assuming the sorting is done with a proxy model likeQSortFilterProxyModel
. Anything that can be done in aQTableWidget
can be done withQTableView
+ your own model, and only gives more flexibility; the reverse is not true. I have indicated that given your question/requirement history I would be "surprised" ifQTableWidget
is the best choice for you. -
@SPlatten
I'm not one for tutorials or guides. I just do and read and see how it goes.All of this is just Qt's model/view architecture. Have you at least read https://doc.qt.io/qt-6/model-view-programming.html and https://doc.qt.io/qt-6/modelview.html? What about https://doc.qt.io/qt-6/qtwidgets-itemviews-customsortfiltermodel-example.html for understand how filtering/sorting a model works (you may only need to use existing
QSortFilterProxyModel
).A
QTableWidget
is merely derived from aQTableView
, and it adds in its own "hidden" model. You can do the UI stuff fromQTableView
and provide your own model to it for the backend.