我们如何在点击按钮时在pyqt中打开特定于选项卡的选项卡?

2024-04-25 13:53:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我用qt designer在我的设计页面中创建了10个选项卡,并在其顶部创建了一个菜单栏。 现在,我想将菜单栏中的一个选项连接到一个选项卡(例如,选项卡5)。 i、 e.当我点击菜单->;按钮,然后打开选项卡5


Tags: gt选项菜单页面qt按钮选项卡designer
1条回答
网友
1楼 · 发布于 2024-04-25 13:53:51

要打开选项卡,必须使用QTabWidgetsetCurrentIndex()方法,此方法必须指明索引。每次与菜单的QAction关联的触发信号出现时,必须执行上述操作

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        widget = QTabWidget(self)
        for i in range(10):
            widget.addTab(QListWidget(), "tab{}".format(i+1))

        self.setCentralWidget(widget)

        menubar = self.menuBar()
        action = menubar.addAction("Select tab5")
        action.triggered.connect(lambda: widget.setCurrentIndex(4))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

plus:

    self.Add_GroupD.triggered.connect(lambda checked, index1=4, index2=1 : self.someslot(index1, index2))
def someslot(self, index1, index2)
    self.tabWidget_4.setCurrentIndex(index1)
    self.tabs.setCurrentIndex(index2)

相关问题 更多 >