Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Creating an array of buttons
Forum Updated to NodeBB v4.3 + New Features

Creating an array of buttons

Scheduled Pinned Locked Moved Mobile and Embedded
12 Posts 4 Posters 23.3k 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.
  • B Offline
    B Offline
    BorahAnshuman
    wrote on 21 Apr 2011, 06:09 last edited by
    #1

    hello friends

    I am creating an array of buttons having 7 rows 7 colums..
    now i trying to display first seven buttons in one row after which it goes to second row and display next seven button..and so on..

    i have wrriten a code for this..its working but problem is that all the buttons are displayed in one row only..so if anyone suggest me to resolve that problem..

    i am sending my code along with this..

    @QWidget *centralWidget = new QWidget;
    selectedDate=QDate::currentDate();
    int count=1,i,j;
    QPushButton *button[10][10];
    QHBoxLayout *controlsLayout = new QHBoxLayout;
    for(i=0;i<7;i++)
    {
    for(j=0;j<7;j++)
    {
    if(count<=42)
    {

                button[i][j] = new QPushButton("p");
    
                button[i][j]->resize(40,40);
    
                button[i][j]->move(40*j, 40*i);
    
                button[i][j]->show();
    
                controlsLayout->addWidget(button[i][j]);
    
                centralWidget->setLayout(controlsLayout);
    
                setCentralWidget(centralWidget);
    
                count++;
            }
    
        }
    
    }
    

    }@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Aleskey78
      wrote on 21 Apr 2011, 06:22 last edited by
      #2

      all the buttons are displayed in one row only..
      You add the buttons into "controlsLayout" which is "QHBoxLayout":http://doc.qt.nokia.com/latest/qhboxlayout.html
      "The QHBoxLayout class lines up widgets horizontally..."

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Aleskey78
        wrote on 21 Apr 2011, 06:37 last edited by
        #3

        Just use "QGridLayout":http://doc.qt.nokia.com/latest/qgridlayout.html instead of QHBoxLayout
        @ QWidget *centralWidget = new QWidget;
        int count=1,i,j;
        QPushButton *button[10][10];
        QGridLayout *controlsLayout = new QGridLayout;
        for(i=0;i<7;i++)
        {
        for(j=0;j<7;j++)
        {
        if(count<=42)
        {

                        button[i][j] = new QPushButton("p");
                        button[i][j]->resize(40,40);
                        button[i][j]->move(40*j, 40*i);
                        button[i][j]->show();
                        controlsLayout->addWidget(button[i][j], i, j);
                        count++;
                    }
                }
            }
            centralWidget->setLayout(controlsLayout);
            setCentralWidget(centralWidget);@
        
        1 Reply Last reply
        0
        • B Offline
          B Offline
          BorahAnshuman
          wrote on 21 Apr 2011, 08:49 last edited by
          #4

          thanks Aleskey..its working now....the only thing is that ...hw can i decrease the space between each row...

          As i had used all this in my code ...but its not working..

          QGridLayout::setSpacing()
          QGridLayout ::setHorizontalSpacing()
          QGridLayout ::setVerticalSpacing()

          space remain same...
          with regards
          Anshuman

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Aleskey78
            wrote on 21 Apr 2011, 09:55 last edited by
            #5

            Maybe it will be better if you use no layouts at all:
            @ button[i][j] = new QPushButton("p", centralWidget);

                            button[i][j]->resize(40,40);
            
                            button[i][j]->move(40*j, 40*i);
            
                            button[i][j]->show();
            
                            //controlsLayout->addWidget(button[i][j], i, j);
            

            ...
            //centralWidget->setLayout(controlsLayout);
            setCentralWidget(centralWidget);
            @

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on 21 Apr 2011, 10:07 last edited by
              #6

              Obviously moving and resizing the buttons and putting them in a layout that manages their size and location is nonsense, but I think using a layout is the way to go. I'd rather dump the resize and the move. What happens if you use your application on a high-res display with a small dot pitch? You'll end up with tiny buttons, making the application unusable.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                BorahAnshuman
                wrote on 21 Apr 2011, 10:17 last edited by
                #7

                yeh aleskey its work..but the thing is that i am using this array of buttons in creating my custom calender...so for that i hv to use layout..otherwise..nw if i try to use textbrowser below after all the button are set in rows and columns..hw can i manage to do it so..thats means the USERInterface design i want is similar to calenderWidget the only difference is that i use buttons for representing each cell..

                with regards
                Anshuman

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Aleskey78
                  wrote on 21 Apr 2011, 10:56 last edited by
                  #8

                  As i had used all this in my code …but its not working..
                  it is working, I have tested:

                  @QWidget *centralWidget = new QWidget;
                  int count=1,i,j;
                  QPushButton *button[10][10];
                  QGridLayout *controlsLayout = new QGridLayout;
                  for(i=0;i<7;i++)
                  {
                  for(j=0;j<7;j++)
                  {
                  if(count<=42)
                  {
                  button[i][j] = new QPushButton("p");
                  controlsLayout->addWidget(button[i][j], i, j);
                  count++;
                  }
                  }
                  }
                  controlsLayout->setHorizontalSpacing(0);
                  controlsLayout->setVerticalSpacing(0);
                  centralWidget->setLayout(controlsLayout);

                      setCentralWidget(centralWidget);@
                  
                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Aleskey78
                    wrote on 21 Apr 2011, 11:04 last edited by
                    #9

                    Some strange things are happening here...
                    http://developer.qt.nokia.com/forums/viewthread/5366/
                    Pair programming? :)

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      vinb
                      wrote on 21 Apr 2011, 11:12 last edited by
                      #10

                      Or 1 and the same person?

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on 21 Apr 2011, 20:16 last edited by
                        #11

                        I am getting the feeling: homework assignment.

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          vinb
                          wrote on 21 Apr 2011, 20:35 last edited by
                          #12

                          Then they have to work together, much easier.

                          1 Reply Last reply
                          0

                          1/12

                          21 Apr 2011, 06:09

                          • Login

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