Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get all Widgets of Type <Type> ?



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


  • Lifetime Qt Champion

    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) {
    }
    


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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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



  • @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?