![]() |
Home · Overviews · Examples |
The QTableWidget class provides an item-based table view with a default model. More...
Inherits QTableView.
The QTableWidget class provides an item-based table view with a default model.
Table widgets provide standard table display facilities for applications. The items in a QTableWidget are provided by QTableWidgetItem.
If you want a table that uses your own data model you should use QTableView rather than this class.
Table widgets can be constructed with the required numbers of rows and columns:
tableWidget = new QTableWidget(12, 3, this);
Alternatively, tables can be constructed without a given size and resized later:
tableWidget = new QTableWidget(this); tableWidget->setRowCount(10); tableWidget->setColumnCount(5);
Items are created ouside the table (with no parent widget) and inserted into the table with setItem:
QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg( (row+1)*(column+1))); tableWidget->setItem(row, column, newItem);
If you want to enable sorting in your table widget, do so after you have populated it with items, otherwise sorting may interfere with the insertion order (see setItem for details).
Tables can be given both horizontal and vertical headers. The simplest way to create the headers is to supply a list of strings to the setHorizontalHeaderLabels and setVerticalHeaderLabels functions. These will provide simple textual headers for the table's columns and rows. More sophisticated headers can be created from existing table items that are usually constructed outside the table. For example, we can construct a table item with an icon and aligned text, and use it as the header for a particular column:
QTableWidgetItem *cubesHeaderItem = new QTableWidgetItem(tr("Cubes")); cubesHeaderItem->setIcon(QIcon(QPixmap(":/Images/cubed.png"))); cubesHeaderItem->setTextAlignment(Qt::AlignVCenter);
The number of rows in the table can be found with rowCount, and the number of columns with columnCount. The table can be cleared with the clear function.
![]() | ![]() | ![]() |
A Windows XP style table widget. | A Macintosh style table widget. | A Plastique style table widget. |
See also QTableWidgetItem, QTableView, and Model/View Programming.
Copyright © 2007 Trolltech | Trademarks | Qt Jambi 4.3.2_01 |