PDA

View Full Version : getting to the buttons inside a QGridLayout



mr_kazoodle
19th February 2011, 15:28
Hi,

Currently, I'm making a little calculator program to train my coding skills. I use the QGridLayout because it helps me creating the layout fast.
My problem is: I don't know how to connect the buttons in the grid because I don't know how to 'access' them.

I started of with the code from this tutorial (http://zetcode.com/tutorials/qt4tutorial/layoutmanagement/), look for QGridLayout.

Added after 4 minutes:

EDIT:
(The solution I am trying now is to first make an array of buttons and then use those buttons in the grid)
This code compiles but doesn't run. If I can find the mistake in this piece of code I think I don't have any problems anymore.

char *knopLabels[20] = { "7", "8", "9", "/", "sqrt",
"4", "5", "6", "*", "x^y",
"1", "2", "3", "-", "n!",
"0", ",", "+/-", "+", "="};
int labelIndex = 0;
QPushButton *knop [20];
for (int i=0; i<4; i++) {
for (int j=0; j<5; j++){
knop[5*i+j]->setText(knopLabels[labelIndex]);
knop[5*i+j]->setMaximumWidth(50);
myGrid->addWidget(knop[5*i+j], i, j);
labelIndex++;
};

Added after 4 minutes:

fixed it:


char *knopLabels[20] = { "7", "8", "9", "/", "sqrt",
"4", "5", "6", "*", "x^y",
"1", "2", "3", "-", "n!",
"0", ",", "+/-", "+", "="};
int labelIndex = 0;
QPushButton *knop[20];
for (int i=0; i<4; i++) {
for (int j=0; j<5; j++){
knop[5*i+j] = new QPushButton(knopLabels[labelIndex]);
knop[5*i+j]->setMaximumWidth(50);
myGrid->addWidget(knop[5*i+j], i, j);
labelIndex++;
};

Lesiok
19th February 2011, 15:33
Read about QObject::findChild method.

aamer4yu
19th February 2011, 16:48
Obviosuly you werent creating the button objects itself !!