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. FindComponent in Qt?
Forum Updated to NodeBB v4.3 + New Features

FindComponent in Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 446 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.
  • DriftwoodD Offline
    DriftwoodD Offline
    Driftwood
    wrote on last edited by
    #1

    The code you're looking at is from a form I created using Lazarus (Object Pascal).

    On that form, I have 20 Textboxes (AMC_Edit1 thru AMC_Edit20), 80 RadioButtons (AMC_RadioButton1 thru AMC_RadioButton80).

    This bit of code uses FindComponent to find the Textboxes and RadioButtons, which allows me to loop thru them.

    My concern is whether or not Qt has something similar.

    var
      i, j, k : Integer;
      rcbc, tec: TComponent;
    begin
      j := 1;
      for i := 1 to 20 do
        begin
          tec := Form1.FindComponent('AMC_Edit' + IntToStr(i));
          if tec is TEdit then
          for k := 1 to 4 do
            begin
              rcbc := Form1.FindComponent('AMC_RadioButton' + IntToStr(j));
              Inc(j);
              if ((tec as TEdit).Text = (rcbc as TRadioButton).Caption) and (rcbc as TRadioButton).Checked then
                begin
                  (rcbc as TRadioButton).Font.Color := clLime;
                end;
            end;
        end;  
    end;
    
    JonBJ 1 Reply Last reply
    0
    • DriftwoodD Driftwood

      The code you're looking at is from a form I created using Lazarus (Object Pascal).

      On that form, I have 20 Textboxes (AMC_Edit1 thru AMC_Edit20), 80 RadioButtons (AMC_RadioButton1 thru AMC_RadioButton80).

      This bit of code uses FindComponent to find the Textboxes and RadioButtons, which allows me to loop thru them.

      My concern is whether or not Qt has something similar.

      var
        i, j, k : Integer;
        rcbc, tec: TComponent;
      begin
        j := 1;
        for i := 1 to 20 do
          begin
            tec := Form1.FindComponent('AMC_Edit' + IntToStr(i));
            if tec is TEdit then
            for k := 1 to 4 do
              begin
                rcbc := Form1.FindComponent('AMC_RadioButton' + IntToStr(j));
                Inc(j);
                if ((tec as TEdit).Text = (rcbc as TRadioButton).Caption) and (rcbc as TRadioButton).Checked then
                  begin
                    (rcbc as TRadioButton).Font.Color := clLime;
                  end;
              end;
          end;  
      end;
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Driftwood
      You can use QObject::findChildren() for this. You can search by type and/or by name.

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Look at findChild.

        Note that you might be able make the logic simpler by first checking what Qt has to offer like signals and slots.

        You should rather write down what should happen and then look if there's a simpler way than looping through all the components.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        DriftwoodD 1 Reply Last reply
        2
        • DriftwoodD Offline
          DriftwoodD Offline
          Driftwood
          wrote on last edited by
          #4

          Thank you, JonB. That's exactly what I needed.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Look at findChild.

            Note that you might be able make the logic simpler by first checking what Qt has to offer like signals and slots.

            You should rather write down what should happen and then look if there's a simpler way than looping through all the components.

            DriftwoodD Offline
            DriftwoodD Offline
            Driftwood
            wrote on last edited by
            #5

            @SGaist said in FindComponent in Qt?:

            Hi,

            Look at findChild.

            Note that you might be able make the logic simpler by first checking what Qt has to offer like signals and slots.

            You should rather write down what should happen and then look if there's a simpler way than looping through all the components.

            I'll do that, SGaist. I'm off and on with Qt, always falling back Lazarus (so easy to use). But Qt, I'm seeing time and again, is just as capable. I do need to study Signals/Slots, though.

            1 Reply Last reply
            0

            • Login

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