PDA

View Full Version : QCombobox with multiple columns



grantbj74
21st February 2014, 05:05
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

anda_skoa
21st February 2014, 09:19
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,
_

grantbj74
25th February 2014, 06:10
Thanks for your reply.

I was thinking something like the following that works:



QTableView *tv = new QTableView(this);

tv->setModel( combo->model() );
tv->setRowHeight(0,100);
tv->insertColumn(0);
tv->insertColumn(1);
tv->insertRow(0);

QTableWidgetItem* item1 = new QTableWidgetItem;
item1->setText("column1");
tv->setItem(0, 0, item1);

QTableWidgetItem* item2 = new QTableWidgetItem;
item2->setText("column2");
tv->setItem(0, 1, item2);

tv->horizontalHeader()->setVisible(false);
tv->verticalHeader()->setVisible(false);
tv->resizeColumnsToContents();
combo->setView(tv);

grantbj74
4th March 2014, 23:58
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.



void SerialNumberProg::columnsTrial(
) {
QTableWidget *tw;

if( ( tw = new QTableWidget( 0, 2, m_qtapp )) != NULL ) {

QTableWidgetItem *item;
QAbstractItemModel *model = m_qtapp->AllocateEcoComboBox->model();

tw->setModel( model );// DOESN'T LIKE THIS LINE...

int rowcount = tw->rowCount();
tw->setRowCount( rowcount + 1 );

item = new QTableWidgetItem( "id" );
tw->setItem( rowcount, 0, item );

item = new QTableWidgetItem( "date" );
tw->setItem( rowcount, 1, item );

m_qtapp->AllocateEcoComboBox->setView( tw );
delete( tw );
}
}

grantbj74
5th March 2014, 06:13
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.



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

QTableView* tableView = new QTableView( this );
tableView->setModel( model );
tableView->verticalHeader()->setVisible(false);
tableView->horizontalHeader()->setVisible(false);
tableView->setColumnWidth ( 0, 60 );
tableView->setColumnWidth ( 1, 160 );
tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded );
tableView->setSelectionBehavior(QAbstractItemView::SelectRows );
tableView->setAutoScroll(false);

myComboBox->setModel( model );
myComboBox->setView( tableView );