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.
  • BDC_PatrickB Offline
    BDC_PatrickB Offline
    BDC_Patrick
    wrote on last edited by BDC_Patrick
    #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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #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) {
      }
      
      BDC_PatrickB 1 Reply Last reply
      4
      • mrjjM mrjj

        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) {
        }
        
        BDC_PatrickB Offline
        BDC_PatrickB Offline
        BDC_Patrick
        wrote on last edited by BDC_Patrick
        #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.

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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

          BDC_PatrickB 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            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.

            BDC_PatrickB Offline
            BDC_PatrickB Offline
            BDC_Patrick
            wrote on last edited by BDC_Patrick
            #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
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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
              • BDC_PatrickB BDC_Patrick

                @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.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on 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

                • Login

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