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 access a layout and layout's children after loading UI from QtDesigner?
Forum Updated to NodeBB v4.3 + New Features

How to access a layout and layout's children after loading UI from QtDesigner?

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 3 Posters 1.9k 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
    Aya Abeliovich
    wrote on last edited by
    #1

    Hey all.

    I'm still a beginner when it comes to using Qt, and I've only just started using layouts and spacers to arrange my windows.
    I use QtDesigner to build my ui and then connect my widgets to their functionalities with python.
    Before using layouts, I could simply access any QWidget in the window by name as if it were a class property.
    Now, since they're all nestled in layouts, I can't seem to find them. I tried accessing the layout as a class property and then using findChild on it, but findChild raised an exception (I thought it was supposed to receive (str, QObject) but it doesn't seem to like this input)

    What is the correct way of tackling this? How do I use findChild properly?

    Thanks!

    JonBJ 1 Reply Last reply
    0
    • A Aya Abeliovich

      Hey all.

      I'm still a beginner when it comes to using Qt, and I've only just started using layouts and spacers to arrange my windows.
      I use QtDesigner to build my ui and then connect my widgets to their functionalities with python.
      Before using layouts, I could simply access any QWidget in the window by name as if it were a class property.
      Now, since they're all nestled in layouts, I can't seem to find them. I tried accessing the layout as a class property and then using findChild on it, but findChild raised an exception (I thought it was supposed to receive (str, QObject) but it doesn't seem to like this input)

      What is the correct way of tackling this? How do I use findChild properly?

      Thanks!

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

      @Aya-Abeliovich
      Hello and welcome.

      Using layouts should not affect this.

      There are several ways of using UI-designer stuff from Python. Tell us whether you are using PySide2/PyQt5/PySide6/PyQt6. Also tell us just how you you "load" the designed UI at runtime: you might be generating .py file from .ui file (sounds like you are not), you might be using some loadUi() call on .ui file at runtime, we need to see.

      IMHO the loadUI()-at-runtime and/or the need to use findChild() are not the "friendliest" way to choose to develop your code. I prefer the way that generates a .py file for you from your .ui file, a much nicer "experience" when coding. This does mean you have to run an extra step whenever you alter your .ui file in Designer, but I think it's worth it. I don't know, but if you work inside Creator I think/hope that may be automated for you now, as it would be if you were using C++.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Aya Abeliovich
        wrote on last edited by
        #3

        Hey Jon,
        thank you for your quick reply!
        I am using PyQt5, and I really was using loadui. I haven't thought of converting my ui file to a .py file.
        Now trying to use the converted file, I ran into another problem: first of all, it looks like it converted it to Python2 code. That's not too bad and can be easily fixed. My problem is that it's unclear how to use the generated class - it has no "init" function of its own.
        Screenshot 2022-09-29 152942.png

        Could you provide any insight on how to use it?
        Couldn't find any proper explanations by Googling.

        Thanks a bunch.
        Aya

        JonBJ 1 Reply Last reply
        0
        • A Aya Abeliovich

          Hey Jon,
          thank you for your quick reply!
          I am using PyQt5, and I really was using loadui. I haven't thought of converting my ui file to a .py file.
          Now trying to use the converted file, I ran into another problem: first of all, it looks like it converted it to Python2 code. That's not too bad and can be easily fixed. My problem is that it's unclear how to use the generated class - it has no "init" function of its own.
          Screenshot 2022-09-29 152942.png

          Could you provide any insight on how to use it?
          Couldn't find any proper explanations by Googling.

          Thanks a bunch.
          Aya

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

          @Aya-Abeliovich said in How to access a layout and layout's children after loading UI from QtDesigner?:

          first of all, it looks like it converted it to Python2 code. That's not too bad and can be easily fixed.

          ? Certainly not, Python 3 only!

          Can't find PyQt5 Riverbank links any longer. Have a read of https://doc.bccnsoft.com/docs/PyQt5/designer.html#using-the-generated-code. There should be loads of examples out there somewhere.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            friedemannkleint
            wrote on last edited by
            #5

            Please take a look at Using .ui files from Designer or QtCreator with QUiLoader and pyside6-uic

            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