Qt Class之QTableWidget
文档名称
Qt Class之QTableWidget
创建时间
2012-9-7
修改时间
2012-9-7
创建人
Baifx
简介(收获)
QTableWidget、QTableWidgetItem、QTableView
一、综述。
1、继承关系。
QTableWidget类:
QHeaderView类:
2、官方描述。
QTableWidget
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 useQTableViewrather 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 withsetItem():
(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 (seesetItem() 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 thesetHorizontalHeaderLabels() andsetVerticalHeaderLabels() 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 = newQTableWidgetItem(tr("Cubes"));
cubesHeaderItem->setIcon(QIcon(QPixmap(":/Images/cubed.png")));
cubesHeaderItem->setTextAlignment(Qt::AlignVCenter);
The number of rows in the table can be found withrowCount(), and the number of columns withcolumnCount(). The table can be cleared with theclear() function.
QTableWidgetItem
The QTableWidgetItem class provides an item for use with theQTableWidgetclass.
Table items are used to hold pieces of information for table widgets. Items usually contain text, icons, or checkboxes
The QTableWidgetItem class is a convenience class that replaces the QTableItem class in Qt 3.It provides an item for use with the QTableWidget class.
Top-level items are constructed without a parent then inserted at the position specified by a pair of row and column numbers:
QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(