Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Get all Widgets of Type <Type> ?

    General and Desktop
    cpp qwidget qaction
    4
    7
    345
    Loading More Posts
    • 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_Patrick
      BDC_Patrick last edited by BDC_Patrick

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by 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_Patrick 1 Reply Last reply Reply Quote 4
        • BDC_Patrick
          BDC_Patrick @mrjj last edited by 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.

          JonB 1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion last edited by

            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 has to stay free or it will die.

            BDC_Patrick 1 Reply Last reply Reply Quote 1
            • BDC_Patrick
              BDC_Patrick @Christian Ehrlicher last edited by BDC_Patrick

              @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 Reply Quote 0
              • Christian Ehrlicher
                Christian Ehrlicher Lifetime Qt Champion last edited by

                @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 has to stay free or it will die.

                1 Reply Last reply Reply Quote 1
                • JonB
                  JonB @BDC_Patrick last edited by

                  @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 Reply Quote 1
                  • First post
                    Last post