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:

Qt Code:
  1. for (int i=0; i<4; i++) {
  2. for (int j=0; j<4; j++) {
  3. QPushButton *btn = new QPushButton(values[pos], this);
  4. btn->setFixedSize(40, 40);
  5. grid->addWidget(btn, i, j);
  6. pos++;
  7. }
  8. }
  9. 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.

Qt Code:
  1. QPushButton *button0 = new QPushButton( tr("0") );
  2. QPushButton *button1 = new QPushButton( tr("1") );
  3. QPushButton *button2 = new QPushButton( tr("2") );
  4. QPushButton *button3 = new QPushButton( tr("3") );
  5. QPushButton *button4 = new QPushButton( tr("4") );
  6. 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

Qt Code:
  1. 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