Results 1 to 5 of 5

Thread: Create PushButtons in Loop

  1. #1
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Question Create PushButtons in Loop

    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

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Create PushButtons in Loop

    addWidget takes a pointer as an argument. Not a reference.

  3. #3
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: Create PushButtons in Loop

    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

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Create PushButtons in Loop

    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) .widget()

  5. The following user says thank you to FelixB for this useful post:

    homerun4711 (8th December 2010)

  6. #5
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: Create PushButtons in Loop

    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!

Similar Threads

  1. GLib-ERROR **: Cannot create pipe main loop wake-up
    By chenxuelian in forum Qt Programming
    Replies: 0
    Last Post: 2nd April 2010, 07:18
  2. Using ICONs as PushButtons
    By sudhakaran in forum Newbie
    Replies: 10
    Last Post: 4th February 2010, 18:34
  3. Three pushButtons in a shape of one
    By Kenji_Takahashi in forum Qt Programming
    Replies: 13
    Last Post: 7th September 2009, 19:25
  4. Pushbuttons with fixed aspect ratios?
    By WinchellChung in forum Newbie
    Replies: 3
    Last Post: 19th December 2007, 18:38
  5. Problems connecting PushButtons
    By Randulf in forum Newbie
    Replies: 3
    Last Post: 23rd August 2006, 14:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.