Is there a way to get all elements like (input, label , combox) with names and regarding value?



  • Hi all,

    i am searching for a way how I can get all Elements on a Widget which has focus.

    So for example I have a widget with 2 labels and some boxes, the first label is just for example "NOTE:" and the second would be "some hint",
    Also there are some comboboxes and input fields on this widget.

    So I am trying to get all element like the labels and the comoboxes with their name and the current set value.

    Is this somehow possible in qt? So that i maybe get a text output of the current set input elements ?

    So that the result could be:
    @
    label1 has value "NOTE"
    label2 has value "some hint"
    comboBox has value 2 selected
    @

    Many Thanks in advance

    Regards


  • Qt Champions 2016

    Hi

    Well If you make them in designer, you can access all via
    ui->

    If you new them yourself, you could just keep the pointers around ?

    else
    look at
    QObject::findChildren

    Something like

      QList<QWidget*> mylist= parent.findChildren<QWidget*>();
        foreach (const QWidget* current, mylist) {
           
        }
    

    parent being the widget you want to work with.
    But can you describe more what you have or show the program so maybe there is a better way.



  • You can walk through QObject tree:

    const QObjectList localChildren = ui->centralWidget->children();
    foreach (QObject *obj, localChildren) {
    QLabel *label = qobject_cast<QLabel *>(obj);
    if(label){
    qDebug() << label->text();
    }
    }

    Or do it recursively.



  • thanks for the replies, i will have a look into it.

    So for my use case i think i need to get first the widget which ahsa focus with:

    QWidget *focus =  QApplication::focusWidget();
    

    and then use yeckel 's code part

    const QObjectList localChildren = focus->children();
    foreach (QObject *obj, localChildren) {
    QLabel *label = qobject_cast<QLabel *>(obj);
    if(label){
    qDebug() << label->text();
    }
    QLabel *box= qobject_cast<QComboBox*>(obj);
    if ( box)
    {
    box->itemData(box->currentIndex());
    }
    }
    

    is this understanding correct?


  • Qt Champions 2016

    @BluTiGeS
    Almost correct,
    just also make sure the variable is the same type as the one toy try to cast to

    QLabel *box= qobject_cast<QComboBox*>(obj);
    if ( box)
    should be
    QComboBox*box= qobject_cast<QComboBox*>(obj);
    


  • Thanks, my fault copy paste error ;)

    I will give it a try


  • Qt Champions 2016

    @BluTiGeS
    Can I ask why you need to do it so dynamic?
    To avoid to rewrite the code that create the widgets in first place or
    you doing something fancy?



  • The background behind this is that i want to have a way to automate testing of the application running.

    So therefore i will have commands sending keyevents to the application and then a command to get the current screen values and get the as a XML or something similar, which than can be read by a python script for example to check if all values are set correct after the key sequence.

    I am using a pad having 6 input keys (embedded) so not a full keyboard.

    Do you have any better idea how to automate it?

    Important is that I must be able to to get all information for the currently view on the page on the display device in a readable file format.


  • Qt Champions 2016

    @BluTiGeS

    Ahh its module testing.
    then it makes sense :)

    Well there is
    http://doc.qt.io/qt-5/qtest-overview.html
    Never used it so can say if its good or anything.



  • @BluTiGeS
    I'm using http://funq.readthedocs.org/ for something similar.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.