Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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


  • Qt Champions 2017

    @jdent
    Hello,
    Short answer is no. If you wish to assign a model, you're simply supposed to use QTableView. 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 a QStandardItemModel should behave exactly as QTableWidget.

    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 for QTableView... :(

    Having carefully Designer-created all my headers' texts, fonts, widths etc. on a QTableWidget, I realize I am happy with a QStandardItemModel 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-time QTreeView with my own model?

    • Make what I wanted to do in my own QStandardModel-derived model work off a plain QStandardModel, 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....


  • Qt Champions 2017

    @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-time QTreeView 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 plain QStandardModel, 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 for

    Use the design-time QTableWidget just as a "template" from which I create a run-time QTreeView with my own model

    This costs me a smidgen of wasted memory and a smidgen of increased startup time, both of which niggle at me... :( !


Log in to reply