我们如何才能打开按钮点击pyqt中的选项卡特定选项卡?
问题描述:
我使用qt设计器在我的设计页面中制作了10个选项卡,并在其顶部创建了一个菜单栏。 现在我想将菜单栏中的一个选项连接到选项卡(比如选项卡5)。 即当我在菜单 - 点击>按钮,然后tab5被打开我们如何才能打开按钮点击pyqt中的选项卡特定选项卡?
答
要打开,你必须使用QTabWidget
的setCurrentIndex()
方法的标签,这种方法必须注明指数。以上每次必须执行与菜单
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_())
的QAction
加上相关的触发信号:
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)
这个工作谢谢,但我有somemore在这里做。我有标签内的标签,我想要导航说按钮点击后,我得到选项卡5,现在我想在Tab2内tab5 ..你能帮我这吗?对不起,我现在延长我的问题:) –
缺少什么? – eyllanesc
显示您的代码以更好地理解,我明白,通过单击按钮访问tab5,就像我在示例中显示的那样,在选择嵌套tab2之前哪个操作? – eyllanesc