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. Grouping widgets into array
QtWS25 Last Chance

Grouping widgets into array

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.0k 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.
  • Y Offline
    Y Offline
    y0yster
    wrote on last edited by
    #1

    Let me sketch the problem a bit.
    With the help of Qt Creator I've created a set of QSpinBoxes, QCheckBoxes and QComboBoxes. All QSpinBoxes have a standard name e.g. sb_x where x is the next number in line. And for the sake of the description let's assume that I have 16 of them. Inside code when I'm referring to specific widget I write:

    @ui->sp_x->setValue(5);@

    When I want to set new value to all 16 of them it's getting a bit messy. Is there a way to do this by loop? If so, what else should I do to make it possible?

    Please don't say to write the widgets by hand, I mean in code. Yes, then you have the full control over them but it requires some time. Using Qt Creator I just do it fast and it is convenient. I have written a chunk of code in WinAPI and I just don't want to go through it again.

    It would be nice if this could work for other widgets. What is important is also the order of the widgets in this array.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      msue
      wrote on last edited by
      #2

      How about something like this:
      @std::vector<QSpinBox *> spins{ui.sp_0, ui.sp_1, ui,sp_2,};
      std::for_each(spins.begin(), spins.end(), [](QSpinBox *spn, const double x){ spn->setValue(x); });
      @

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        y0yster
        wrote on last edited by
        #3

        Yes, this can definitely do it. Thanks.

        Meanwhile, I have come up with another question. Which is actually a variation of this what I've described at the beginning. Let's say I don't change name of the spin boxes and those have some default names. Not necessary consistent names. Some part of them I have grouped in the frame which is vertically aligned. Is there a way to access the widgets inside the frame in some ordered way.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          msue
          wrote on last edited by
          #4

          You can find objects by their name using QObject::findChild, and so fill the std::vector<QSpinBox *> by code instead of by hand.

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            y0yster
            wrote on last edited by
            #5

            Yes, but as you said I have to use findChild and feed it with object's name.
            As I've written in my second post the names are random like sb_1, sb_5, sb_7, .... and I have few of theme. The method findChild would force me to rename all of the items.
            I found childAt() method which uses coordinates in widget's coordinate system. Unless I'm wrong, it uses "visible" coordinate system. What would actually be a very good solution to what I need is a method which would take exactly the same parameters as childAt() but interpret them as "grid" coordinates.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Marsellus
              wrote on last edited by
              #6

              Hi,

              If you use a QGridLayout to organize your widgets, you can use the itemAtPosition method :
              @QLayoutItem * itemAtPosition ( int row, int column ) const;@

              You will be able to get the corresponding widget thanks to the QLayoutItem::widget() method.

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

                Hi,

                Just use findChildren<QSpinBox*> and you'll have the list of all QSpinBoxes

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

                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