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, 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;
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++;
};
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++;
};
To copy to clipboard, switch view to plain text mode
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;
for (int i=0; i<4; i++) {
for (int j=0; j<5; j++){
knop[5*i+j]->setMaximumWidth(50);
myGrid->addWidget(knop[5*i+j], i, j);
labelIndex++;
};
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++;
};
To copy to clipboard, switch view to plain text mode
Bookmarks