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. Call object by name from QString
Forum Updated to NodeBB v4.3 + New Features

Call object by name from QString

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.7k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Philipp DE
    wrote on 19 May 2020, 23:12 last edited by
    #1

    Hi,

    in php it is possible and I think it might be possible in qt5 as well. The following code will not work, but should show what my idea is. I have a line edit with object name e_city on my window.

    QString name = "e_city";
    
    if (ui->name->text() == "")
    {
                form_valid = false;
                ui->name->setStyleSheet("background-color:red;");
     }
    

    Is there any way to let name be handled as e_city to let that code anyhow running.

    P 1 Reply Last reply 20 May 2020, 00:37
    0
    • P Philipp DE
      19 May 2020, 23:12

      Hi,

      in php it is possible and I think it might be possible in qt5 as well. The following code will not work, but should show what my idea is. I have a line edit with object name e_city on my window.

      QString name = "e_city";
      
      if (ui->name->text() == "")
      {
                  form_valid = false;
                  ui->name->setStyleSheet("background-color:red;");
       }
      

      Is there any way to let name be handled as e_city to let that code anyhow running.

      P Offline
      P Offline
      Pl45m4
      wrote on 20 May 2020, 00:37 last edited by Pl45m4
      #2

      @Philipp-DE said in Call object by name from QString:

      my idea is

      But why? :)

      Yes, it's kinda possible with findChild. You can get the widget by object name, but only if you have set one before.

      If you want to set your stylesheet after something else happened, you can use Signals & Slots and trigger your action to set your styles to your widget.
      https://doc.qt.io/qt-5/signalsandslots.html#signals-and-slots


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      6
      • P Offline
        P Offline
        Philipp DE
        wrote on 20 May 2020, 07:04 last edited by
        #3

        Hi,

        this was my idea:

        I have a formular with a lot of lineedits and I need to validate them. There are ca. 20 fields and I think it is not good to copy the same code for all fields.

        As I worked the last 15 years with PHP, there I would have created an array with the 20 name of the objects, then loop that array and run that code for all objects.

        I was wondering if that way would be in qt5 a good way as well.

        Of course I could write a function and call it 20x for each field.

        Maybe with that background you can tell me what the best way would be.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Philipp DE
          wrote on 20 May 2020, 07:27 last edited by
          #4

          I have just seen the documentation. I think it would worked when I put all object Names in a StringList and then loop it in combination with findchild. But I am not sure if this is a good practise of programming in qt5.

          J 1 Reply Last reply 20 May 2020, 09:12
          0
          • B Offline
            B Offline
            Bonnie
            wrote on 20 May 2020, 07:29 last edited by Bonnie
            #5

            Then why don't you create an array with the pointers of the line edits?
            I don't think it is necessary to store their object names instead of their pointers.

            1 Reply Last reply
            6
            • P Philipp DE
              20 May 2020, 07:27

              I have just seen the documentation. I think it would worked when I put all object Names in a StringList and then loop it in combination with findchild. But I am not sure if this is a good practise of programming in qt5.

              J Offline
              J Offline
              JonB
              wrote on 20 May 2020, 09:12 last edited by
              #6

              @Philipp-DE
              As @Bonnie has said you can explicitly put your line edits into an array to iterate through. I don't think you will need to look them up by object name since you just presumably want to validate all of them, but if you do you can always put them in dictionary/map if you really want speed.

              If you can't be bothered to create an array/map, just use

              QList<QLineEdit *> allLineEdits = parentWidget.findChildren<QLineEdit *>();
              

              to get a list/iterate through all QLineEdits off some parent.

              1 Reply Last reply
              2
              • P Offline
                P Offline
                Philipp DE
                wrote on 20 May 2020, 19:15 last edited by
                #7

                I thanks for your help. This helps me to solv my problems.

                1 Reply Last reply
                0

                1/7

                19 May 2020, 23:12

                • Login

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