Results 1 to 5 of 5

Thread: How To Create Array of Widgets in Qt4?

  1. #1

    Default How To Create Array of Widgets in Qt4?

    Hi
    How I Can Create Array of Widgets (For Example: QLineEdit) on Qt4?

    Please Help Me!
    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How To Create Array of Widgets in Qt4?

    Maybe
    Qt Code:
    1. QList<QLineEdit*> myList;
    2. mylist.append(new QLineEdit());
    To copy to clipboard, switch view to plain text mode 

    depends on what you want to do.

    Lykurg

  3. #3
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How To Create Array of Widgets in Qt4?

    Is this bad? I want to create an array of widgets to put on a QStackedWidget. Can I do this:

    Qt Code:
    1. // ...in myfile.cpp...
    2.  
    3. QPushButton *pb[2];
    4.  
    5. for(int a=0; a<2; a++)
    6. {
    7. pb[a] = new QPushButton;
    8. myStack->addWidget(pb[a]);
    9. }
    To copy to clipboard, switch view to plain text mode 

    It compiles and seems to work. Previously when I've needed an array of Widgets, I would declare

    QPushButton *pb[2];


    in the header file. But is there any reason I cannot declare it in the .cpp file like above?

  4. #4
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How To Create Array of Widgets in Qt4?

    follow-up: if I need to replace the "2" with "getNumber()" like so:

    Qt Code:
    1. // ...in myfile.cpp...
    2.  
    3. // QPushButton *pb[2];
    4.  
    5. QPushButton *pb[ getNumber() ];
    6.  
    7. // or
    8. // const int tempNum = getNumber();
    9. // QPushButton *pb[tempNum];
    10.  
    11. for(int a=0; a<2; a++)
    12. {
    13. pb[a] = new QPushButton;
    14. myStack->addWidget(pb[a]);
    15. }
    To copy to clipboard, switch view to plain text mode 

    I get an error: expected constant expression. Is there a way around this?

  5. #5
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: How To Create Array of Widgets in Qt4?

    Quote Originally Posted by vonCZ View Post
    follow-up: if I need to replace the "2" with "getNumber()" like so:

    Qt Code:
    1. // ...in myfile.cpp...
    2.  
    3. // QPushButton *pb[2];
    4.  
    5. QPushButton *pb[ getNumber() ];
    6.  
    7. // or
    8. // const int tempNum = getNumber();
    9. // QPushButton *pb[tempNum];
    10.  
    11. for(int a=0; a<2; a++)
    12. {
    13. pb[a] = new QPushButton;
    14. myStack->addWidget(pb[a]);
    15. }
    To copy to clipboard, switch view to plain text mode 
    I get an error: expected constant expression. Is there a way around this?
    You will have to wait for constexpr in C++0x to be able to do it.
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  6. The following user says thank you to ComaWhite for this useful post:

    vonCZ (27th April 2009)

Similar Threads

  1. Using a 2d data array to create an image
    By dbrmik in forum Newbie
    Replies: 20
    Last Post: 28th October 2008, 17:22
  2. How to create Custom Slot upon widgets
    By ashukla in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2007, 14:07
  3. How to create a movable border between two widgets?
    By Teuniz in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2007, 08:45
  4. create custom widgets
    By nimmyj in forum General Discussion
    Replies: 1
    Last Post: 20th November 2006, 08:24
  5. How to create QPixmap from unsigned character array?
    By rashidbutt in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 18:25

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.