QTableWidget Class Reference

[ QtGui module]

The QTableWidget class provides an item-based table view with a default model. 更多...

继承 QTableView .

方法

Qt Signals


详细描述

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 .

若希望表格使用您自己的数据模型,应使用 QTableView rather than this 类。

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 到 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 () 函数。

Screenshot of a Windows XP style table widget Screenshot of a Macintosh style table widget Screenshot of a Plastique style table widget
A Windows XP style table 小部件。 A Macintosh style table 小部件。 A Plastique style table 小部件。

方法文档编制

QTableWidget.__init__ ( self , QWidget   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

创建新的表格视图,采用给定 parent .

QTableWidget.__init__ ( self , int  rows , int  columns , QWidget   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

创建新的表格视图,采用给定 rows and columns ,和采用给定 parent .

QWidget QTableWidget.cellWidget ( self , int  row , int  column )

返回单元格显示 Widget,在给定 row and column .

注意: 表格拥有 Widget 的所有权。

该函数在 Qt 4.1 引入。

另请参阅 setCellWidget ().

QTableWidget.clear ( self )

This method is also a Qt slot with the C++ signature void clear() .

Removes all items in the view. This will also remove all selections. The table dimensions stay the same.

QTableWidget.clearContents ( self )

This method is also a Qt slot with the C++ signature void clearContents() .

Removes all items not in the headers from the view. This will also remove all selections. The table dimensions stay the same.

该函数在 Qt 4.2 引入。

QTableWidget.closePersistentEditor ( self , QTableWidgetItem   item )

关闭持久编辑器为 item .

另请参阅 openPersistentEditor ().

int QTableWidget.column ( self , QTableWidgetItem   item )

返回列号为 item .

int QTableWidget.columnCount ( self )

int QTableWidget.currentColumn ( self )

返回当前项的列号。

另请参阅 currentRow () 和 setCurrentCell ().

QTableWidgetItem QTableWidget.currentItem ( self )

返回当前项。

另请参阅 setCurrentItem ().

int QTableWidget.currentRow ( self )

返回当前项的行号。

另请参阅 currentColumn () 和 setCurrentCell ().

QTableWidget.dropEvent ( self , QDropEvent   event )

重实现自 QWidget.dropEvent ().

bool QTableWidget.dropMimeData ( self , int  row , int  column , QMimeData   data , Qt.DropAction   action )

处理 data supplied by a drag and drop operation that ended with the given action 以给定 row and column . Returns true if the data and action can be handled by the model; otherwise returns false.

另请参阅 supportedDropActions ().

QTableWidget.editItem ( self , QTableWidgetItem   item )

开始编辑 item 若它可编辑。

bool QTableWidget.event ( self , QEvent   e )

重实现自 QObject.event ().

list-of-QTableWidgetItem QTableWidget.findItems ( self , QString  text , Qt.MatchFlags   flags )

Finds items that matches the text using the given flags .

QTableWidgetItem QTableWidget.horizontalHeaderItem ( self , int  column )

Returns the horizontal header item for column, column , if one has been set; otherwise returns 0.

另请参阅 setHorizontalHeaderItem ().

QModelIndex QTableWidget.indexFromItem ( self , QTableWidgetItem   item )

返回 QModelIndex assocated with the given item .

QTableWidget.insertColumn ( self , int  column )

This method is also a Qt slot with the C++ signature void insertColumn(int) .

把空列插入表格,位置 column .

QTableWidget.insertRow ( self , int  row )

This method is also a Qt slot with the C++ signature void insertRow(int) .

把空行插入表格,位置 row .

bool QTableWidget.isItemSelected ( self , QTableWidgetItem   item )

bool QTableWidget.isSortingEnabled ( self )

QTableWidgetItem QTableWidget.item ( self , int  row , int  column )

返回项为给定 row and column if one has been set; otherwise returns 0.

另请参阅 setItem ().

QTableWidgetItem QTableWidget.itemAt ( self , QPoint   p )

Returns a pointer to the item at the given point ,或 returns 0 if point is not covered by an item in the table 小部件。

另请参阅 item ().

QTableWidgetItem QTableWidget.itemAt ( self , int  ax , int  ay )

Returns the item at the position equivalent to QPoint ( ax , ay ) in the table widget's coordinate system, or returns 0 if the specified point is not covered by an item in the table widget.

另请参阅 item ().

QTableWidgetItem QTableWidget.itemFromIndex ( self , QModelIndex   index )

返回指针指向 QTableWidgetItem assocated with the given index .

QTableWidgetItem QTableWidget.itemPrototype ( self )

Returns the item prototype used by the table.

另请参阅 setItemPrototype ().

list-of-QTableWidgetItem QTableWidget.items ( self , QMimeData   data )

Returns a list of pointers to the items contained in the data object. If the object was not created by a QTreeWidget in the same process, the list is empty.

QMimeData QTableWidget.mimeData ( self , list-of-QTableWidgetItem  items )

QMimeData result

Returns an object that contains a serialized description of the specified items . The format used to describe the items is obtained from the mimeTypes () 函数。

If the list of items is empty, 0 is returned rather than a serialized empty list.

QStringList QTableWidget.mimeTypes ( self )

Returns a list of MIME types that can be used to describe a list of tablewidget items.

另请参阅 mimeData ().

QTableWidget.openPersistentEditor ( self , QTableWidgetItem   item )

Opens an editor for the give item . The editor remains open after editing.

另请参阅 closePersistentEditor ().

QTableWidget.removeCellWidget ( self , int  arow , int  acolumn )

Removes the widget set on the cell indicated by row and column .

该函数在 Qt 4.3 引入。

QTableWidget.removeColumn ( self , int  column )

This method is also a Qt slot with the C++ signature void removeColumn(int) .

移除列 column and all its items from the table.

QTableWidget.removeRow ( self , int  row )

This method is also a Qt slot with the C++ signature void removeRow(int) .

移除行 row 及表格中的所有项。

int QTableWidget.row ( self , QTableWidgetItem   item )

Returns the row for the item .

int QTableWidget.rowCount ( self )

QTableWidget.scrollToItem ( self , QTableWidgetItem   item , QAbstractItemView.ScrollHint   hint  = QAbstractItemView.EnsureVisible)

This method is also a Qt slot with the C++ signature void scrollToItem(const QTableWidgetItem*,QAbstractItemView::ScrollHint = QAbstractItemView.EnsureVisible) .

Scrolls the view if necessary to ensure that the item is visible. The hint parameter specifies more precisely where the item should be located after the operation.

list-of-QTableWidgetItem QTableWidget.selectedItems ( self )

Returns a list of all selected items.

This function returns a list of pointers to the contents of the selected cells. Use the selectedIndexes () function to retrieve the complete selection including empty cells.

另请参阅 selectedIndexes ().

list-of-QTableWidgetSelectionRange QTableWidget.selectedRanges ( self )

Returns a list of all selected ranges.

另请参阅 QTableWidgetSelectionRange .

QTableWidget.setCellWidget ( self , int  row , int  column , QWidget   widget )

widget argument has it's ownership transferred to Qt.

设置给定 widget to be displayed in the cell in the given row and column , passing the ownership of the widget to the table.

If cell widget A is replaced with cell widget B, cell widget A will be deleted. For example, in the code snippet below, the QLineEdit object will be deleted.

 setCellWidget(index, new QLineEdit);
 ...
 setCellWidget(index, new QTextEdit);
			

该函数在 Qt 4.1 引入。

另请参阅 cellWidget ().

QTableWidget.setColumnCount ( self , int  columns )

QTableWidget.setCurrentCell ( self , int  row , int  column )

Sets the current cell to be the cell at position ( row , column ).

Depending on the current selection mode , cell may also be selected.

该函数在 Qt 4.1 引入。

另请参阅 setCurrentItem (), currentRow (),和 currentColumn ().

QTableWidget.setCurrentCell ( self , int  row , int  column , QItemSelectionModel.SelectionFlags   command )

Sets the current cell to be the cell at position ( row , column ), using the given command .

该函数在 Qt 4.4 引入。

另请参阅 setCurrentItem (), currentRow (),和 currentColumn ().

QTableWidget.setCurrentItem ( self , QTableWidgetItem   item )

Sets the current item to item .

除非选定模式为 NoSelection , item is also be selected.

另请参阅 currentItem () 和 setCurrentCell ().

QTableWidget.setCurrentItem ( self , QTableWidgetItem   item , QItemSelectionModel.SelectionFlags   command )

Sets the current item to be item ,使用给定 command .

该函数在 Qt 4.4 引入。

另请参阅 currentItem () 和 setCurrentCell ().

QTableWidget.setHorizontalHeaderItem ( self , int  column , QTableWidgetItem   item )

item argument has it's ownership transferred to Qt.

Sets the horizontal header item for column column to item .

另请参阅 horizontalHeaderItem ().

QTableWidget.setHorizontalHeaderLabels ( self , QStringList  labels )

设置水平 Header 头标签,使用 labels .

QTableWidget.setItem ( self , int  row , int  column , QTableWidgetItem   item )

item argument has it's ownership transferred to Qt.

Sets the item for the given row and column to item .

The table takes ownership of the item.

Note that if sorting is enabled (see sortingEnabled ) and column is the current sort column, the row 将是 moved to the sorted position determined by item .

If you want to set several items of a particular row (say, by calling setItem() in a loop), you may want to turn off sorting before doing so, and turn it back on afterwards; this will allow you to use the same row argument for all items in the same row (i.e. setItem() will not move the row).

另请参阅 item () 和 takeItem ().

QTableWidget.setItemPrototype ( self , QTableWidgetItem   item )

item argument has it's ownership transferred to Qt.

Sets the item prototype for the table to the specified item .

The table widget will use the item prototype clone function when it needs to create a new table item. For example when the user is editing in an empty cell. This is useful when you have a QTableWidgetItem subclass and want to make sure that QTableWidget creates instances of your subclass.

The table takes ownership of the prototype.

另请参阅 itemPrototype ().

QTableWidget.setItemSelected ( self , QTableWidgetItem   item , bool  select )

QTableWidget.setRangeSelected ( self , QTableWidgetSelectionRange   range , bool  select )

Selects or deselects the range depending on select .

QTableWidget.setRowCount ( self , int  rows )

QTableWidget.setSortingEnabled ( self , bool  enable )

QTableWidget.setVerticalHeaderItem ( self , int  row , QTableWidgetItem   item )

item argument has it's ownership transferred to Qt.

Sets the vertical header item for row row to item .

另请参阅 verticalHeaderItem ().

QTableWidget.setVerticalHeaderLabels ( self , QStringList  labels )

Sets the vertical header labels using labels .

QTableWidget.sortItems ( self , int  column , Qt.SortOrder   order  = Qt.AscendingOrder)

Sorts all the rows in the table widget based on column and order .

Qt.DropActions QTableWidget.supportedDropActions ( self )

返回由此视图所支持的掉落动作。

另请参阅 Qt.DropActions .

QTableWidgetItem QTableWidget.takeHorizontalHeaderItem ( self , int  column )

QTableWidgetItem result

Removes the horizontal header item at column 从 header without deleting it.

该函数在 Qt 4.1 引入。

QTableWidgetItem QTableWidget.takeItem ( self , int  row , int  column )

QTableWidgetItem result

移除项在 row and column from the table without deleting it.

QTableWidgetItem QTableWidget.takeVerticalHeaderItem ( self , int  row )

QTableWidgetItem result

移除垂直 Header 头部项在 row from the header without deleting it.

该函数在 Qt 4.1 引入。

QTableWidgetItem QTableWidget.verticalHeaderItem ( self , int  row )

Returns the vertical header item for row row .

另请参阅 setVerticalHeaderItem ().

int QTableWidget.visualColumn ( self , int  logicalColumn )

Returns the visual column of the given logicalColumn .

QRect QTableWidget.visualItemRect ( self , QTableWidgetItem   item )

返回项所占据的视口矩形在 item .

int QTableWidget.visualRow ( self , int  logicalRow )

Returns the visual row of the given logicalRow .


Qt Signal Documentation

void cellActivated (int,int)

This is the default overload of this signal.

This signal is emitted when the cell specified by row and column has been activated

该函数在 Qt 4.1 引入。

void cellChanged (int,int)

This is the default overload of this signal.

This signal is emitted whenever the data of the item in the cell 指定通过 row and column 已改变。

该函数在 Qt 4.1 引入。

void cellClicked (int,int)

This is the default overload of this signal.

This signal is emitted whenever a cell in the table is clicked. row and column specified is the cell that was clicked.

该函数在 Qt 4.1 引入。

void cellDoubleClicked (int,int)

This is the default overload of this signal.

This signal is emitted whenever a cell in the table is double clicked. The row and column specified is the cell that was double clicked.

该函数在 Qt 4.1 引入。

void cellEntered (int,int)

This is the default overload of this signal.

This signal is emitted when the mouse cursor enters a cell. The cell is specified by row and column .

此信号才被发射当 mouseTracking is turned on, or when a mouse button is pressed while moving into an item.

该函数在 Qt 4.1 引入。

void cellPressed (int,int)

This is the default overload of this signal.

This signal is emitted whenever a cell in the table is pressed. row and column specified is the cell that was pressed.

该函数在 Qt 4.1 引入。

void currentCellChanged (int,int,int,int)

This is the default overload of this signal.

This signal is emitted whenever the current cell changes. The cell specified by previousRow and previousColumn is the cell that previously had the focus, the cell specified by currentRow and currentColumn is the new current cell.

该函数在 Qt 4.1 引入。

void currentItemChanged (QTableWidgetItem*,QTableWidgetItem*)

This is the default overload of this signal.

此信号被发射每当当前项改变。 上一 item is the item that previously had the focus, current 是新的当前项。

void itemActivated (QTableWidgetItem*)

This is the default overload of this signal.

此信号被发射当指定 item has been activated

void itemChanged (QTableWidgetItem*)

This is the default overload of this signal.

This signal is emitted whenever the data of item has 改变。

void itemClicked (QTableWidgetItem*)

This is the default overload of this signal.

This signal is emitted whenever an item in the table is clicked. item specified is the item that was clicked.

void itemDoubleClicked (QTableWidgetItem*)

This is the default overload of this signal.

This signal is emitted whenever an item in the table is double clicked. The item specified is the item that was double clicked.

void itemEntered (QTableWidgetItem*)

This is the default overload of this signal.

This signal is emitted when the mouse cursor enters an item. The item is the item entered.

此信号才被发射当 mouseTracking is turned on, or when a mouse button is pressed while moving into an item.

void itemPressed (QTableWidgetItem*)

This is the default overload of this signal.

This signal is emitted whenever an item in the table is pressed. item specified is the item that was pressed.

void itemSelectionChanged ()

This is the default overload of this signal.

此信号被发射每当选定改变。

另请参阅 selectedItems () 和 QTableWidgetItem.isSelected ().