PDA

View Full Version : how to add column in QComboBox??



anupamgee
11th September 2009, 08:19
hey,
I want to create a combobox havin 2 columns.Both the columns have different strings to show.
how to do it??
Plzz rep

wysota
11th September 2009, 09:35
Use QComboBox::setView() to set a different view (a tree probably) for the combobox.

anupamgee
11th September 2009, 09:41
Thnxx for reply,
can you plzz explain this a little more with the help of an example/code...
waiting for ur rep..

wysota
11th September 2009, 09:54
QComboBox *cb = new QComboBox;
QAbstractItemModel *model = new SomeModel;
cb->setModel(model);
QTreeView *tv = new QTreeView;
tv->setModel(model); // I don't know if this is required
tv->header()->hide();
cb->setView(tv);

anupamgee
24th September 2009, 13:39
hi,
i hv written dis code but it is not showing the popup containing items.what is wrong in dis code??



QComboBox *countryCombo=new QComboBox(this);
countryCombo->setGeometry(42,25,205,25);
countryCombo->setEditable(true);
QStandardItemModel model(5, 3);
for (int i = 0; i < model.rowCount(); ++i)
{
QStandardItem* col0 = new QStandardItem(QString("hh").arg(i));
QStandardItem* col1 = new QStandardItem(QString("ee").arg(i));
QStandardItem* col2 = new QStandardItem(QString("r").arg(i));
model.setItem(i, 0, col0);
model.setItem(i, 1, col1);
model.setItem(i, 2, col2);
}
countryCombo->setModel(&model);
QTreeView* treeView = new QTreeView(countryCombo);
countryCombo->setView(treeView);
treeView->setColumnHidden(0, true);
treeView->setSelectionBehavior(QAbstractItemView::SelectRows );
treeView->setAllColumnsShowFocus(true);
treeView->setRootIsDecorated(false);
treeView->header()->hide();

wysota
24th September 2009, 16:44
Your model goes out of scope before it has a chance to do anything.

anupamgee
25th September 2009, 13:13
hi,
thanx for reply
But I didnt got your point .pls explain with necessary change in the code .

wysota
25th September 2009, 16:10
Allocate the model on heap and not on the stack.