QT4 表

我们可以使用 QTableWidget 显示一个表,它是PyQt 模块的一部分。我们可以设置标题,行数,列数并添加数据。

Qt4 表示例

以下示例:

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
import sys
 
def main():

    app     = QApplication(sys.argv)
    table   = QTableWidget()
    tableItem   = QTableWidgetItem()
 
    # initiate table
    table.setWindowTitle("QTableWidget Example @pythonspot.com")
    table.resize(400, 250)
    table.setRowCount(4)
    table.setColumnCount(2)
 
    # set data
    table.setItem(0,0, QTableWidgetItem("Item (1,1)"))
    table.setItem(0,1, QTableWidgetItem("Item (1,2)"))
    table.setItem(1,0, QTableWidgetItem("Item (2,1)"))
    table.setItem(1,1, QTableWidgetItem("Item (2,2)"))
    table.setItem(2,0, QTableWidgetItem("Item (3,1)"))
    table.setItem(2,1, QTableWidgetItem("Item (3,2)"))
    table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
    table.setItem(3,1, QTableWidgetItem("Item (4,2)"))
 
    # show table
    table.show()
    return app.exec_()
 
if __name__ == '__main__':
    main()

结果:

[PyQt](/tutorial/pyqt4/) 表

QTableWidget 标签

你可以使用 setHorizo​​ntalHeaderLabels() 函数设置标头。这同样适用于垂直标签。下面的演示:

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
import sys
 
def main():

    app     = QApplication(sys.argv)
    table   = QTableWidget()
    tableItem   = QTableWidgetItem()
 
    # initiate table
    table.setWindowTitle("QTableWidget Example @pythonspot.com")
    table.resize(400, 250)
    table.setRowCount(4)
    table.setColumnCount(2)
 
    # set label
    table.setHorizontalHeaderLabels(QString("H1;H2;").split(";"))
    table.setVerticalHeaderLabels(QString("V1;V2;V3;V4").split(";"))
 
    # set data
    table.setItem(0,0, QTableWidgetItem("Item (1,1)"))
    table.setItem(0,1, QTableWidgetItem("Item (1,2)"))
    table.setItem(1,0, QTableWidgetItem("Item (2,1)"))
    table.setItem(1,1, QTableWidgetItem("Item (2,2)"))
    table.setItem(2,0, QTableWidgetItem("Item (3,1)"))
    table.setItem(2,1, QTableWidgetItem("Item (3,2)"))
    table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
    table.setItem(3,1, QTableWidgetItem("Item (4,2)"))
 
    # show table
    table.show()
    return app.exec_()
 
if __name__ == '__main__':
    main()

结果:

PyQT 表

QTableWidget 单击事件

我们可以使用此过程检测单元格点击,首先添加一个函数:

# on click function
table.cellClicked.connect(cellClick)

然后定义函数:

def cellClick(row,col):
    print "Click on " + str(row) + " " + str(col)

Python 编程语言从 0 开始计数,所以当你按(1,1)时你会看到(0,0)。检测表单击的完整代码:

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
import sys
 
def cellClick(row,col):
    print "Click on " + str(row) + " " + str(col)
 
def main():

    app     = QApplication(sys.argv)
    table   = QTableWidget()
    tableItem   = QTableWidgetItem()
 
    # initiate table
    table.setWindowTitle("QTableWidget Example @pythonspot.com")
    table.resize(400, 250)
    table.setRowCount(4)
    table.setColumnCount(2)
 
    # set label
    table.setHorizontalHeaderLabels(QString("H1;H2;").split(";"))
    table.setVerticalHeaderLabels(QString("V1;V2;V3;V4").split(";"))
 
    # set data
    table.setItem(0,0, QTableWidgetItem("Item (1,1)"))
    table.setItem(0,1, QTableWidgetItem("Item (1,2)"))
    table.setItem(1,0, QTableWidgetItem("Item (2,1)"))
    table.setItem(1,1, QTableWidgetItem("Item (2,2)"))
    table.setItem(2,0, QTableWidgetItem("Item (3,1)"))
    table.setItem(2,1, QTableWidgetItem("Item (3,2)"))
    table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
    table.setItem(3,1, QTableWidgetItem("Item (4,2)"))
 
    # on click function
    table.cellClicked.connect(cellClick)
 
    # show table
    table.show()
    return app.exec_()
 
if __name__ == '__main__':
    main()

如果要以非程序员方式显示单元格/行号,请使用以下代码:

def cellClick(row,col):
    print "Click on " + str(row+1) + " " + str(col+1)

工具提示文字

我们可以使用该方法设置工具提示(鼠标悬停)文本。如果在不存在的列上设置工具提示,则会出现错误。

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
import sys
 
def main():

    app     = QApplication(sys.argv)
    table   = QTableWidget()
    tableItem   = QTableWidgetItem()
 
    # initiate table
    table.setWindowTitle("QTableWidget Example @pythonspot.com")
    table.resize(400, 250)
    table.setRowCount(4)
    table.setColumnCount(2)
 
    # set label
    table.setHorizontalHeaderLabels(QString("H1;H2;").split(";"))
    table.setVerticalHeaderLabels(QString("V1;V2;V3;V4").split(";"))
 
    # set data
    table.setItem(0,0, QTableWidgetItem("Item (1,1)"))
    table.setItem(0,1, QTableWidgetItem("Item (1,2)"))
    table.setItem(1,0, QTableWidgetItem("Item (2,1)"))
    table.setItem(1,1, QTableWidgetItem("Item (2,2)"))
    table.setItem(2,0, QTableWidgetItem("Item (3,1)"))
    table.setItem(2,1, QTableWidgetItem("Item (3,2)"))
    table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
    table.setItem(3,1, QTableWidgetItem("Item (4,2)"))
 
    # tooltip text
    table.horizontalHeaderItem(0).setToolTip("Column 1 ")
    table.horizontalHeaderItem(1).setToolTip("Column 2 ")
 
    # show table
    table.show()
    return app.exec_()
 
if __name__ == '__main__':
    main()

结果:

PyQT 表工具提示