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. how to modify the ui from outside the instance that created the ui?

how to modify the ui from outside the instance that created the ui?

Scheduled Pinned Locked Moved Unsolved Qt for Python
12 Posts 4 Posters 577 Views
  • 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.
  • A Offline
    A Offline
    adrian88888888
    wrote on last edited by adrian88888888
    #1

    Hi!

    I want to insert in a textEdit that MyObject was created, like this:

    self.ui.textEdit.insertHtml('MyObject was created<br>')
    

    But how I change the textEdit from outside the instance that creates the ui?

    For example: if I want to change the textEdit from MyObject, should I do this?:

    class MyObject:
        def __init__(self):
            MainWindow.ui.textEdit.insertHtml('An Obj was created<br>')
    

    or this?:

    class MyObject:
        def __init__(self, MainWindow): # <===I put MainWindow here
            MainWindow.ui.textEdit.insertHtml('An Obj was created<br>')
    

    or this?:

    class MyObject(MainWindow): # <===I put MainWindow here
        def __init__(self):
            self.ui.textEdit.insertHtml('An Obj was created<br>') # <===I put self here
    

    (MainWindow is the class that creates the ui)

    As you can see the problem is that I don't know how to reference the instance of MainWindow from MyObject

    This is how the overall code looks like if you want to know:

    *lots of imports*
    *lots of imports*
    *lots of imports*
    
    class MainWindow(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            ...
            ...
            ...
    
    class MyObject ():
        def __init__(self):
        MainWindow.ui.textEdit.insertHtml('An Obj was created<br>')
        ...
        ...
        ...
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    

    Thanks!

    jsulmJ 1 Reply Last reply
    0
    • A adrian88888888

      Hi!

      I want to insert in a textEdit that MyObject was created, like this:

      self.ui.textEdit.insertHtml('MyObject was created<br>')
      

      But how I change the textEdit from outside the instance that creates the ui?

      For example: if I want to change the textEdit from MyObject, should I do this?:

      class MyObject:
          def __init__(self):
              MainWindow.ui.textEdit.insertHtml('An Obj was created<br>')
      

      or this?:

      class MyObject:
          def __init__(self, MainWindow): # <===I put MainWindow here
              MainWindow.ui.textEdit.insertHtml('An Obj was created<br>')
      

      or this?:

      class MyObject(MainWindow): # <===I put MainWindow here
          def __init__(self):
              self.ui.textEdit.insertHtml('An Obj was created<br>') # <===I put self here
      

      (MainWindow is the class that creates the ui)

      As you can see the problem is that I don't know how to reference the instance of MainWindow from MyObject

      This is how the overall code looks like if you want to know:

      *lots of imports*
      *lots of imports*
      *lots of imports*
      
      class MainWindow(QMainWindow):
          def __init__(self):
              QMainWindow.__init__(self)
              self.ui = Ui_MainWindow()
              self.ui.setupUi(self)
              ...
              ...
              ...
      
      class MyObject ():
          def __init__(self):
          MainWindow.ui.textEdit.insertHtml('An Obj was created<br>')
          ...
          ...
          ...
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
          window = MainWindow()
          window.show()
          sys.exit(app.exec_())
      

      Thanks!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @adrian88888888 said in how to modify the ui from outside the instance that created the ui?:

      For example: if I want to change the textEdit from MyObject, should I do this?:

      No you should not.
      You should instead define an interface to set the text in the class where this UI is located.

      class MainWindow...
          def setText(self, text):
              self.ui.textEdit.insertHtml(text)
      

      And then use this interface (setText in my example) from other classes to change the text. Never expose internal implementation details of a class to the outside world!

      In Qt you also could use signals slots for that if it makes sense.

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

      JonBJ 1 Reply Last reply
      1
      • jsulmJ jsulm

        @adrian88888888 said in how to modify the ui from outside the instance that created the ui?:

        For example: if I want to change the textEdit from MyObject, should I do this?:

        No you should not.
        You should instead define an interface to set the text in the class where this UI is located.

        class MainWindow...
            def setText(self, text):
                self.ui.textEdit.insertHtml(text)
        

        And then use this interface (setText in my example) from other classes to change the text. Never expose internal implementation details of a class to the outside world!

        In Qt you also could use signals slots for that if it makes sense.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @jsulm said in how to modify the ui from outside the instance that created the ui?:

        And then use this interface (setText in my example) from other classes to change the text.

        I cannot imagine another class which would/should need access to an application's QMainWindow. It would either require passing the main window instance as a parameter, or finding the single top-level main window instance via QApplication::topLevelWidgets(), or casting a parent to the MainWindow class. None of which are recommended, or should be necessary.

        If the OP really wants some other class to cause the main window to alter, either the main window should fetch the result from the other class or the other class should send a signal which the main window has slotted onto.

        Or, the architecture should be rethought, it's not clear why this other class would even need to do this.

        I trust @jsulm will agree with this approach :)

        jsulmJ 1 Reply Last reply
        0
        • JonBJ JonB

          @jsulm said in how to modify the ui from outside the instance that created the ui?:

          And then use this interface (setText in my example) from other classes to change the text.

          I cannot imagine another class which would/should need access to an application's QMainWindow. It would either require passing the main window instance as a parameter, or finding the single top-level main window instance via QApplication::topLevelWidgets(), or casting a parent to the MainWindow class. None of which are recommended, or should be necessary.

          If the OP really wants some other class to cause the main window to alter, either the main window should fetch the result from the other class or the other class should send a signal which the main window has slotted onto.

          Or, the architecture should be rethought, it's not clear why this other class would even need to do this.

          I trust @jsulm will agree with this approach :)

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JonB My suggestion isn't restricted to this specific case. It is more general.
          In this specific case signals/slots would help to decouple main window and other classes even more.

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

          JonBJ 1 Reply Last reply
          2
          • jsulmJ jsulm

            @JonB My suggestion isn't restricted to this specific case. It is more general.
            In this specific case signals/slots would help to decouple main window and other classes even more.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @jsulm
            Yes indeed that's fine. In this particular case, i would ask the OP to consider what it is which requires another object/class to need to access the main window and call a method in it directly, as that at least is often a sign of a bad approach....

            1 Reply Last reply
            0
            • A Offline
              A Offline
              adrian88888888
              wrote on last edited by adrian88888888
              #6

              @jsulm and @JonB this is really useful to me because i didn't know about the concept of decoupling just a minute ago

              My problem its more general then:

              I have 750 objects created from the same class, his attributes are stabilized by like 10 modules in the constructor
              I want to work with one object at a time with a ui

              So I want from the ui to:
              -see the attributes
              -edit them
              -apply the edit
              -repeat the process with the next object

              And i want to show on what step of the process i am in a textEdit, for example:

              log('Analyzing object number X...')
              log('His name its not valid...')
              log('Assigning automatically a new name...')
              log('Asking to the user to define his color with the ui...')
              

              My approach was wrong because of coupling

              What's the right way then?

              Because if for example I use the class of the ui to know about the attributes
              of each object to show/edit them, then i'm coupling the classes right?, then
              that's not the solution, I guess?

              The 2 classes have to interact somehow

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

                Hi,

                What are these 750 objects ?

                From the looks of it, you could store these various settings in a model and then use QDataWidgetMapper to navigate through the model and do the edition.

                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
                • A adrian88888888

                  @jsulm and @JonB this is really useful to me because i didn't know about the concept of decoupling just a minute ago

                  My problem its more general then:

                  I have 750 objects created from the same class, his attributes are stabilized by like 10 modules in the constructor
                  I want to work with one object at a time with a ui

                  So I want from the ui to:
                  -see the attributes
                  -edit them
                  -apply the edit
                  -repeat the process with the next object

                  And i want to show on what step of the process i am in a textEdit, for example:

                  log('Analyzing object number X...')
                  log('His name its not valid...')
                  log('Assigning automatically a new name...')
                  log('Asking to the user to define his color with the ui...')
                  

                  My approach was wrong because of coupling

                  What's the right way then?

                  Because if for example I use the class of the ui to know about the attributes
                  of each object to show/edit them, then i'm coupling the classes right?, then
                  that's not the solution, I guess?

                  The 2 classes have to interact somehow

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @adrian88888888
                  First, always follow @SGaist's advice. You need to think through the big picture of whatever you're trying to achieve.

                  For the earlier, specific question of showing something from elsewhere in the main window. If you have some action to report from somewhere else in code, instead of having that access the main window directly and set text, think about having the code emit a signal, with a parameter conveying what has happened. The main window connects a slot to that, and updates the text appropriately when it gets called. Qt code is all about signals & slots.

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    adrian88888888
                    wrote on last edited by
                    #9

                    Okey now i'm hesitating about my general approach
                    Can you tell me if my approach its the best?

                    I'm a music producer, I have 750 song projects in 750 folders, each folder has an id number, readme files, etc(caotic stuff)

                    So I have to order this caos

                    So I figured why not make 750 objects and each one represents a project, in their creation i gatter the data and make them their attributes, like:
                    attributes:
                    -his directory
                    -list of thing to do with that project(todos basically)
                    -i need to know which projects are good and which are bad, so each project has that written somewhere, i want that as an attribute
                    -the time it was created
                    -and more hard to explain things

                    Then I process the data/attributes with the help of the Qt gui and scripts, I put the data/attributes in a database, and this chaos should be useful again

                    Should I keep going in my direction and learn about signals/slots making the porjectObject communicate with the UI?, or should I use QDataWidgetMapper like @SGaist suggested? or there's something else?

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

                      Ok, so in fact you want to build a parser to create a database, correct ?

                      Are all these musical project folders stored in one directory ?
                      Depending on that, there might be several ways to write your application.

                      Take the time to learn both signals and slots and model views. They are not mutually exclusive, quite the contrary.

                      You should take the time as well to look at Qt's SQL module.

                      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
                      • A adrian88888888

                        Okey now i'm hesitating about my general approach
                        Can you tell me if my approach its the best?

                        I'm a music producer, I have 750 song projects in 750 folders, each folder has an id number, readme files, etc(caotic stuff)

                        So I have to order this caos

                        So I figured why not make 750 objects and each one represents a project, in their creation i gatter the data and make them their attributes, like:
                        attributes:
                        -his directory
                        -list of thing to do with that project(todos basically)
                        -i need to know which projects are good and which are bad, so each project has that written somewhere, i want that as an attribute
                        -the time it was created
                        -and more hard to explain things

                        Then I process the data/attributes with the help of the Qt gui and scripts, I put the data/attributes in a database, and this chaos should be useful again

                        Should I keep going in my direction and learn about signals/slots making the porjectObject communicate with the UI?, or should I use QDataWidgetMapper like @SGaist suggested? or there's something else?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @adrian88888888
                        They are not either-ors. You will end up using signals & slots soon enough with Qt. The issue of sending a message to the main window is a small detail.

                        You will want to create a model for your song projects. Think rows for the projects, "attributes" exposed as columns. You can then use Qt's QDataWidgetMapper to allow the user to view, and edit if desired, one project at a time, with all its attributes as widgets. Read about Qt's Model/View architecture first: https://doc.qt.io/qt-5/model-view-programming.html & https://doc.qt.io/qt-5/modelview.html .

                        1 Reply Last reply
                        1
                        • A Offline
                          A Offline
                          adrian88888888
                          wrote on last edited by
                          #12

                          @JonB making a model of the 750 items make sense if there's data, but there's some data, the rest has to be gathered

                          also when the program gets a piece of data that can't figure out what is(using RegExp), then it has to be shown to the user(using de ui) so it can be decided manually what is

                          so i'm using the ui to make half of the data, so then i can make the model/data-base

                          @SGaist i just googled what is a 'parser', i didn't know that this was a common problem, i'm so newbie, I would have said it from the start...

                          and yes, the projects are inside the same directory

                          now i know what's the next step, learning about signals/slots and QDataWidgetMapper, i know already about model views

                          Thanks!

                          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