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. How to map widgets created in QtCreator with an array?

How to map widgets created in QtCreator with an array?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 397 Views 2 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.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    Hello, I made a periodic table with QPushbuttons in QtCreator. And I want to map these buttons with an array, say *pb[115]. I used pb[1]=ui->hydrogen; pb[2]=ui->helium; But it takes tons of work and resulted in a long code. How can I do this elegantly?
    1.png

    Pl45m4P JonBJ 2 Replies Last reply
    0
    • M MasterBlade

      Hello, I made a periodic table with QPushbuttons in QtCreator. And I want to map these buttons with an array, say *pb[115]. I used pb[1]=ui->hydrogen; pb[2]=ui->helium; But it takes tons of work and resulted in a long code. How can I do this elegantly?
      1.png

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @MasterBlade

      Hi,

      first, a QVector< QPushButton * > or even a std::vector < QPushButton * > is always better than an array of QPushButton.
      Then, since your widgets are all buttons, you could use a QButtonGroup, because you created the actual button in Designer and probably only need a container to group them logically.
      Maybe you also want to have a look at QHash or QMap, where you could store the chemical element as string and map it to your button.


      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
      3
      • M MasterBlade

        Hello, I made a periodic table with QPushbuttons in QtCreator. And I want to map these buttons with an array, say *pb[115]. I used pb[1]=ui->hydrogen; pb[2]=ui->helium; But it takes tons of work and resulted in a long code. How can I do this elegantly?
        1.png

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @MasterBlade
        Further to @Pl45m4.

        This question comes up often. Designer won't put your widgets into some kind of array/list/map. But at runtime (e.g. just after setupUi() call) you can retrieve all or some of the widgets you have created via
        template <typename T> QList<T> QObject::findChildren(QAnyStringView name, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
        This is the secret/required to avoid having to write 115 lines of code, and keep it maintainable if things change.

        // `theParent` is whatever widget encloses all your QPushButtons,
        // e.g. might be a `QMainWindow` or some `QWidget`
        QList<QPushButton *> allButtons = theParent->findChildren<QPushButton *>();
        

        This gives you a list of all your QPushButtons. If you keep a widget container for just the element-name buttons area (suggest you do) you can use that as theParent. If not you need to remove, say, the OK and Cancel buttons from this list when you get it back.

        That's it if you just want a QList of them (can be indexed by an integer counter just like an array). If you get advanced, and decide you would like to be able to access a particular element's button from, say, the element name/text on the button, convert it over to a QMap<QString, QPushButton *> and then you can do things like:

        QPushButton *btnHelium = buttonsMap.value("He");
        
        Pl45m4P 1 Reply Last reply
        1
        • JonBJ JonB

          @MasterBlade
          Further to @Pl45m4.

          This question comes up often. Designer won't put your widgets into some kind of array/list/map. But at runtime (e.g. just after setupUi() call) you can retrieve all or some of the widgets you have created via
          template <typename T> QList<T> QObject::findChildren(QAnyStringView name, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
          This is the secret/required to avoid having to write 115 lines of code, and keep it maintainable if things change.

          // `theParent` is whatever widget encloses all your QPushButtons,
          // e.g. might be a `QMainWindow` or some `QWidget`
          QList<QPushButton *> allButtons = theParent->findChildren<QPushButton *>();
          

          This gives you a list of all your QPushButtons. If you keep a widget container for just the element-name buttons area (suggest you do) you can use that as theParent. If not you need to remove, say, the OK and Cancel buttons from this list when you get it back.

          That's it if you just want a QList of them (can be indexed by an integer counter just like an array). If you get advanced, and decide you would like to be able to access a particular element's button from, say, the element name/text on the button, convert it over to a QMap<QString, QPushButton *> and then you can do things like:

          QPushButton *btnHelium = buttonsMap.value("He");
          
          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @JonB said in How to map widgets created in QtCreator with an array?:

          This gives you a list of all your QPushButtons. If you keep a widget container for just the element-name buttons area (suggest you do) you can use that as theParent. If not you need to remove, say, the OK and Cancel buttons from this list when you get it back.

          Or cleaner:
          You rename all periodic table buttons to something like "btn_element_helium", "btn_element_uranium" etc. so that it's different from "other" control buttons which are also part of that form class.
          Then match only the element buttons using their object name pattern (yes, you can match object names by regex and wildcards)

          QRegularExpression re("*element*");
          QList<QPushButton *> allElementBtns = yourForm->findChildren<QPushButton *>(re);
          

          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
          0
          • M MasterBlade has marked this topic as solved on

          • Login

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