Results 1 to 6 of 6

Thread: How can I create button from arraylist.

  1. #1
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How can I create button from arraylist.

    Hi all,

    I have an arraylist. This arraylist hold QPushButton's name.

    Qt Code:
    1. List<QString> buttunNameList = new List<QString>;
    2. buttonNameList<<"buttonname1"<<"buttonname2"<<"buttonname3";
    To copy to clipboard, switch view to plain text mode 

    I create button manuel.

    Qt Code:
    1. QPushButton *buttonname1 = new QPushButton();
    2. QPushButton *buttonname2 = new QPushButton();
    3. QPushButton *buttonname3 = new QPushButton();
    To copy to clipboard, switch view to plain text mode 

    But I want to create button from arraylist. How can I create button from arraylist?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I create button from arraylist.

    Qt Code:
    1. QStringList buttonsNames;
    2. buttonsNames << "button1" << "button2" << "button3";
    3. foreach (const QString &name, buttonsNames) {
    4. QPushButton *pb = new QPushButton(name, this);
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    electronicboy (5th October 2009)

  4. #3
    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 can I create button from arraylist.

    ... and if you need to access the pointers later on, use a QList<QPushButton*>.
    Qt Code:
    1. QList<QPushButton*> buttons;
    2. // loop
    3. buttons.append(pb);
    4. // end loop
    5. buttons.at(0)->... // == buttonname1
    6. buttons.at(1)->... // == buttonname2
    7. //...
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How can I create button from arraylist.

    @Spirit thanks for help. It's right.

    @Lykurg sory. I don't understand ur algorithm. Can u explain ur algoritm?

    Performans is very important for me. What do u recommended for high performans?

  6. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I create button from arraylist.

    he means
    Qt Code:
    1. QList<QPushButton *> buttons;//global variable or class-member
    2. ...
    3. QStringList buttonsNames;
    4. buttonsNames << "button1" << "button2" << "button3";
    5. foreach (const QString &name, buttonsNames) {
    6. QPushButton *pb = new QPushButton(name, this);
    7. buttons << pb;
    8. ...
    9. }
    10. ...
    11. for (int i = 0; i < buttons.count(); ++i) {
    12. QPushButton *pb = buttons.at(i);
    13. pb->setText(QString("new text %1").arg(i));
    14. }
    15. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #6
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How can I create button from arraylist.

    Hımm. Thanks. I try this code;

    Qt Code:
    1. buttonsNames << "button1" << "button2" << "button3"<<"button4"<<"button5";
    2. foreach (const QString &name, buttonsNames) {
    3. QPushButton *pb = new QPushButton(name,this);
    4. pb->setObjectName(name);
    5. connect(pb,SIGNAL(clicked()),this,SLOT(clickedButton()));
    6. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 13th January 2009, 15:32
  2. button background color when it is clicked
    By navi1084 in forum Qt Programming
    Replies: 4
    Last Post: 9th December 2008, 15:02
  3. How to create a custom Button?
    By Mister_Crac in forum Qt Programming
    Replies: 18
    Last Post: 22nd October 2006, 23:59
  4. Create a new button
    By avis_phoenix in forum Qt Programming
    Replies: 7
    Last Post: 3rd September 2006, 18:56
  5. How to create an Insert/Submit button for a form.
    By fnmblot in forum Qt Programming
    Replies: 5
    Last Post: 4th August 2006, 16:18

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.