内容表

上一话题

QTableView

下一话题

QTableWidgetItem

QTableWidget

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

Inheritance diagram of PySide2.QtWidgets.QTableWidget

概要

函数

虚函数

信号

详细描述

../../_images/windows-tableview.png

Table widgets provide standard table display facilities for applications. The items in a QTableWidget are provided by QTableWidgetItem .

若希望表格使用您自己的数据模型,应使用 QTableView 而不是此类。

可以采用所需行数和列数构造表格 Widget:

tableWidget = new QTableWidget(12, 3, this);
											

另外,可以在不给定尺寸的情况下构建表格并稍后重置尺寸:

tableWidget = new QTableWidget(this);
tableWidget->setRowCount(10);
tableWidget->setColumnCount(5);
											

Items are created outside 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() 了解细节)。

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

class QTableWidget ( [ parent=None ] )

QTableWidget(rows, columns[, parent=None])

param parent

QWidget

param columns

int

param rows

int

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

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

PySide2.QtWidgets.QTableWidget. cellActivated ( row , column )
参数
  • row int

  • column int

PySide2.QtWidgets.QTableWidget. cellChanged ( row , column )
参数
  • row int

  • column int

PySide2.QtWidgets.QTableWidget. cellClicked ( row , column )
参数
  • row int

  • column int

PySide2.QtWidgets.QTableWidget. cellDoubleClicked ( row , column )
参数
  • row int

  • column int

PySide2.QtWidgets.QTableWidget. cellEntered ( row , column )
参数
  • row int

  • column int

PySide2.QtWidgets.QTableWidget. cellPressed ( row , column )
参数
  • row int

  • column int

PySide2.QtWidgets.QTableWidget. cellWidget ( row , column )
参数
  • row int

  • column int

返回类型

QWidget

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

注意

表格拥有 Widget 的所有权。

另请参阅

setCellWidget()

PySide2.QtWidgets.QTableWidget. clear ( )

Removes all items in the view. This will also remove all selections and headers. If you don’t want to remove the headers, use clearContents() . The table dimensions stay the same.

PySide2.QtWidgets.QTableWidget. clearContents ( )

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

PySide2.QtWidgets.QTableWidget. closePersistentEditor ( item )
参数

item QTableWidgetItem

关闭持久编辑器为 item .

PySide2.QtWidgets.QTableWidget. column ( item )
参数

item QTableWidgetItem

返回类型

int

返回列号为 item .

PySide2.QtWidgets.QTableWidget. columnCount ( )
返回类型

int

返回列数。

另请参阅

setColumnCount()

PySide2.QtWidgets.QTableWidget. currentCellChanged ( currentRow , currentColumn , previousRow , previousColumn )
参数
  • currentRow int

  • currentColumn int

  • previousRow int

  • previousColumn int

PySide2.QtWidgets.QTableWidget. currentColumn ( )
返回类型

int

返回当前项的列号。

PySide2.QtWidgets.QTableWidget. currentItem ( )
返回类型

QTableWidgetItem

返回当前项。

另请参阅

setCurrentItem()

PySide2.QtWidgets.QTableWidget. currentItemChanged ( current , 上一 )
参数
PySide2.QtWidgets.QTableWidget. currentRow ( )
返回类型

int

返回当前项的行号。

PySide2.QtWidgets.QTableWidget. dropMimeData ( row , column , data , action )
参数
  • row int

  • column int

  • data QMimeData

  • action DropAction

返回类型

bool

处理 data 提供通过拖放操作结束按给定 action 以给定 row and column 。返回 true if the data and action can be handled by the model; otherwise returns false .

PySide2.QtWidgets.QTableWidget. editItem ( item )
参数

item QTableWidgetItem

开始编辑 item 若它可编辑。

PySide2.QtWidgets.QTableWidget. findItems ( text , flags )
参数
  • text – unicode

  • flags MatchFlags

返回类型

Finds items that matches the text using the given flags .

PySide2.QtWidgets.QTableWidget. horizontalHeaderItem ( column )
参数

column int

返回类型

QTableWidgetItem

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

PySide2.QtWidgets.QTableWidget. indexFromItem ( item )
参数

item QTableWidgetItem

返回类型

QModelIndex

返回 QModelIndex 关联给定 item .

注意

In Qt versions prior to 5.10, this function took a non- const item .

PySide2.QtWidgets.QTableWidget. indexFromItem ( item )
参数

item QTableWidgetItem

返回类型

QModelIndex

返回 QModelIndex 关联给定 item .

注意

In Qt versions prior to 5.10, this function took a non- const item .

PySide2.QtWidgets.QTableWidget. insertColumn ( column )
参数

column int

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

PySide2.QtWidgets.QTableWidget. insertRow ( row )
参数

row int

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

PySide2.QtWidgets.QTableWidget. isItemSelected ( item )
参数

item QTableWidgetItem

返回类型

bool

注意

此函数被弃用。

返回 true item is selected, otherwise returns false .

This function is deprecated. Use isSelected() 代替。

PySide2.QtWidgets.QTableWidget. isPersistentEditorOpen ( item )
参数

item QTableWidgetItem

返回类型

bool

返回是否打开持久编辑器为项 item .

PySide2.QtWidgets.QTableWidget. item ( row , column )
参数
  • row int

  • column int

返回类型

QTableWidgetItem

返回项为给定 row and column 若有设置;否则返回 None .

另请参阅

setItem()

PySide2.QtWidgets.QTableWidget. itemActivated ( item )
参数

item QTableWidgetItem

PySide2.QtWidgets.QTableWidget. itemAt ( p )
参数

p QPoint

返回类型

QTableWidgetItem

Returns a pointer to the item at the given point , or returns None if point is not covered by an item in the table widget.

另请参阅

item()

PySide2.QtWidgets.QTableWidget. itemAt ( x , y )
参数
  • x int

  • y int

返回类型

QTableWidgetItem

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

另请参阅

item()

PySide2.QtWidgets.QTableWidget. itemChanged ( item )
参数

item QTableWidgetItem

PySide2.QtWidgets.QTableWidget. itemClicked ( item )
参数

item QTableWidgetItem

PySide2.QtWidgets.QTableWidget. itemDoubleClicked ( item )
参数

item QTableWidgetItem

PySide2.QtWidgets.QTableWidget. itemEntered ( item )
参数

item QTableWidgetItem

PySide2.QtWidgets.QTableWidget. itemFromIndex ( index )
参数

index QModelIndex

返回类型

QTableWidgetItem

返回指针指向 QTableWidgetItem 关联给定 index .

PySide2.QtWidgets.QTableWidget. itemPressed ( item )
参数

item QTableWidgetItem

PySide2.QtWidgets.QTableWidget. itemPrototype ( )
返回类型

QTableWidgetItem

Returns the item prototype used by the table.

PySide2.QtWidgets.QTableWidget. itemSelectionChanged ( )
PySide2.QtWidgets.QTableWidget. items ( data )
参数

data QMimeData

返回类型

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.

PySide2.QtWidgets.QTableWidget. mimeData ( items )
参数

items

返回类型

QMimeData

返回对象包含序列化描述指定通过 items 。用于描述项的格式获取自 mimeTypes() 函数。

若项列表为空, None 被返回而不是序列化空列表。

PySide2.QtWidgets.QTableWidget. mimeTypes ( )
返回类型

字符串列表

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

另请参阅

mimeData()

PySide2.QtWidgets.QTableWidget. openPersistentEditor ( item )
参数

item QTableWidgetItem

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

PySide2.QtWidgets.QTableWidget. removeCellWidget ( row , column )
参数
  • row int

  • column int

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

PySide2.QtWidgets.QTableWidget. removeColumn ( column )
参数

column int

移除列 column 及表格中的所有项。

PySide2.QtWidgets.QTableWidget. removeRow ( row )
参数

row int

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

PySide2.QtWidgets.QTableWidget. row ( item )
参数

item QTableWidgetItem

返回类型

int

Returns the row for the item .

PySide2.QtWidgets.QTableWidget. rowCount ( )
返回类型

int

Returns the number of rows.

另请参阅

setRowCount()

PySide2.QtWidgets.QTableWidget. scrollToItem ( item [ , hint=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.

PySide2.QtWidgets.QTableWidget. selectedItems ( )
返回类型

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 包括 empty cells.

另请参阅

selectedIndexes()

PySide2.QtWidgets.QTableWidget. selectedRanges ( )
返回类型

Returns a list of all selected ranges.

PySide2.QtWidgets.QTableWidget. setCellWidget ( row , column , widget )
参数
  • row int

  • column int

  • widget QWidget

设置给定 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, QLineEdit())
...
setCellWidget(index, QTextEdit())
											

另请参阅

cellWidget()

PySide2.QtWidgets.QTableWidget. setColumnCount ( columns )
参数

columns int

Sets the number of columns in this table’s model to columns . If this is less than columnCount() , the data in the unwanted columns is discarded.

PySide2.QtWidgets.QTableWidget. setCurrentCell ( row , column )
参数
  • row int

  • column int

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

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

PySide2.QtWidgets.QTableWidget. setCurrentCell ( row , column , command )
参数
  • row int

  • column int

  • command SelectionFlags

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

PySide2.QtWidgets.QTableWidget. setCurrentItem ( item )
参数

item QTableWidgetItem

Sets the current item to item .

除非选定模式为 NoSelection ,项还被选中。

PySide2.QtWidgets.QTableWidget. setCurrentItem ( item , command )
参数

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

PySide2.QtWidgets.QTableWidget. setHorizontalHeaderItem ( column , item )
参数

Sets the horizontal header item for column column to item . If necessary, the column count is increased to fit the item. The previous header item (if there was one) is deleted.

PySide2.QtWidgets.QTableWidget. setHorizontalHeaderLabels ( labels )
参数

labels – 字符串列表

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

PySide2.QtWidgets.QTableWidget. setItem ( row , column , item )
参数

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 will be moved to the sorted position determined by item .

If you want to set several items of a particular row (say, by calling 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. will not move the row).

PySide2.QtWidgets.QTableWidget. setItemPrototype ( item )
参数

item QTableWidgetItem

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()

PySide2.QtWidgets.QTableWidget. setItemSelected ( item , select )
参数

注意

此函数被弃用。

Selects or deselects item depending on select .

This function is deprecated. Use setSelected() 代替。

另请参阅

isItemSelected()

PySide2.QtWidgets.QTableWidget. setRangeSelected ( range , select )
参数

Selects or deselects the range depending on select .

PySide2.QtWidgets.QTableWidget. setRowCount ( rows )
参数

rows int

Sets the number of rows in this table’s model to rows . If this is less than rowCount() , the data in the unwanted rows is discarded.

PySide2.QtWidgets.QTableWidget. setVerticalHeaderItem ( row , item )
参数

Sets the vertical header item for row row to item .

PySide2.QtWidgets.QTableWidget. setVerticalHeaderLabels ( labels )
参数

labels – 字符串列表

Sets the vertical header labels using labels .

PySide2.QtWidgets.QTableWidget. sortItems ( column [ , order=Qt.AscendingOrder ] )
参数
  • column int

  • order SortOrder

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

PySide2.QtWidgets.QTableWidget. supportedDropActions ( )
返回类型

DropActions

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

另请参阅

DropActions

PySide2.QtWidgets.QTableWidget. takeHorizontalHeaderItem ( column )
参数

column int

返回类型

QTableWidgetItem

Removes the horizontal header item at column 从头部而不删除它。

PySide2.QtWidgets.QTableWidget. takeItem ( row , column )
参数
  • row int

  • column int

返回类型

QTableWidgetItem

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

PySide2.QtWidgets.QTableWidget. takeVerticalHeaderItem ( row )
参数

row int

返回类型

QTableWidgetItem

移除垂直 Header 头部项在 row 从头部而不删除它。

PySide2.QtWidgets.QTableWidget. verticalHeaderItem ( row )
参数

row int

返回类型

QTableWidgetItem

Returns the vertical header item for row row .

PySide2.QtWidgets.QTableWidget. visualColumn ( logicalColumn )
参数

logicalColumn int

返回类型

int

Returns the visual column of the given logicalColumn .

PySide2.QtWidgets.QTableWidget. visualItemRect ( item )
参数

item QTableWidgetItem

返回类型

QRect

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

PySide2.QtWidgets.QTableWidget. visualRow ( logicalRow )
参数

logicalRow int

返回类型

int

Returns the visual row of the given logicalRow .