如果您的自定义控件继承自QTableWidget,您可以使用scrollToItem方法将特定单元格滚动到可见区域。以下是一个示例代码:

from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QMainWindow

class CustomTableWidget(QTableWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setColumnCount(5)
        self.setRowCount(5)
        self.setHorizontalHeaderLabels(["Col 1", "Col 2", "Col 3", "Col 4", "Col 5"])

        for row in range(5):
            for col in range(5):
                item = QTableWidgetItem(f"Cell {row + 1}, {col + 1}")
                self.setItem(row, col, item)

        # 滚动到指定单元格
        self.scrollToItem(self.item(2, 3))

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.tableWidget = CustomTableWidget(self)
        self.setCentralWidget(self.tableWidget)

if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()

在上述示例中,我们创建了一个自定义的QTableWidget,将其作为主窗口的中央部件,并在初始化方法中调用scrollToItem方法将第2行第3列的单元格滚动到可见区域

QTableWidget 滚动到指定位置 23 我是自定义控件用不了scrollToItem

原文地址: http://www.cveoy.top/t/topic/i0XT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录