View Full Version : QTableView versus QTableWidget

24th May 2006, 20:02
I need to develop a spreadsheet widget with sorting capabilities on columns, where the presentation of the data inside each cell would depend on the type of data, and I would like to do some operations on a selection of cells (calculate the sum of the cells' values for instance).
QTableWidget sounds a good option but the the Model/View Programming documentaiton warns against:
"A number of convenience classes are derived from the standard view classes for the benefit of applications that rely on Qt's item-based item view and table classes. They are not intended to be subclassed, but simply exist to provide a familiar interface to the equivalent classes in Qt 3. Examples of such classes include QListWidget, QTreeWidget, and QTableWidget; these provide similar behavior to the QListBox, QListView, and QTable classes in Qt 3.
These classes are less flexible than the view classes, and cannot be used with arbitrary models. We recommend that you use a model/view approach to handling data in item views unless you strongly need an item-based set of classes. " Pretty strong words!
Using the QTableView road, I used the QSortFilterProxyModel to sort the columns and it works fine. How should I go with the presentation of the cell's content (an equivalent to QTableWidgetItem would be nice) and what about the handling of selection (somethings like QTableWidgetSelectionRange would help)?
Many thanks in advance

24th May 2006, 20:13
You have to implement your own model for that (derived from QAbstractTableModel) or at least use QStandardItemModel.

24th May 2006, 20:51
You definatly want the Model-View classes. You will implement the following:

QAbstractItemModel which will interface with your own data structures. This will contain only data. If you need only lightweight data then the QStandarditemModel might just work. That has internal data stuctures to hold data.

QAbstractItemDelegate. This is where you can define drawing and editing for each cell

The above is analagous to the old TableItem where data, drawing and editing were stuffed into one class.

You will use
QItemSelectionModel for handle selections.