NEStenerus nester Asked:2020-07-22 20:53:22 +0000 UTC2020-07-22 20:53:22 +0000 UTC 2020-07-22 20:53:22 +0000 UTC 如何在 QTableWidget 中捕获列调整大小事件? 772 我有QTableWidget,用户可以用鼠标调整列的大小,我希望能够在调整任何列的大小时捕捉到一些信号。 但是QTableWidget没有信号可以做我想做的事。 我在哪里可以找到正确的信号? python 1 个回答 Voted Best Answer gil9red 2020-07-27T16:18:29Z2020-07-27T16:18:29Z 要跟踪表头中列的大小,您需要参考horizontalHeader并连接到它的信号sectionResized: QHeaderView::sectionResized(int logicalIndex, int oldSize, int newSize) 例子: from PyQt5.QtWidgets import QApplication, QTableWidget def _on_section_resized(logical_index: int, old_size: int, new_size: int): print(logical_index, old_size, new_size) app = QApplication([]) table = QTableWidget() table.setColumnCount(5) table.horizontalHeader().sectionResized.connect(_on_section_resized) table.resize(600, 400) table.show() app.exec() 如果信号连接将到方法: from PyQt5.QtWidgets import QApplication, QTableWidget, QMainWindow class MainWindow(QMainWindow): def __init__(self): super().__init__() table = QTableWidget() table.setColumnCount(5) table.horizontalHeader().sectionResized.connect(self._on_section_resized) self.setCentralWidget(table) def _on_section_resized(self, logical_index: int, old_size: int, new_size: int): print(logical_index, old_size, new_size) app = QApplication([]) mw = MainWindow() mw.resize(600, 400) mw.show() app.exec()
要跟踪表头中列的大小,您需要参考
horizontalHeader并连接到它的信号sectionResized:例子:
如果信号连接将到方法: