Results 1 to 4 of 4

Thread: creating objects in a for()

  1. #1
    Join Date
    Jan 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default creating objects in a for()

    Hi,

    I'm rather new to C++ and now I need a dynamic amount (int i) of buttons. I tried something (where I was sure that it wouldn't work, but just to tell you what I need) like this:
    Qt Code:
    1. int i; i=0;
    2. for(;i<9;i++){
    3. QLabel label(&fenster);
    4. label.setFrameStyle(QFrame::Panel | QFrame::Sunken);
    5. label.setText("first line\nsecond line");
    6. label.setAlignment(Qt::AlignTop | Qt::AlignLeft);
    7. label.setGeometry(10*i,80,120,50);
    8. }
    To copy to clipboard, switch view to plain text mode 

    I know that normally it would be my job to learn C++ properly, but perhaps you could be so kind and correct the code given above.

    Thanks.
    Last edited by phil_; 20th January 2006 at 19:16.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: creating objects in a for()

    Qt Code:
    1. for(int i=0;i<9;i++){
    2. QLabel *label = new QLabel(&fenster);
    3. label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    4. label->setText("first line\nsecond line");
    5. label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    6. label->setGeometry(10*i,80,120,50);
    7. }
    To copy to clipboard, switch view to plain text mode 

    This will more or less work, but two more potential errors are here:

    Qt Code:
    1. QLabel *label = new QLabel(&fenster);
    To copy to clipboard, switch view to plain text mode 

    By that code I guess "fenster" is a stack allocated object which can go out of scope. It should be allocated on heap (just like I corrected the labels here)

    Qt Code:
    1. label->setGeometry(10*i,80,120,50);
    To copy to clipboard, switch view to plain text mode 

    Based on this I guess you are not using layouts, while you probably should.

  3. #3
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: creating objects in a for()

    Quote Originally Posted by wysota
    This will more or less work, but two more potential errors are here:
    Qt Code:
    1. QLabel *label = new QLabel(&fenster);
    To copy to clipboard, switch view to plain text mode 
    By that code I guess "fenster" is a stack allocated object which can go out of scope. It should be allocated on heap (just like I corrected the labels here)
    Which means that your code should eventualy look like this *and* compile flawlesly:
    Qt Code:
    1. QLabel *label = new QLabel(fenster);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: creating objects in a for()

    Thank you very much, you were a great help!

Similar Threads

  1. DAO or ORM or ?. objects <-> rdbms
    By darkadept in forum Qt Programming
    Replies: 4
    Last Post: 7th October 2010, 00:55
  2. objects modelling
    By mickey in forum General Discussion
    Replies: 1
    Last Post: 12th April 2008, 08:14
  3. Ole objects in Qt(MS OFFice)
    By mchara in forum Qt Programming
    Replies: 9
    Last Post: 26th September 2007, 07:07
  4. Custom objects in a QGraphicScene
    By draand in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2007, 11:31
  5. Problem in creating thread in GUI application
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:05

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.