Results 1 to 5 of 5

Thread: QCombobox with multiple columns

  1. #1
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QCombobox with multiple columns

    Thanks for your reply.

    I was thinking something like the following that works:

    Qt Code:
    1. QTableView *tv = new QTableView(this);
    2.  
    3. tv->setModel( combo->model() );
    4. tv->setRowHeight(0,100);
    5. tv->insertColumn(0);
    6. tv->insertColumn(1);
    7. tv->insertRow(0);
    8.  
    9. item1->setText("column1");
    10. tv->setItem(0, 0, item1);
    11.  
    12. item2->setText("column2");
    13. tv->setItem(0, 1, item2);
    14.  
    15. tv->horizontalHeader()->setVisible(false);
    16. tv->verticalHeader()->setVisible(false);
    17. tv->resizeColumnsToContents();
    18. combo->setView(tv);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

    Qt Code:
    1. void SerialNumberProg::columnsTrial(
    2. ) {
    3.  
    4. if( ( tw = new QTableWidget( 0, 2, m_qtapp )) != NULL ) {
    5.  
    6. QAbstractItemModel *model = m_qtapp->AllocateEcoComboBox->model();
    7.  
    8. tw->setModel( model );// DOESN'T LIKE THIS LINE...
    9.  
    10. int rowcount = tw->rowCount();
    11. tw->setRowCount( rowcount + 1 );
    12.  
    13. item = new QTableWidgetItem( "id" );
    14. tw->setItem( rowcount, 0, item );
    15.  
    16. item = new QTableWidgetItem( "date" );
    17. tw->setItem( rowcount, 1, item );
    18.  
    19. m_qtapp->AllocateEcoComboBox->setView( tw );
    20. delete( tw );
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

    Qt Code:
    1. QStandardItemModel *model = new QStandardItemModel( 3, 2, this );
    2. for (int i = 0; i < model->rowCount(); ++i) {
    3. QStandardItem* col0 = new QStandardItem( QString("foo%0").arg(i) );
    4. QStandardItem* col1 = new QStandardItem( QString("bar%0").arg(i) );
    5. model->setItem(i, 0, col0);
    6. model->setItem(i, 1, col1);
    7. }
    8.  
    9. QTableView* tableView = new QTableView( this );
    10. tableView->setModel( model );
    11. tableView->verticalHeader()->setVisible(false);
    12. tableView->horizontalHeader()->setVisible(false);
    13. tableView->setColumnWidth ( 0, 60 );
    14. tableView->setColumnWidth ( 1, 160 );
    15. tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    16. tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    17. tableView->setAutoScroll(false);
    18.  
    19. myComboBox->setModel( model );
    20. myComboBox->setView( tableView );
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 6th December 2012, 19:56
  2. Width QTableView for QCombobox with multiple columns
    By visor_ua in forum Qt Programming
    Replies: 7
    Last Post: 21st June 2011, 10:54
  3. ComboBox with multiple columns
    By haldrik in forum Qt Programming
    Replies: 7
    Last Post: 11th July 2009, 10:15
  4. Need to have columns in QComboBox
    By anju123 in forum Qt Programming
    Replies: 6
    Last Post: 6th July 2009, 22:04
  5. QCompleter on multiple columns
    By akiross in forum Qt Programming
    Replies: 0
    Last Post: 9th November 2008, 16:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.