Results 1 to 9 of 9

Thread: QcomboBox delimiter

  1. #1
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default QcomboBox delimiter

    Guys,

    I am trying to insert two items in comboBox.

    I have QString like this QString str=" Item1, Item2";

    Qt Code:
    1. QString CItemclass::GetStringFile ()
    2. {
    3. QString str;
    4. ......
    5. str=" Item1, Item2";
    6.  
    7. return str;
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    and then I am trying to insert to combobox

    Qt Code:
    1. pComboBox->insertItems (0, GetStringFile().split(","));
    To copy to clipboard, switch view to plain text mode 

    or
    Qt Code:
    1. pComboBox->insertStringList(GetStringFile().split(","));
    To copy to clipboard, switch view to plain text mode 
    However I am able to insert items as list in the comboBox but it appear as follows in drop menu;-

    Item1, Item2
    Item2

    I want to display as separate items but however the first item in the drop down menu is not getting seperated..even though i have used split(,) while inserting items at comboBox..
    It should show as follows..

    Item1
    Item2

    any clue or idea will be helpful.


    thanks.
    Last edited by wysota; 29th November 2007 at 14:29. Reason: Changed [quote] to [code]

  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: QcomboBox delimiter

    Do you have any items in the combobox before issuing the above code?

  3. #3
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: QcomboBox delimiter

    No, the comBoBox is empty. I am loading comboBox from UI form and I have checked the property of combobox.. current index is -1. I have one another Item3 in the comBoBox dropdown menu but that I am calling after running the code above..so basically after running the whole code it shows as follows

    Item1,Item2
    Item2
    Item3
    but there is nothing in the comBoBox before executing above code.

  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: QcomboBox delimiter

    Qt Code:
    1. $ cat main.cpp
    2. #include <QComboBox>
    3. #include <QApplication>
    4.  
    5. int main(int argc, char **argv){
    6. QApplication app(argc, argv);
    7. QComboBox box;
    8. box.insertItems(0, QString("Item1, Item2").split(", "));
    9. box.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    This works quite fine for me. Double check your code please. Call pComboBox->clear() before adding items to it, just in case. Also add qDebug() << GetStringFile() to your code to make sure the string is what you expect it to be. Adding qDebug() << GetStringFile().split(", "); might be a good idea as well. Just make sure you call the function once and then operate on the result, like this:
    Qt Code:
    1. QString str = GetStringFile();
    2. qDebug() << str;
    3. QStringList slist = str.split(", ");
    4. qDebug() << slist;
    5. pComboBox->clear();
    6. pComboBox->insertItems(0, slist);
    To copy to clipboard, switch view to plain text mode 

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

    user_mail07 (29th November 2007)

  6. #5
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: QcomboBox delimiter

    Thanks a lot Wysota !! Acutally I found a mistake in my program since i was calling insertItems() twice in my program and in the second call I was not calling str.split( ) function so it was being overwritten by calling insertItems( 0,str) insead of nsertItems( 0,str.split( ",")).

  7. #6
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: QcomboBox delimiter

    I have another question related to my last post . Is it possible to assign list of separate strings to QString ?

    Code Snippet:

    Qt Code:
    1. QString CItemclass::GetStringFile ()
    2.  
    3. {
    4. QString str1 = "Item1"
    5. QString str2 = "Item2"
    6.  
    7. QString mainStr;
    8.  
    9. mainStr = ( str1, str2); //without assigning in quotes
    10. return mainStr;
    11. }
    12.  
    13. Output : Item1
    14. instead of
    15. Output: Item1
    16. Item2
    To copy to clipboard, switch view to plain text mode 


    The problem with this code is - it add only one string that is str1(Item1) the comboBox drop down menu. The problem appears if I want to read str1 from different resource file. The only way I can do this is declaring both strings ("Item1, Item2") here (hard coded). Is it possible to that we can return two different strings to the QString ?
    Last edited by user_mail07; 1st December 2007 at 20:54. Reason: missing [code] tags

  8. #7
    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: QcomboBox delimiter

    I'm not sure I understand... You want to join two strings into one, correct?
    Qt Code:
    1. QString s1 = "xxx";
    2. QString s2 = "yyy";
    3. QString result = s1 + ", " + s2;
    4. // or
    5. result = QString("%1, %2").arg(s1).arg(s2);
    6. // or
    7. slist << s1 << s2;
    8. result = slist.join(", ");
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to wysota for this useful post:

    user_mail07 (2nd December 2007)

  10. #8
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: QcomboBox delimiter

    Thanks that solved my problem. I did not know how to return result of two strings from separate resources without doing hard code.

    Following line worked for me.
    QString result = s1 + ", " + s2;


    I really appreciate for your help.

  11. #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: QcomboBox delimiter

    All of the three above work. The first solution is the most crude and prone to problems. The nicest is the last one.

Similar Threads

  1. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 19:25
  2. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 23:29
  3. Style Sheet color of QComboBox
    By tpf80 in forum Qt Programming
    Replies: 6
    Last Post: 25th June 2007, 02:55
  4. using QComboBox as an ItemView
    By EricTheFruitbat in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2007, 16:14
  5. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 10:50

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.