Please refer to the vertical header part.
import sys
from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import *
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setupUI()
def setupUI(self):
self.setGeometry(800, 200, 300, 300)
self.tableWidget = QTableWidget(self)
self.tableWidget.resize(290, 290)
self.tableWidget.setRowCount(2)
self.tableWidget.setColumnCount(2)
self.setTableWidgetData()
self.tableWidget.cellClicked.connect(self.mycell_clicked)
self.tableWidget.verticalHeader().sectionClicked.connect(self.myheader_clicked)
def setTableWidgetData(self):
self.tableWidget.setHorizontalHeaderLabels(["aaaa", "bbbb"])
self.tableWidget.setItem(0, 0, QTableWidgetItem("(0,0)"))
self.tableWidget.setItem(0, 1, QTableWidgetItem("(0,1)"))
self.tableWidget.setItem(1, 0, QTableWidgetItem("(1,0)"))
self.tableWidget.setItem(1, 1, QTableWidgetItem("(1,1)"))
@pyqtSlot(int, int)
def mycell_clicked(self, row, col):
QMessageBox.information(self, '{}, {}'.format(row, col), '{}, {}'.format(row, col))
@pyqtSlot(int)
def myheader_clicked(self, logicalIndex):
QMessageBox.information(self, str(logicalIndex + 1), str(logicalIndex + 1))
if __name__ == "__main__":
app = QApplication(sys.argv)
mywindow = MyWindow()
mywindow.show()
app.exec_()
© 2024 OneMinuteCode. All rights reserved.