How to show a Tab (QWidget object) embedded in a QtWidget on button click?
-
Hello guys! I need a few help.
Here is my design with Qt Design and PyQt5.
Now I want to show the tab "Onglet i" when I click on it's corresponding push button "Ouvrir_Onglet_i".
Below is my code that is not working. Three different ways tried under each button.
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_TabWidget(object): def setupUi(self, TabWidget): TabWidget.setObjectName("TabWidget") TabWidget.resize(400, 300) self.tab_1 = QtWidgets.QWidget() self.tab_1.setObjectName("tab_1") self.Ouvrir_Onglet_1 = QtWidgets.QPushButton(self.tab_1) self.Ouvrir_Onglet_1.setGeometry(QtCore.QRect(10, 20, 91, 23)) self.Ouvrir_Onglet_1.setObjectName("Ouvrir_Onglet_1") self.Ouvrir_Onglet_2 = QtWidgets.QPushButton(self.tab_1) self.Ouvrir_Onglet_2.setGeometry(QtCore.QRect(10, 50, 91, 23)) self.Ouvrir_Onglet_2.setObjectName("Ouvrir_Onglet_2") self.Ouvrir_Onglet_3 = QtWidgets.QPushButton(self.tab_1) self.Ouvrir_Onglet_3.setGeometry(QtCore.QRect(10, 80, 91, 23)) self.Ouvrir_Onglet_3.setObjectName("Ouvrir_Onglet_3") TabWidget.addTab(self.tab_1, "") self.tab_2 = QtWidgets.QWidget() self.tab_2.setObjectName("tab_2") TabWidget.addTab(self.tab_2, "") self.tab_3 = QtWidgets.QWidget() self.tab_3.setObjectName("tab_3") TabWidget.addTab(self.tab_3, "") self.tab_4 = QtWidgets.QWidget() self.tab_4.setObjectName("tab_4") TabWidget.addTab(self.tab_4, "") self.retranslateUi(TabWidget) TabWidget.setCurrentIndex(0) QtCore.QMetaObject.connectSlotsByName(TabWidget) def Fonction_Ouvrir_Onglet_1(self): self.window = QtWidgets() self.ui = Ui_TabWidget() self.ui.setupUi(self.window.tab_2) self.window.tab_2.show() self.Ouvrir_Onglet_1.clicked.connect(self.Fonction_Ouvrir_Onglet_1) def Fonction_Ouvrir_Onglet_2(self): self.Ui_TabWidget.setCurrentIndex(2) self.Ouvrir_Onglet_2.clicked.connect(self.Fonction_Ouvrir_Onglet_2) self.Ouvrir_Onglet_3.clicked.connect(self.TabWidget.setCurrentIndex(3)) def retranslateUi(self, TabWidget): _translate = QtCore.QCoreApplication.translate TabWidget.setWindowTitle(_translate("TabWidget", "TabWidget")) self.Ouvrir_Onglet_1.setText(_translate("TabWidget", "Ouvrir_Onglet_1")) self.Ouvrir_Onglet_2.setText(_translate("TabWidget", "Ouvrir_Onglet_2")) self.Ouvrir_Onglet_3.setText(_translate("TabWidget", "Ouvrir_Onglet_3")) TabWidget.setTabText(TabWidget.indexOf(self.tab_1), _translate("TabWidget", "Menu_principal")) TabWidget.setTabText(TabWidget.indexOf(self.tab_2), _translate("TabWidget", "Onglet 1")) TabWidget.setTabText(TabWidget.indexOf(self.tab_3), _translate("TabWidget", "Onglet 2")) TabWidget.setTabText(TabWidget.indexOf(self.tab_4), _translate("TabWidget", "Onlget 3")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) TabWidget = QtWidgets.QTabWidget() ui = Ui_TabWidget() ui.setupUi(TabWidget) TabWidget.show() sys.exit(app.exec_())
Note : I generated the code using "pyiuc5" in command, then tried to do job with functions
Fonction_Ouvrir_Onglet_1
,Fonction_Ouvrir_Onglet_2
andself.TabWidget.setCurrentIndex()
method. None of these 3 attempts is working.Eternal gratitude to my helper.
Thanks in advance -
Hello @denni-0,
Thak you very much for your answer. It's working perfectly and I have learned a lot through your coding style.
The reasons for the buttons in a tab are :
- Other widgets are on the main menu windows
- I have a lot of tabs (42 I think, corresponding to 42 data bases).
I want to be able to reach any of those tabs from that specific one by button click, and, come back there from any other tab by button click (that last button will be in the current tab I am in, redirecting to the tab containing all my buttons).
I need to understand inheritance to make my tabs communicate that way. Added to what you have provided, I think it will do.
Thanks at last!