Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Acces to qlineedit from another class



  • I am new in Python/Qt designer, I am trying to create an application with Qt Designer. I added a application with 2 tab, the first one I create a button to add a text in the second tab. You can see in the second tab it's a simple lineedit (where the text will displayed). When I click in the button, slot signal is activated but nothing changed in the second tab. I don't know how can I get the index of the tab and use it when I need to change the settext of the lineedit in the second tab.

    
    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'EXample_simple_Tab_Main.ui'
    #
    # Created by: PyQt5 UI code generator 5.9.2
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtCore import QObject, pyqtSlot                                        
    
    class Populate_Tab(QtWidgets.QWidget):
    
        def Populate_BCU_Info(obj):
            # print("final self",type(self))
            Value ="10.22"
    
            obj.Tab_Ip_Adress_2.setText(Value)
    
            print("resultat",obj.Tab_Ip_Adress_2.text())
    
    
    class TabPage(QtWidgets.QWidget):
        def __init__(self, parent):
            super().__init__()
                              
            self.tab_2 = QtWidgets.QWidget()
            self.tab_2.setObjectName("tab_2")
            self.Tab_Ip_Adress_2 = QtWidgets.QLineEdit(self)
            self.Tab_Ip_Adress_2.setGeometry(QtCore.QRect(130, 110, 120, 20))
            self.Tab_Ip_Adress_2.setStyleSheet("background-color: rgb(169, 255, 8);\n"
    "")
            self.Tab_Ip_Adress_2.setReadOnly(False)
            self.Tab_Ip_Adress_2.setObjectName("Tab_Ip_Adress_2")
            self.Tab_Ip_Adress_2.setText("test from tab")
    
    
    
    class Ui_MainWindow(QtWidgets.QWidget):
        def __init__(self):
            super().__init__()
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(1179, 945)
            MainWindow.setStyleSheet("background-color: rgb(255, 255, 255);")
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.centralwidget)
            self.verticalLayout_3.setObjectName("verticalLayout_3")
            self.verticalLayout = QtWidgets.QVBoxLayout()
            self.verticalLayout.setObjectName("verticalLayout")
            self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
            self.tabWidget.setTabsClosable(True)
            self.tabWidget.setObjectName("tabWidget")
            self.tab = QtWidgets.QWidget()
            self.tab.setObjectName("tab")
            self.gridLayout = QtWidgets.QGridLayout(self.tab)
            self.gridLayout.setObjectName("gridLayout")
            self.pushButton = QtWidgets.QPushButton(self.tab)
            self.pushButton.setObjectName("pushButton")
            self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
    
            self.tabWidget.addTab(self.tab, "")
                                      
            self.verticalLayout.addWidget(self.tabWidget)
            self.verticalLayout_3.addLayout(self.verticalLayout)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 1179, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            self.tabWidget.setCurrentIndex(0)
            # self.tabWidget.tabCloseRequested['int'].connect(MainWindow.CloseTab)
            self.pushButton.clicked.connect(lambda x:self.CreatNewTab())
            # self.pushButton_New.clicked.connect(lambda x:self.CreatNewTab2())
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.pushButton.setText(_translate("MainWindow", "PushButton"))
            # self.pushButton_New.setText(_translate("MainWindow", "New_test"))
            self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Main"))
                                                                                                           
        @pyqtSlot( )
        def CreatNewTab( self ):
            """create a tab from the list of the BCUs"""
            List_of_BCUs = ["10.22.91.103"]
            # print(List_of_BCUs)
            for i in range (len(List_of_BCUs)):
                if List_of_BCUs[i] != '':
                    text = List_of_BCUs[i]
                    # print(type(self))
                    # TabPage(self)
                    index_tab = self.tabWidget.addTab(TabPage(self), text ) 
    
                    # Populate_Tab(TabPage(self))
                    # Testons = Populate_Tab.Populate_BCU_Info(TabPage(self))
                    # print(index_tab)
                    Populate_Tab.Populate_BCU_Info(TabPage(self))
            pass  
    
                                
    
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
    
        # ui.setupUi(MainWindow)
    
        MainWindow.show()
        sys.exit(app.exec_())
    

    I think that the call function is correct, I can have acces to "Tab_Ip_Adress_2" qlinewidget, and settext is working fine but it's not displayed in the window I don't why, please any help :)


  • Lifetime Qt Champion

    @AnasHanlab said in Acces to qlineedit from another class:

    index_tab = self.tabWidget.addTab(TabPage(self), text )

    Populate_Tab.Populate_BCU_Info(TabPage(self))

    You are creating two different TabPage instances here and change the text on the second one.



  • @jsulm ,
    thank you for the reactivity.
    the first line index_tab = self.tabWidget.addTab(TabPage(self), text ), is to create a new tab from the main with the slot.
    and the second one is to populate this new tab page.
    How can I populate the tab created before ?


  • Lifetime Qt Champion

    @AnasHanlab said in Acces to qlineedit from another class:

    and the second one is to populate this new tab page.

    But you are NOT populating the one you created, you are populating a new one!
    Why not simply:

    tabPage = TabPage(self)
    index_tab = self.tabWidget.addTab(tabPage, text )
    Populate_Tab.Populate_BCU_Info(tabPage)
    


  • oh magic, I had a doubt about it I didn't have any way to check it.
    Thank you so much you resolved my issue :)


Log in to reply