PDA

View Full Version : Where to use QTableView and QTableWidget



psarangi
3rd February 2014, 14:18
I went through availed post related to this.
Still I did not get much and get confused for this. Sometimes I feel better to use QTableWidget.

So could please help me out in this by giving such generic reasons to use QTableView or QTableWidget.



Thanks ...

anda_skoa
3rd February 2014, 15:38
If your use case it better served with a QTableWidget, then by all means use a QTableWidget.

Cheers,
_

d_stranz
3rd February 2014, 22:06
So could please help me out in this by giving such generic reasons to use QTableView or QTableWidget.


The major difference between the two is that QTableView requires you to use an external class derived from QAbstractItemModel with it. The rowCount(), columnCount(), and data() methods for the item model are used by the table view to tell it how to fill in and format the table cells.

In QTableWidget, the model is built-in and you simply set the count and contents of table rows and columns individually in your program by creating QTableWidgetItem instances, filling them with information, and inserting them into the table widget.

If you are displaying information from a database, then QTableView is the way to go. If you are displaying data generated internally by your program, then either one will work, whichever is easier to map onto your program's data representation.

psarangi
4th February 2014, 07:42
Thnaks for your reply ..

Yes i have a xml file, from which i need to get the data.
So i should use QTableView rather than QTableWidget.


Thanks ..

anda_skoa
4th February 2014, 10:32
Yes i have a xml file, from which i need to get the data.
So i should use QTableView rather than QTableWidget.


It mostly depends on the amount of data and whether you already have a model that interfaces with the data.

Cheers,
_

d_stranz
4th February 2014, 17:33
It mostly depends on the amount of data and whether you already have a model that interfaces with the data.

It also depends on how the XML data is organized. If it is just a series of elements with attributes, all elements of the same type, then it will map easily to a table. You can choose to create a class based on QAbstractItemModel, read the XML into that, and put it in a QTableView. Or you can read the XML and dynamically insert QTableWidgetItem instances into a QTableWidget.

If I was doing it, I would use the model / view approach because it makes a lot of things easier, like sorting or filtering the data for example. It is a lot harder to do that with QTableWidget.

On the other hand, if the XML is hierarchical, where elements have sub-elements, and so forth, then it will be difficult to match this to a table and it would be more appropriate to display this as a tree that follows the hierarchy. Use a QTreeView or QTreeWidget in that case.

psarangi
7th February 2014, 11:26
Thank you all