Results 1 to 9 of 9

Thread: add multiple QCoombox using loop

  1. #1
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default add multiple QCoombox using loop

    Hello,

    How can one add multiple QComboBoxes using a for-loop?

    Like we do in qcheckbox as:
    Qt Code:
    1. for (int k=0; k<3 ; k++)
    2. {
    3. mCheckBox = new QCheckBox(tr("input %2:").arg(k));
    4. }
    To copy to clipboard, switch view to plain text mode 

    Is there a similiar way to add multiple QComboBoxes aswell?

    PS: i could not find the .arg() object in QComboBox :-(

    Thanks!

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

    Default Re: add multiple QCoombox using loop

    arg() is a method of QString.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: add multiple QCoombox using loop

    Okay, then what is the way of doing it for QComboBox?

    I have a combobox next to checkbox. For checkbox I used the code as mentioned above.
    Now I want to repeat this combination using a for-loop.

    How can this be achieved?
    Last edited by uz_qt; 26th September 2013 at 11:45.

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

    Default Re: add multiple QCoombox using loop

    Qt Code:
    1. for(int i=0;i<n;++i) {
    2. QComboBox *cb = new QComboBox;
    3. // ...
    4. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: add multiple QCoombox using loop

    @wysota
    I have already tried this. But the comboBoxes are not repeated. Only the checkboxes are repeated. Following is my code:

    Qt Code:
    1. QGridLayout* ChannelLayout1 = new QGridLayout(this);
    2. QGroupBox *GroupBox1 = new QGroupBox(tr("Box"));
    3.  
    4. for(k=0; k < 5 ; k++)
    5. {
    6. QCheckBox *mCheckBox1 = new QCheckBox(tr("input %2:").arg(k));
    7. QComboBox *mComboBox1 = new QComboBox;
    8.  
    9. for(int i=0; i < 3; i++)
    10. {
    11. mComboBox1->addItem(tr("Item %2").arg(i));
    12. }
    13.  
    14. mComboBox1->setFixedWidth(200);
    15. ChannelLayout1->addWidget(mCheckBox1);
    16. ChannelLayout1->addWidget(mComboBox1,0,1,0,20);
    17. }
    18. GroupBox1->setLayout(ChannelLayout1);
    To copy to clipboard, switch view to plain text mode 

    Please help.

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

    Default Re: add multiple QCoombox using loop

    You are putting all comboboxes in one place in the layout.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: add multiple QCoombox using loop

    @wysota

    Ya, i figured that out. There was one more problem besides that in lines 15 and 16. The corrected lines are as follows:

    Qt Code:
    1. 15. ChannelLayout1->addWidget(mCheckBox1,k,0);
    2. 16. ChannelLayout1->addWidget(mComboBox1,k,1);
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  8. #8
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Smile

    @wysota:

    Thanks for ur advice. I have sorted it out! :-)
    Last edited by uz_qt; 27th September 2013 at 11:45.

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

    Default Re: add multiple QCoombox using loop

    You are trying to set the same layout to two widgets.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. For loop and While loop handling.
    By kiboi in forum Newbie
    Replies: 7
    Last Post: 2nd January 2013, 13:43
  2. Replies: 2
    Last Post: 19th October 2011, 09:30
  3. Replies: 4
    Last Post: 6th August 2011, 01:40
  4. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  5. Replies: 0
    Last Post: 21st December 2006, 11:48

Tags for this Thread

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.