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 表

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 表工具提示