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. Is there a way to get all elements like (input, label , combox) with names and regarding value?

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

Scheduled Pinned Locked Moved General and Desktop
inputcomboboxoutputqwidgetfocus
10 Posts 3 Posters 9.5k 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
    BluTiGeS
    wrote on 15 Oct 2015, 14:59 last edited by
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 15 Oct 2015, 15:31 last edited by mrjj
      #2

      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.

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yeckel
        wrote on 15 Oct 2015, 15:41 last edited by
        #3

        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.

        1 Reply Last reply
        1
        • B Offline
          B Offline
          BluTiGeS
          wrote on 16 Oct 2015, 05:47 last edited by
          #4

          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?

          M 1 Reply Last reply 16 Oct 2015, 06:37
          0
          • B BluTiGeS
            16 Oct 2015, 05:47

            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?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 16 Oct 2015, 06:37 last edited by mrjj
            #5

            @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);
            
            1 Reply Last reply
            0
            • B Offline
              B Offline
              BluTiGeS
              wrote on 16 Oct 2015, 07:54 last edited by
              #6

              Thanks, my fault copy paste error ;)

              I will give it a try

              M 1 Reply Last reply 16 Oct 2015, 08:45
              0
              • B BluTiGeS
                16 Oct 2015, 07:54

                Thanks, my fault copy paste error ;)

                I will give it a try

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 16 Oct 2015, 08:45 last edited by
                #7

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

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  BluTiGeS
                  wrote on 16 Oct 2015, 09:58 last edited by
                  #8

                  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.

                  M Y 2 Replies Last reply 16 Oct 2015, 10:02
                  0
                  • B BluTiGeS
                    16 Oct 2015, 09:58

                    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.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 16 Oct 2015, 10:02 last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • B BluTiGeS
                      16 Oct 2015, 09:58

                      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.

                      Y Offline
                      Y Offline
                      yeckel
                      wrote on 16 Oct 2015, 18:23 last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      8/10

                      16 Oct 2015, 09:58

                      • Login

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