Creating an array of buttons



  • 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++;
            }
    
        }
    
    }
    

    }@



  • 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..."



  • 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);@


  • 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



  • 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);
    @



  • 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.



  • 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



  • 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);@


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



  • Or 1 and the same person?



  • I am getting the feeling: homework assignment.



  • Then they have to work together, much easier.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.