PDA

View Full Version : Creating an Array of Button



Anshuman
21st April 2011, 08:06
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++;
}

}

}

}

nish
21st April 2011, 08:09
just use a gridlayout for this.

Anshuman
21st April 2011, 08:14
I hv try it using grid layout also..

squidge
21st April 2011, 08:36
then your doing it wrong.

Alternatively, you could a QVBoxLayout and a QHBoxLayout for each line.

Anshuman
21st April 2011, 09:21
hello squidge

yeh u r right..its working now by using grid layout..now hw can i decrease the space between each rows..moreever hw can i add textBrowser below..after all buttons are arranged in rows and colums..

my code is:u plz check it..

QWidget *centralWidget = new QWidget;
selectedDate=QDate::currentDate();
int count=1,i,j;

QPushButton *button[10][10];
QGridLayout *controlsLayout = new QGridLayout;
QTextBrowser *T=new QTextBrowser() //how can i add this text browser below after all the array of buttons are set
for(i=0;i<7;i++)
{

for(j=0;j<7;j++)
{


if(count<=42)
{

button[i][j] = new QPushButton(QString::number(count));

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



controlsLayout->setSpacing(0);

centralWidget->setLayout(controlsLayout);

setCentralWidget(centralWidget);

count++;
}

}


}




with regards
Anshuman

FelixB
21st April 2011, 09:45
I suggest to read some tutorials about layouts...

Anshuman
21st April 2011, 10:54
hello nish

yeh by using gridlayout its workin 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()

squidge
21st April 2011, 14:17
The easiest way would be to do the same effect in designer and edit the properties until you get your desired effect and then check the generated output. The designer properties should also give you a clue as to what needs changing.

Or you could just read the documentation of QWidget which QPushButton inherits.

nish
21st April 2011, 14:32
use setContentMargins() for spacing.