PDA

View Full Version : QcomboBox delimiter



user_mail07
29th November 2007, 14:30
Guys,

I am trying to insert two items in comboBox.

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


QString CItemclass::GetStringFile ()
{
QString str;
......
str=" Item1, Item2";

return str;

}

and then I am trying to insert to combobox


pComboBox->insertItems (0, GetStringFile().split(","));

or

pComboBox->insertStringList(GetStringFile().split(","));
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.

wysota
29th November 2007, 14:36
Do you have any items in the combobox before issuing the above code?

user_mail07
29th November 2007, 14:41
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.

wysota
29th November 2007, 15:28
$ cat main.cpp
#include <QComboBox>
#include <QApplication>

int main(int argc, char **argv){
QApplication app(argc, argv);
QComboBox box;
box.insertItems(0, QString("Item1, Item2").split(", "));
box.show();
return app.exec();
}
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:

QString str = GetStringFile();
qDebug() << str;
QStringList slist = str.split(", ");
qDebug() << slist;
pComboBox->clear();
pComboBox->insertItems(0, slist);

user_mail07
29th November 2007, 20:30
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( ",")).

user_mail07
1st December 2007, 21:53
I have another question related to my last post . Is it possible to assign list of separate strings to QString ?

Code Snippet:


QString CItemclass::GetStringFile ()

{
QString str1 = "Item1"
QString str2 = "Item2"

QString mainStr;

mainStr = ( str1, str2); //without assigning in quotes
return mainStr;
}

Output : Item1
instead of
Output: Item1
Item2


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 ?

wysota
1st December 2007, 22:58
I'm not sure I understand... You want to join two strings into one, correct?

QString s1 = "xxx";
QString s2 = "yyy";
QString result = s1 + ", " + s2;
// or
result = QString("%1, %2").arg(s1).arg(s2);
// or
QStringList slist;
slist << s1 << s2;
result = slist.join(", ");

user_mail07
2nd December 2007, 01:12
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.

wysota
2nd December 2007, 11:20
All of the three above work. The first solution is the most crude and prone to problems. The nicest is the last one.