i have made 10 tabs in my design page using qt designer and have made a menu bar on the top of it. Now i want to connect one of the options in the menubar to a tab (say tab 5). i.e. when i click on menu->button then the tab5 gets opened
Asked
Active
Viewed 2,240 times
1 Answers
4
To open a tab you must use the setCurrentIndex()
method of the QTabWidget
, to this method you must indicate the index. The above must be executed each time the triggered signal associated with the QAction
of the menu
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)

eyllanesc
- 235,170
- 19
- 170
- 241
-
This Worked Thanks but i have somemore to do here. i have tabs inside tabs in which i want to navigate say after button click i got to tab 5 and now i wanted to be in tab2 inside tab5..could you help me with this too? sorry for the fact that i m extending my question now :) – Sahil Jain Sep 25 '17 at 08:35
-
What is missing? – eyllanesc Sep 25 '17 at 08:36
-
Show your code to understand better, I understand that with the click on the button access tab5, as I show in my example, before which action do you want to select the nested tab2? – eyllanesc Sep 25 '17 at 08:40
-
self.Add_GroupD.triggered.connect(lambda : self.tabWidget_4.setCurrentIndex(4)) self.Add_GroupD.triggered.connect(lambda : self.tabs.setCurrentIndex(1)) – Sahil Jain Sep 25 '17 at 08:40
-
yup i wanted to go to nested tab2 exactly. you have a better idea than this? – Sahil Jain Sep 25 '17 at 08:42
-
Working Great thanks can this function be generic? if i could send index1 and index2 as args i could reuse the function easily for every nest as i have many nests but if this cannot be generic then maybe there wouldn't be need of function here as it wouldn't modularize my code anyhow – Sahil Jain Sep 25 '17 at 08:51
-
see my answer :P – eyllanesc Sep 25 '17 at 08:54
-
Works like a Fire :P thanks again i think you can help me alot with QT – Sahil Jain Sep 25 '17 at 09:01
-
see this answer: https://stackoverflow.com/questions/46396422/using-database-and-dict-to-add-pyqt5-menus-and-triggers-python-3/46396590#46396590 and your comments. – eyllanesc Sep 25 '17 at 09:08