Important: Please read the Qt Code of Conduct -

QTableWidget populating

  • Hi guys,

    Back with another problem I'm afraid! I'm having terrible trouble filling a row in a QTableWidget.

    The idea is simply that I load a file using the QFileDialog class which dumps its data into a QStringList. From here I need to get this data into the QTableWidget by creating a new row, then filling 4 seperate columns with data.

    Could somebody please point me in the right direction?


  • Oh! Never mind solved it myself! Thanks Anways :-D

  • You should build a model inherited from QAbstractTableModel and set it as a basemodel for the QTableWidget.

  • If you use models, you should use QTabelView, not QTableWidget. And from my point of view, the model-view approach is much better than the QTableWidget approach.

  • AFAIK QTableWidget inherits QTableView

  • [quote author="VCsala" date="1292523877"]AFAIK QTableWidget inherits QTableView[/quote]

    Indeed it does. You can consider the QWidget classes to be exactly like the QView classes, but with a builtin model (which closely resembles QStandardItemModel) and helper classes to manage this models (the QWidgetItem classes). Using a custom model with the QWidget classes is pointless, since you won't be able to use their helper classes to manage the model.

    [quote author="VCsala" date="1292521276"]You should build a model inherited from QAbstractTableModel and set it as a basemodel for the QTableWidget.[/quote]

    s/QTableWidget/QTableView/, see above.

  • Guys, you win :) Anyhow, thanks for the correction, I am still have a lot to learn.

  • But the *widget classes with their helpers and standard models are for the general purpos and therefore not optimized. So, in fact, if you have a growing app, it might happen, that you need more code in the end then with a custom model / view approach.

  • The decision is not so easy. We use both the QxxWidgets and the QxxViews in our code. Sometimes it's more convenient to have a QxxWidgetItem subclass to add the functionality, especially if you only want to add a small improvement and are satisfied with the standard implementation for the rest. Although I must admit that we generally do not have big amounts of data in the Widgets, so speed and space optimization is not a big issue for us.

  • Ok, so there's a big difference, we normally have much data or possibly much data, as we visualize industry values....

Log in to reply