Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. How to move class method out of gui modul
Forum Updated to NodeBB v4.3 + New Features

How to move class method out of gui modul

Scheduled Pinned Locked Moved Unsolved Language Bindings
python
35 Posts 4 Posters 13.1k Views 2 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.
  • C Offline
    C Offline
    California
    wrote on last edited by
    #11

    Picture of GUI is missing ...
    alt text
    alt text

    1 Reply Last reply
    1
    • C Offline
      C Offline
      California
      wrote on last edited by
      #12

      Ok, this time succesed.
      alt text
      alt text
      alt text

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

        Just a quick note, you do realise that you are modifying a file which clearly states in its header that it can be regenerated and all your modification lost ?

        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
        • C Offline
          C Offline
          California
          wrote on last edited by
          #14

          Back with 3 modules. Take a look please

          main_1

          # python 2.7.9
          
          from PyQt4 import QtCore, QtGui
          from gui_1 import Ui_Form
          from apps_1 import my_app
          import sys
          
          
          if __name__ == "__main__":
          
              app = QtGui.QApplication(sys.argv)
          
              #a = my_app.print_situation(Ui_Form)
          
              gui_master = QtGui.QWidget()
              master = Ui_Form()
              master.setupUi(gui_master)
          
              master.radioButton.clicked.connect(my_app.print_situation)
          
              gui_master.show()
              sys.exit(app.exec_())
          
          

          gui_1

          # -*- coding: utf-8 -*-
          
          # Form implementation generated from reading ui file 'gui_2.ui'
          #
          # Created: Tue Jan 31 18:01:54 2017
          #      by: PyQt4 UI code generator 4.11.2
          #
          # WARNING! All changes made in this file will be lost!
          
          from PyQt4 import QtCore, QtGui
          
          try:
              _fromUtf8 = QtCore.QString.fromUtf8
          except AttributeError:
              def _fromUtf8(s):
                  return s
          
          try:
              _encoding = QtGui.QApplication.UnicodeUTF8
              def _translate(context, text, disambig):
                  return QtGui.QApplication.translate(context, text, disambig, _encoding)
          except AttributeError:
              def _translate(context, text, disambig):
                  return QtGui.QApplication.translate(context, text, disambig)
          
          class Ui_Form(object):
              def setupUi(self, Form):
                  Form.setObjectName(_fromUtf8("Form"))
                  Form.resize(300, 150)
                  self.radioButton = QtGui.QRadioButton(Form)
                  self.radioButton.setGeometry(QtCore.QRect(100, 75, 118, 26))
                  self.radioButton.setObjectName(_fromUtf8("radioButton"))
          
                  self.retranslateUi(Form)
                  QtCore.QMetaObject.connectSlotsByName(Form)
          
              def retranslateUi(self, Form):
                  Form.setWindowTitle(_translate("Form", "Form", None))
                  self.radioButton.setText(_translate("Form", "RadioButton", None))
          
          #    def print_situation(self):
          #        if self.radioButton.isChecked():
          #            print "Checked"
          #        else:
          #            print "Unchecked"
          
          
          if __name__ == "__main__":
              import sys
              app = QtGui.QApplication(sys.argv)
              Form = QtGui.QWidget()
              ui = Ui_Form()
              ui.setupUi(Form)
              Form.show()
              sys.exit(app.exec_())
          

          apps_1

          # python 2.7.9
          from PyQt4 import QtCore, QtGui
          from gui_1 import Ui_Form
          
          
          class my_app(QtGui.QRadioButton, Ui_Form):
              def __init__(self, parent=None):
                  super(my_app, self).__init__(parent)
                  self.setupUi(self)
                  self.radioButton = QtGui.QRadioButton()
          
              def print_situation(self):
                  if self.radioButton.isChecked():
                      print "Checked"
                  else:
                      print "Unchecked"
          

          error message

          Traceback (most recent call last):
            
          File "/home/pi/Desktop/Qt4/Project/01/Test_Class_2/main_1.py", line 19, in <module>
              
          master.radioButton.clicked.connect(my_app.print_situation)
          
          AttributeError: 
          'function' object has no attribute '__pyqtSignature__'
          
          jsulmJ 1 Reply Last reply
          0
          • C California

            Back with 3 modules. Take a look please

            main_1

            # python 2.7.9
            
            from PyQt4 import QtCore, QtGui
            from gui_1 import Ui_Form
            from apps_1 import my_app
            import sys
            
            
            if __name__ == "__main__":
            
                app = QtGui.QApplication(sys.argv)
            
                #a = my_app.print_situation(Ui_Form)
            
                gui_master = QtGui.QWidget()
                master = Ui_Form()
                master.setupUi(gui_master)
            
                master.radioButton.clicked.connect(my_app.print_situation)
            
                gui_master.show()
                sys.exit(app.exec_())
            
            

            gui_1

            # -*- coding: utf-8 -*-
            
            # Form implementation generated from reading ui file 'gui_2.ui'
            #
            # Created: Tue Jan 31 18:01:54 2017
            #      by: PyQt4 UI code generator 4.11.2
            #
            # WARNING! All changes made in this file will be lost!
            
            from PyQt4 import QtCore, QtGui
            
            try:
                _fromUtf8 = QtCore.QString.fromUtf8
            except AttributeError:
                def _fromUtf8(s):
                    return s
            
            try:
                _encoding = QtGui.QApplication.UnicodeUTF8
                def _translate(context, text, disambig):
                    return QtGui.QApplication.translate(context, text, disambig, _encoding)
            except AttributeError:
                def _translate(context, text, disambig):
                    return QtGui.QApplication.translate(context, text, disambig)
            
            class Ui_Form(object):
                def setupUi(self, Form):
                    Form.setObjectName(_fromUtf8("Form"))
                    Form.resize(300, 150)
                    self.radioButton = QtGui.QRadioButton(Form)
                    self.radioButton.setGeometry(QtCore.QRect(100, 75, 118, 26))
                    self.radioButton.setObjectName(_fromUtf8("radioButton"))
            
                    self.retranslateUi(Form)
                    QtCore.QMetaObject.connectSlotsByName(Form)
            
                def retranslateUi(self, Form):
                    Form.setWindowTitle(_translate("Form", "Form", None))
                    self.radioButton.setText(_translate("Form", "RadioButton", None))
            
            #    def print_situation(self):
            #        if self.radioButton.isChecked():
            #            print "Checked"
            #        else:
            #            print "Unchecked"
            
            
            if __name__ == "__main__":
                import sys
                app = QtGui.QApplication(sys.argv)
                Form = QtGui.QWidget()
                ui = Ui_Form()
                ui.setupUi(Form)
                Form.show()
                sys.exit(app.exec_())
            

            apps_1

            # python 2.7.9
            from PyQt4 import QtCore, QtGui
            from gui_1 import Ui_Form
            
            
            class my_app(QtGui.QRadioButton, Ui_Form):
                def __init__(self, parent=None):
                    super(my_app, self).__init__(parent)
                    self.setupUi(self)
                    self.radioButton = QtGui.QRadioButton()
            
                def print_situation(self):
                    if self.radioButton.isChecked():
                        print "Checked"
                    else:
                        print "Unchecked"
            

            error message

            Traceback (most recent call last):
              
            File "/home/pi/Desktop/Qt4/Project/01/Test_Class_2/main_1.py", line 19, in <module>
                
            master.radioButton.clicked.connect(my_app.print_situation)
            
            AttributeError: 
            'function' object has no attribute '__pyqtSignature__'
            
            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #15

            @California You should not move anything out of gui.py as it is auto-generated (as @SGaist already said). You should never touch auto-generated stuff.

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

            1 Reply Last reply
            0
            • C Offline
              C Offline
              California
              wrote on last edited by
              #16

              @jsulm copy that. What is max size of the module to work properly?

              jsulmJ 1 Reply Last reply
              0
              • C California

                @jsulm copy that. What is max size of the module to work properly?

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #17

                @California There is no such max size. But for your own modules you should try to keep them small (not more than some hundreds of lines if possible), else it will be hard to understand them or change something. How big auto-genereated modules are doesn't matter as you should not change anything there and usually you do not want to read this code.

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

                1 Reply Last reply
                1
                • C Offline
                  C Offline
                  California
                  wrote on last edited by
                  #18

                  @jsulm In my case gui.py is quite big because all methods related to GUI must be inside GUI class. The problem is, project is not finished yet, so this module will be even bigger after some time.
                  About diagrams, what tool or library should be used in my project?

                  jsulmJ 1 Reply Last reply
                  0
                  • C California

                    @jsulm In my case gui.py is quite big because all methods related to GUI must be inside GUI class. The problem is, project is not finished yet, so this module will be even bigger after some time.
                    About diagrams, what tool or library should be used in my project?

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #19

                    @California Is it really that important how big this auto-generated gui.py is?
                    Diagrams? See QtCharts.

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

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      California
                      wrote on last edited by
                      #20

                      @jsulm When QTCreator is used, your code is big as my very fast. Tab controll with few pages produce plenty of lines of code ...
                      Ok, few words about my project.
                      My project involves RaspberryPi as a Master in RS485 network. This application is like a sort of SCADA for home. On the other side of the network there are slaves responsible for diferent things like temperatures, humidity, consumption of watter, electricity, ...
                      So Master collets those data, do some calcullations, diagrams, storing data, ... , and most important do some graphic visualisation of everything in network.
                      At the momment my code covers three slaves, but there are three slaves more to include into aplication. Each slave contains Microchip microcontroller PIC18F458 ...
                      Slaves are already in function in real situation.

                      jsulmJ 1 Reply Last reply
                      0
                      • C California

                        @jsulm When QTCreator is used, your code is big as my very fast. Tab controll with few pages produce plenty of lines of code ...
                        Ok, few words about my project.
                        My project involves RaspberryPi as a Master in RS485 network. This application is like a sort of SCADA for home. On the other side of the network there are slaves responsible for diferent things like temperatures, humidity, consumption of watter, electricity, ...
                        So Master collets those data, do some calcullations, diagrams, storing data, ... , and most important do some graphic visualisation of everything in network.
                        At the momment my code covers three slaves, but there are three slaves more to include into aplication. Each slave contains Microchip microcontroller PIC18F458 ...
                        Slaves are already in function in real situation.

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #21

                        @California To keep your gui.py smaller you can create one widget for each tab in its own file (using designer, widget_1.py, widget_2.py, ...). Then add those widgets to the tabs.

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

                        1 Reply Last reply
                        1
                        • C Offline
                          C Offline
                          California
                          wrote on last edited by
                          #22

                          @jsulm that would be very usefull, do you have any example of how to do it or where it could be find to look?

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

                            What are your skills level with python ?

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

                            C 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              What are your skills level with python ?

                              C Offline
                              C Offline
                              California
                              wrote on last edited by
                              #24

                              @SGaist I started to learn python six months ago with my project described above

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

                                So you know how to split your code in several logical modules/files and import them to use them ?

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

                                C 1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  So you know how to split your code in several logical modules/files and import them to use them ?

                                  C Offline
                                  C Offline
                                  California
                                  wrote on last edited by
                                  #26

                                  @SGaist No probles with importing functions, more troubles with classes, specially with everything related to gui.
                                  My project is unique and it's not comercial.

                                  1 Reply Last reply
                                  0
                                  • C Offline
                                    C Offline
                                    California
                                    wrote on last edited by
                                    #27

                                    Smal change in function makes script running without an error but only "Unchecked" is printed out when radiobutton is checked or it is unchecked. How to fix this to have correct respond?

                                    # python 2.7.9
                                    from PyQt4 import QtCore, QtGui
                                    #from gui_1 import Ui_Form
                                    
                                    
                                    def print_situation(*args):
                                        radioButton = QtGui.QRadioButton()
                                        if radioButton.isChecked():
                                            print "Checked"
                                        else:
                                            print "Unchecked"
                                    
                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #28

                                      Importing functions or classes is exactly the same process.

                                      The thing that you seem to want to do is take apart class functions

                                      Take a look at the 3rd example from the PyQt 5 designer introduction documentation.

                                      Basically, you have your generated file from pyuic5, then you create your own class that will use the generated code. Finally a main.py where you will instantiate your custom widget(s) and start the application.

                                      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
                                      • mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #29

                                        Hi
                                        Wont

                                        def print_situation(*args):
                                            radioButton = QtGui.QRadioButton()
                                            if radioButton.isChecked():
                                                print "Checked"
                                            else:
                                                print "Unchecked"
                                        

                                        radioButton = QtGui.QRadioButton()
                                        create a new QRadioButton

                                        Each time and its unchecked by default.

                                        Hence it always says "Unchecked" ?

                                        1 Reply Last reply
                                        0
                                        • C Offline
                                          C Offline
                                          California
                                          wrote on last edited by
                                          #30

                                          @mrjj you are right I suppose

                                          @SGaist I tried to follow your point but ...
                                          code:

                                          # python 2.7.9
                                          from PyQt4 import QtCore, QtGui
                                          from PyQt4.QtGui import QCheckBox
                                          #from gui_5 import Ui_Form
                                          
                                          
                                          class My_app(QCheckBox):
                                              def __init__(self):
                                                  super(My_app, self).__init__()
                                                  # Set up the user interface from Designer.
                                                  self.setupUi(self)
                                                  self.checkBox = QtGui.QCheckBox(self)
                                                  # Connect up the buttons.
                                                  self.checkBox.clicked.connect(self.accept)
                                          
                                              def print_situation(self, *args):
                                                  if self.checkBox.checkState() == QtCore.Qt.Checked:
                                                      print "Checked"
                                                  else:
                                                      print "Unchecked"
                                          

                                          error:

                                          Traceback (most recent call last):
                                            File "/home/pi/Desktop/Qt4/Project/01/Test_Class/main_5.py", line 17, in <module>
                                              master.checkBox.clicked.connect(My_app.print_situation)
                                          AttributeError: 'function' object has no attribute '__pyqtSignature__'
                                          
                                          1 Reply Last reply
                                          0

                                          • Login

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