Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get all Widgets of Type <Type> ?
QtWS25 Last Chance

Get all Widgets of Type <Type> ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
cppqwidgetqaction
7 Posts 4 Posters 3.4k 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.
  • B Offline
    B Offline
    BDC_Patrick
    wrote on 11 Jul 2021, 14:11 last edited by BDC_Patrick 7 Nov 2021, 14:12
    #1

    Hi there..
    i think it is a dumb Question but..
    How to get ALL QWidgets inside a ui of a specific Type (label, combobox..etc) into an Array?
    Same for ALL actions inside a ui?

    like (pseudo)

    arr = ui -> getWidgets<Type>
    

    I need to write my own localisation system, which works for single objects.. but i don´t want to add each Widget and Action by hand.. so.. i need to collect them at startup automatically.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 11 Jul 2021, 14:15 last edited by mrjj 7 Nov 2021, 14:15
      #2

      Hi
      Its not a dumb question. I do use it sometimes.
      The function is called findChildren

      QList<QWidget *> widgets = parent->findChildren<QWidget *>();
      foreach(QWidget *w, widgets) {
      }
      
      B 1 Reply Last reply 11 Jul 2021, 15:15
      4
      • M mrjj
        11 Jul 2021, 14:15

        Hi
        Its not a dumb question. I do use it sometimes.
        The function is called findChildren

        QList<QWidget *> widgets = parent->findChildren<QWidget *>();
        foreach(QWidget *w, widgets) {
        }
        
        B Offline
        B Offline
        BDC_Patrick
        wrote on 11 Jul 2021, 15:15 last edited by BDC_Patrick 7 Nov 2021, 15:18
        #3

        @mrjj Thanks.
        But, this only works, if i put the code directly into the construct function of the Window itself (after ui->setupUi...).

        But, i thought more of a Function in a seperated file, that all Windows can make use of.

        J 1 Reply Last reply 11 Jul 2021, 17:24
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 11 Jul 2021, 15:18 last edited by
          #4

          Why should this be needed? You can only get the children of your widget - there is no reason to access other widget's children from outside the class the widget is managed from.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          B 1 Reply Last reply 11 Jul 2021, 15:20
          1
          • C Christian Ehrlicher
            11 Jul 2021, 15:18

            Why should this be needed? You can only get the children of your widget - there is no reason to access other widget's children from outside the class the widget is managed from.

            B Offline
            B Offline
            BDC_Patrick
            wrote on 11 Jul 2021, 15:20 last edited by BDC_Patrick 7 Nov 2021, 15:23
            #5

            @Christian-Ehrlicher The reason is, to avoid a few hundret lines of code extra, in each source file..

            So..
            MainWindow, DialogWindow, UserWindow..etc
            Can include a
            localize_win.h
            and call the function "localize"..
            And this function gets all Widgets and Actions of the caller Class to localize them.

            With @mrjj ´s code, i need to add the whole localization function into each construction of each Window, Dialog..etc
            And this Function is getting really heavy and large, cause i need to write it for all Text types of widgets.. means.. labels, buttons, comboboxes, tooltip type widgets..etc..

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 11 Jul 2021, 17:24 last edited by
              #6

              @BDC_Patrick said in Get all Widgets of Type <Type> ?:

              And this function gets all Widgets and Actions of the caller Class to localize them.

              You should read about how localization is done in Qt. You're doing it wrong. When you want to change the language during program runtime then you have to write code, no other chance.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • B BDC_Patrick
                11 Jul 2021, 15:15

                @mrjj Thanks.
                But, this only works, if i put the code directly into the construct function of the Window itself (after ui->setupUi...).

                But, i thought more of a Function in a seperated file, that all Windows can make use of.

                J Offline
                J Offline
                JonB
                wrote on 11 Jul 2021, 17:24 last edited by
                #7

                @BDC_Patrick said in Get all Widgets of Type <Type> ?:

                But, this only works, if i put the code directly into the construct function of the Window itself (after ui->setupUi...).

                Why? It's QObject::findChilden(), you can use it for any widget.

                But, i thought more of a Function in a seperated file, that all Windows can make use of.

                If you really want it in a separate file then pass the parent widget as a parameter.

                Are you using Qt's support for cross-language?

                1 Reply Last reply
                1

                6/7

                11 Jul 2021, 17:24

                • Login

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