PDA

View Full Version : Need to have columns in QComboBox



anju123
31st March 2008, 07:57
Hi All,
I have a peculiar requirement. I need to have 2 columns in the QComboBox control.
I have a scenario where there are 2 strings coming from different places and getting concatenated and shown as 1 single string in the combobox.
The problem arises when the strings are a mixture of RTL (right to left) and LTR(left to right) language. In this case my strings distorts. for eg.
String 1 المتحدة String 2: 28 apr 2007
In combobox it appears as 28, المتحدة apr 2007
Somebody told me that I can do away with this problem by making 2 columns in the Combobox. But I have no clue as to how to achieve this.

Thanks,

jpn
31st March 2008, 14:32
// main.cpp
#include <QtGui>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

QStandardItemModel model(5, 3);
for (int i = 0; i < model.rowCount(); ++i)
{
QStandardItem* col0 = new QStandardItem(QString("foo%0,bar%0").arg(i));
QStandardItem* col1 = new QStandardItem(QString("foo%0").arg(i));
QStandardItem* col2 = new QStandardItem(QString("bar%0").arg(i));
model.setItem(i, 0, col0);
model.setItem(i, 1, col1);
model.setItem(i, 2, col2);
}

QComboBox comboBox;
comboBox.setModel(&model);

QTreeView* treeView = new QTreeView(&comboBox);
comboBox.setView(treeView);
treeView->setColumnHidden(0, true);
treeView->setSelectionBehavior(QAbstractItemView::SelectRows );
treeView->setAllColumnsShowFocus(true);
treeView->setRootIsDecorated(false);
treeView->header()->hide();

comboBox.show();
return app.exec();
}

QTing Apps
3rd April 2008, 12:52
Hi,
Tried this solution..
But still does not solve problem of string concatenation because the string being set in combobox is through the statement

QStandardItem* col0 = new QStandardItem(QString("foo%0,bar%0").arg(i));

and this means that finally string appending comes into picture.

So how do I set 2 different columns on combo box so that strings remain seperate?

jpn
3rd April 2008, 13:00
Just write a model which concatenates strings in data() for column 0 from columns 1 and 2 like you want.

QTing Apps
3rd April 2008, 13:07
Hi,
Thanks for the reply JPN- but this still involves string contactenation
and the problem lies there with RTL & LTR strings
String 1 المتحدة String 2: 28 apr 2007
If we concatenate it appears as 28, المتحدة apr 2007 -
I need to avoid string concatenation i.e i need to maintain seperate columns for the strings ..
Can I do that..

jpn
3rd April 2008, 13:15
Columns apply for the popup view only. The combobox itself is capable of showing a single string, thus concatenation.

chezifresh
6th July 2009, 22:04
I have a similar problem. I have a combo box with a tree widget inside of it with 3 columns. (all of which I want displayed)

The first column has the information I want displayed when the user selects something. The other two columns are just extra information for the user. Basically, I want to treat the entire row as one entity

So if someone clicks on an item in the view I would like the text from the first column to show up in the combo box (or text from all three columns concatenated or something).

However, if the user clicks on a tree widget item, it sets the text to whatever is in the cell they clicked on. Which I suppose is expected... though unexpected for the user since they're expecting either the entire row to show up in the combo box or at least the first column (the name column)