PDA

View Full Version : Create PushButtons in Loop



homerun4711
8th December 2010, 00:18
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++) {
QPushButton *btn = new QPushButton(values[pos], this);
btn->setFixedSize(40, 40);
grid->addWidget(btn, i, j);
pos++;
}
}
http://zetcode.com/tutorials/qt4tutorial/layoutmanagement/


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.



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


My question is: why does it work to place the buttons with a loop?
While adding the button with


grid->addWidget(btn, i, j);

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

SixDegrees
8th December 2010, 00:25
addWidget takes a pointer as an argument. Not a reference.

homerun4711
8th December 2010, 07:23
Hi!
Thanks for your quick response. You are right, I mixed that up.

But I still do not understand how this works...

Two questions that might lead to what I mean:
Is the PushButton-widget created and then forgotton when it's overwritten?
What if I need to change one single widged later?
Can it be accessed indendently?

Kind regards,
HomeR

FelixB
8th December 2010, 08:06
you need to learn some basic c++ things. this is about pointer. "new" creates a new object (let's call it "newObject") on the heap. "btn" has the adress(!) of this object, not the object itself. The pointer is passed to "addWidget" by value. This is quite tricky if you're not familiar with pointers. There is a copy - but not of the actual object, only of the pointer to this object. Inside "addWidget" exists a second pointer to "newObject". If you reassign the pointer "btn", nothing happens to "newObject". Since the adress of this object has been copied, Qt can use the object. Btw, Qt does memory deallocation for QWidgets, that were added to other QWidgets. So you don't have to care about deleting these objects.

If you want to access WIdgets in a Gridlayout, use itemAtPosition(row,col) (http://doc.qt.nokia.com/4.6/qgridlayout.html#itemAtPosition) .widget() (http://doc.qt.nokia.com/4.6/qlayoutitem.html#widget)

homerun4711
8th December 2010, 11:39
You are right, having used Java Swing before, I am not so familiar with pointers by now. :)
I did not realize that in fact a completly new object is created by the keyword "new" while btn is a pointer to this new object.
Thanks, that solved my question!