Hi!
I am new to Qt and hope that you can help me with.
Trying to learn the basics of Qt I found a nice tutorial. The principle of a GridLayout was shown with buttons on a calculator.
The code example was:
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++) {
btn->setFixedSize(40, 40);
grid->addWidget(btn, i, j);
pos++;
}
}
http://zetcode.com/tutorials/qt4tutorial/layoutmanagement/
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++) {
QPushButton *btn = new QPushButton(values[pos], this);
btn->setFixedSize(40, 40);
grid->addWidget(btn, i, j);
pos++;
}
}
http://zetcode.com/tutorials/qt4tutorial/layoutmanagement/
To copy to clipboard, switch view to plain text mode
Well, so far my idea to solve this would be to put the buttons into the layout one by one. I found such code in another calculator example.
http://www.java2s.com/Code/Cpp/Qt/Calculatorwithpushbuttons.htm
QPushButton *button0 = new QPushButton( tr("0") );
QPushButton *button1 = new QPushButton( tr("1") );
QPushButton *button2 = new QPushButton( tr("2") );
QPushButton *button3 = new QPushButton( tr("3") );
QPushButton *button4 = new QPushButton( tr("4") );
http://www.java2s.com/Code/Cpp/Qt/Calculatorwithpushbuttons.htm
To copy to clipboard, switch view to plain text mode
My question is: why does it work to place the buttons with a loop?
While adding the button with
grid->addWidget(btn, i, j);
grid->addWidget(btn, i, j);
To copy to clipboard, switch view to plain text mode
the reference to btn is passed to the addWidget-function. But in the next round while the reference does not change, another button is added having the same reference. It seems to me, that the button before is overwritten.
Well, if addWidget would be called-by-value things would be clearer.
I hope you get what I mean
Kind regards,
HomeR
Bookmarks