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. When you have similar buttons in the QPushButton class, is it a way I can put them in an array?
QtWS25 Last Chance

When you have similar buttons in the QPushButton class, is it a way I can put them in an array?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 1.6k 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
    BernWillChris
    wrote on last edited by
    #1

    Hi Qt Programmers,

    When I was designing a calculator, I had buttons from 0 - 9 just for the digits alone. I was thinking that is has to be a more efficient way than just copying and pasting those buttons 10 times to create those objects. It has to be a way that I can use a for-loop or something?
    Can I get some tips? :)

    Thanks so much!!

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Wel... yes, you can use a for loop:

      for(int i=0; i < 10; ++i)
         yourLayout->addWidget(new QPushButton(QString::number(i)));
      
      B 1 Reply Last reply
      1
      • M Offline
        M Offline
        mcosta
        wrote on last edited by
        #3

        To add something to @Chris-Kawa I think in your case could be also useful to use QSignalMapper

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply
        1
        • Chris KawaC Chris Kawa

          Wel... yes, you can use a for loop:

          for(int i=0; i < 10; ++i)
             yourLayout->addWidget(new QPushButton(QString::number(i)));
          
          B Offline
          B Offline
          BernWillChris
          wrote on last edited by
          #4

          @Chris-Kawa

          Thanks Chris for the reply!! That works, but it doesn't give 10 different names to the objects. What I mean is:

          QPushButton button1 = new QPushButton():
          QPushButton button2 = new QPushButton()
          :QPushButton button3 = new QPushButton():

          .......

          So basically objects button1, button2, and button3 that I can simply call and do some things with.

          If only this was legal.

          for(int x = 0, x < 10; x++) {

          = new QPushButton(QSring::number(x))

          }

          that way I can have button[0], button[1], button[2], and so on and so.

          H 1 Reply Last reply
          0
          • B BernWillChris

            @Chris-Kawa

            Thanks Chris for the reply!! That works, but it doesn't give 10 different names to the objects. What I mean is:

            QPushButton button1 = new QPushButton():
            QPushButton button2 = new QPushButton()
            :QPushButton button3 = new QPushButton():

            .......

            So basically objects button1, button2, and button3 that I can simply call and do some things with.

            If only this was legal.

            for(int x = 0, x < 10; x++) {

            = new QPushButton(QSring::number(x))

            }

            that way I can have button[0], button[1], button[2], and so on and so.

            H Offline
            H Offline
            Huulivoide
            wrote on last edited by
            #5

            @BernWillChris

            The is nothing illegal about that for-loop, except that you need
            to have array of QPushButton pointers, and QString is misspelled.

            B 1 Reply Last reply
            1
            • H Huulivoide

              @BernWillChris

              The is nothing illegal about that for-loop, except that you need
              to have array of QPushButton pointers, and QString is misspelled.

              B Offline
              B Offline
              BernWillChris
              wrote on last edited by
              #6

              @Huulivoide

              Thanks!!! Yeah, you right. It wasn't misspelled when I first tried it. I will see what I can do to establish an array of QPushButton pointers.

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #7
                QList<QPushButton*> buttons;
                for(int x = 0; x < 10; ++x)
                    buttons << new QPushButton(QString::number(x));
                
                
                1 Reply Last reply
                1

                • Login

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