Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Class issue in connect definition
Forum Updated to NodeBB v4.3 + New Features

Class issue in connect definition

Scheduled Pinned Locked Moved Solved Qt for Python
qt for python
12 Posts 3 Posters 899 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    heedaf
    wrote on last edited by heedaf
    #1

    Can someone please help me with how I can edit
    the label when the push button is clicked via the pushButton connect? Can't figure it out. The class UI_dialog is created by PYUIC in Pycharm.

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_Dialog(object):
    def setupUi(self, Dialog):
    Dialog.setObjectName("Dialog")
    Dialog.resize(585, 321)
    self.label = QtWidgets.QLabel(Dialog)
    self.label.setGeometry(QtCore.QRect(250, 50, 61, 21))
    self.label.setMinimumSize(QtCore.QSize(41, 0))
    self.label.setAlignment(QtCore.Qt.AlignCenter)
    self.label.setWordWrap(True)
    self.label.setObjectName("label")
    self.pushButton = QtWidgets.QPushButton(Dialog)
    self.pushButton.setGeometry(QtCore.QRect(266, 98, 31, 23))
    self.pushButton.setObjectName("pushButton")
    font = QtGui.QFont()
    font.setPointSize(12)
    
    self.retranslateUi(Dialog)
    self.pushButton.clicked.connect(Dialog.SB_UP_click)
    QtCore.QMetaObject.connectSlotsByName(Dialog)
    
    def retranslateUi(self, Dialog):
    _translate = QtCore.QCoreApplication.translate
    Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
    self.label.setText(_translate("Dialog", "SOUTH"))
    self.pushButton.setText(_translate("Dialog", "UP"))
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    from My_Window import Ui_Dialog
    import sys
    
    class MainWindow(QtWidgets.QMainWindow, Ui_Dialog):
    def __init__(self):
    super(MainWindow, self).__init__()
    self.ui = Ui_Dialog()
    self.ui.setupUi(self)
    
    
    def SB_UP_click(self):
    print("SB UP")
    self.label.setText("HI")
    print("got here")
    
    
    def main():
    app = QtWidgets.QApplication([])
    window = MainWindow()
    window.show()
    
    sys.exit(app.exec_())
    
    if __name__ == '__main__':
    main()
    
    jsulmJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #10

      Late night strikes again ! :-D
      I misread the code.

      Your slot should be something like:

      def SB_UP_click(self):
          print("SB UP")
          self.ui.label.setText("HI")
          print("got here")
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • H heedaf

        Can someone please help me with how I can edit
        the label when the push button is clicked via the pushButton connect? Can't figure it out. The class UI_dialog is created by PYUIC in Pycharm.

        from PyQt5 import QtCore, QtGui, QtWidgets
        
        class Ui_Dialog(object):
        def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(585, 321)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(250, 50, 61, 21))
        self.label.setMinimumSize(QtCore.QSize(41, 0))
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setWordWrap(True)
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(266, 98, 31, 23))
        self.pushButton.setObjectName("pushButton")
        font = QtGui.QFont()
        font.setPointSize(12)
        
        self.retranslateUi(Dialog)
        self.pushButton.clicked.connect(Dialog.SB_UP_click)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        
        def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "SOUTH"))
        self.pushButton.setText(_translate("Dialog", "UP"))
        
        from PyQt5 import QtCore, QtGui, QtWidgets
        from My_Window import Ui_Dialog
        import sys
        
        class MainWindow(QtWidgets.QMainWindow, Ui_Dialog):
        def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        
        
        def SB_UP_click(self):
        print("SB UP")
        self.label.setText("HI")
        print("got here")
        
        
        def main():
        app = QtWidgets.QApplication([])
        window = MainWindow()
        window.show()
        
        sys.exit(app.exec_())
        
        if __name__ == '__main__':
        main()
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @heedaf said in Class issue in connect definition:

        self.pushButton.clicked.connect(Dialog.SB_UP_click)

        You're already connecting, so what does not work?

        And please indent your code properly: it is hard to read.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        H 1 Reply Last reply
        1
        • jsulmJ jsulm

          @heedaf said in Class issue in connect definition:

          self.pushButton.clicked.connect(Dialog.SB_UP_click)

          You're already connecting, so what does not work?

          And please indent your code properly: it is hard to read.

          H Offline
          H Offline
          heedaf
          wrote on last edited by
          #3

          @jsulm Sorry! On a cell phone and copied and pasted it over. When I try and write to the text of the label widget from this call I get something like label is not part of MainWindow.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Hi,

            You do not have any widget named label_2 be it in your designer part or your MainWindow class.

            You have one that is named label that you can access through "self.ui".

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • H Offline
              H Offline
              heedaf
              wrote on last edited by
              #5

              My bad! Missed that on and just fixed it.. It should be just label. I'm using PyCharm so I copied it over from their forum. I'm not at my computer but I believe I got an error with self.ui.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by SGaist
                #6

                I see.

                You did the connection in the wrong class.
                Setup the connection in your MainWindow class not the one that is building the UI.

                Wrong code reading, it's really just the slot that has an error.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  heedaf
                  wrote on last edited by heedaf
                  #7
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    No this one:

                    @heedaf said in Class issue in connect definition:

                    self.pushButton.clicked.connect(Dialog.SB_UP_click)

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    H 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      No this one:

                      @heedaf said in Class issue in connect definition:

                      self.pushButton.clicked.connect(Dialog.SB_UP_click)

                      H Offline
                      H Offline
                      heedaf
                      wrote on last edited by
                      #9

                      @SGaist wow! I'm loosing it. That is the one I meant to paste. The ...connect... line is also created by PYUIC in pycharm.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        Late night strikes again ! :-D
                        I misread the code.

                        Your slot should be something like:

                        def SB_UP_click(self):
                            print("SB UP")
                            self.ui.label.setText("HI")
                            print("got here")
                        

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • H Offline
                          H Offline
                          heedaf
                          wrote on last edited by
                          #11

                          Well I'm seriously confused. I swear I tried what you suggested and it didn't work. But what you posted works and I would really like to know what I did wrong since it is an obvious solution. Thank you for your help.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            You likely forgot the .ui between the self and .label.

                            Working with designer based UI requires some getting used to since you have all the code in the same file.

                            You have the Ui_XXX classes for the aesthetics of your widget (and which is auto generated) and the corresponding XXX that is for the logic you want to apply.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved