QCombobox with multiple columns
Hi,
I have been searching around for a day and half now. I can't seem to find any complete examples of QComboboxes with muliple columns.
Basically all I want to display is two columns of text.
Currently I use a QCombobox. When a part number selection is made in the QCombobox, the description is found in the database and shown in a QLineEdit. This approach is a bit slow, it would be nice to combine the two.
I have considered just adding the two text to each row of the combobox. But retrieving a valid part number would depend on the quality of the data in the database.
Can anyone help me out?
Thanks in advance
Brendan
Re: QCombobox with multiple columns
Not sure if I understand your target functionality correctly, but how about this:
You format the string for each entry so that it contains both the number and the description.
Additionally you set the number as the entry's user data (see argument "userData" for addItem() and insertItem()).
When you react on user changes in a slot, you can ignore the text and retrieve the number through itemData()
Cheers,
_
Re: QCombobox with multiple columns
Thanks for your reply.
I was thinking something like the following that works:
Code:
tv->setModel( combo->model() );
tv->setRowHeight(0,100);
tv->insertColumn(0);
tv->insertColumn(1);
tv->insertRow(0);
item1->setText("column1");
tv->setItem(0, 0, item1);
item2->setText("column2");
tv->setItem(0, 1, item2);
tv->horizontalHeader()->setVisible(false);
tv->verticalHeader()->setVisible(false);
tv->resizeColumnsToContents();
combo->setView(tv);
Re: QCombobox with multiple columns
This is attempt 2 (still doesn't compile).
Can anyone point me in the right direction? I don't have a clue what im doing.
Code:
void SerialNumberProg::columnsTrial(
) {
tw->setModel( model );// DOESN'T LIKE THIS LINE...
int rowcount = tw->rowCount();
tw->setRowCount( rowcount + 1 );
tw->setItem( rowcount, 0, item );
tw->setItem( rowcount, 1, item );
m_qtapp->AllocateEcoComboBox->setView( tw );
delete( tw );
}
}
Re: QCombobox with multiple columns
If anyone is after a similar topic try searching for setView.
I got the following to work with ideas from this forum and a colleague.
I got to make a class for it now.
Code:
for (int i = 0; i < model->rowCount(); ++i) {
model->setItem(i, 0, col0);
model->setItem(i, 1, col1);
}
tableView->setModel( model );
tableView->verticalHeader()->setVisible(false);
tableView->horizontalHeader()->setVisible(false);
tableView->setColumnWidth ( 0, 60 );
tableView->setColumnWidth ( 1, 160 );
tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
tableView->setAutoScroll(false);
myComboBox->setModel( model );
myComboBox->setView( tableView );