Solved Can we assign a model to a QTableWidget even though its setModel is private?
-
Hi,
I love the view/model architecture and I think Qt has done a great job following it. I have seen examples of views which get a model associated, but what about QTableWidget (and other Widgets), do they participate in the view/model architecture as well?
Thanks!!
Juan -
@jdent
Hello,
Short answer is no. If you wish to assign a model, you're simply supposed to useQTableView
. You could do some hacking to expose the method but why would you want to do that? In the end putting a table view with aQStandardItemModel
should behave exactly asQTableWidget
.Kind regards.
-
@kshegunov said in Can we assign a model to a QTableWidget even though its setModel is private?:
but why would you want to do that?
Sadly, because Qt Designer design-time support for entering row/column values/headers is there for
QTableWidget
and completely absent forQTableView
... :(Having carefully Designer-created all my headers' texts, fonts, widths etc. on a
QTableWidget
, I realize I am happy with aQStandardItemModel
model but I have my own subclass of that to do what I need code-wise but cannot use mine to back the table widget... '(Which leaves me with alternatives from here:
-
Don't want to hack to expose the method. At least, not if it means any changes/recompiling of Qt, unless your "hack" can be done without that??
-
Make editing from the
QTableWidget
also set data in my own "shadow" model? Not great. -
Use the design-time
QTableWidget
just as a "template" from which I create a run-timeQTreeView
with my own model? -
Make what I wanted to do in my own
QStandardModel
-derived model work off a plainQStandardModel
, if I can manage without any overriding etc.? I think I'll have a go at this first as it seems like the least work, if it does work....
-
-
@JonB said in Can we assign a model to a QTableWidget even though its setModel is private?:
- Don't want to hack to expose the method. At least, not if it means any changes/recompiling of Qt, unless your "hack" can be done without that??
Nope, you need to go into the actual implementation.
- Make editing from the
QTableWidget
also set data in my own "shadow" model? Not great.
Not great not terrible, as Dyatlov'd said.
- Use the design-time
QTableWidget
just as a "template" from which I create a run-timeQTreeView
with my own model?
Yep, that'd be the preferred and actually one of the few viable methos.
- Make what I wanted to do in my own
QStandardModel
-derived model work off a plainQStandardModel
, if I can manage without any overriding etc.? I think I'll have a go at this first as it seems like the least work, if it does work....
If you can do that, then you can slap the standard model onto a view. Another thing is you could put a proxy model on top of your model/the standard model to fit one structure, to the structure the view expects.
-
@kshegunov
As always, thanks for your comments :) I thought about it a lot, and went forUse the design-time
QTableWidget
just as a "template" from which I create a run-timeQTreeView
with my own modelThis costs me a smidgen of wasted memory and a smidgen of increased startup time, both of which niggle at me... :( !
-
Hello @JonB,
I was having a similar problem with my design, I am reading data from a sqlite database usingQSqlQueryModel
and showing it withQTableView
, but then I cannot edit the model to update locally part of the data and thbefore writing back to the database. I have thought about use aQTableWidget
directly and copy data from theQSqlQueryModel
to it item by item... But then I have read this conversation and I am wondering what exactly can you do with theQTreeView
in your case, could you explain a little bit more if it can be suitable for my case?
Thanks for you help.