Results 1 to 8 of 8

Thread: Width QTableView for QCombobox with multiple columns

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: Width QTableView for QCombobox with multiple columns

    Quote Originally Posted by haldrik View Post
    I´m joing to your thread, but I need a how to get a QComboBox with multple columns like VBasic does.
    what do you mean? do you mean that a combobox displays in a popup a table with many columns?

  2. #2
    Join Date
    May 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Width QTableView for QCombobox with multiple columns

    Hello,

    I've just had the same problem, here's my solution :

    Qt Code:
    1. QTableView* coilView = new QTableView(this); //create the tableview
    2. ui->coilComboBox->setView(coilView); //set it to the comboBox before making changes
    3. coilView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); //fine tuning of some options
    4. coilView->setSelectionMode(QAbstractItemView::SingleSelection);
    5. coilView->setSelectionBehavior(QAbstractItemView::SelectRows);
    6. coilView->setAutoScroll(false);
    7. ui->coilComboBox->setModel(model); //set the model
    8. coilView->resizeColumnsToContents(); //resise to content after setting model (needs data)
    9. coilView->resizeRowsToContents();
    10. coilView->setMinimumWidth(coilView->horizontalHeader()->length()); //to have a good width on the drop down widget
    To copy to clipboard, switch view to plain text mode 

    Hope it helps

  3. #3
    Join Date
    Jun 2011
    Location
    Groningen, Netherlands
    Posts
    1
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Width QTableView for QCombobox with multiple columns

    From Qt Assistant:
    void QComboBox::setView ( QAbstractItemView * itemView ):
    Sets the view to be used in the combobox popup to the given itemView. The combobox takes ownership of the view.
    Note: If you want to use the convenience views (like QListWidget, QTableWidget or QTreeWidget), make sure to call setModel() on the combobox with the convenience widgets model before calling this function.
    Example: (written in Python)
    Qt Code:
    1. box = QtGui.QComboBox()
    2. box.setObjectName('somename')
    3.  
    4. view = QtGui.QTableView()
    5. model = QtSql.QSqlQueryModel()
    6. model.setQuery('select something as somename, somethingother from somewhere')
    7. view.setModel(model)
    8.  
    9. #Do with the view whatever you like
    10. view.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
    11. view.setSelectionMode(QAbstractItemView.SingleSelection)
    12. view.setSelectionBehavior(QAbstractItemView.SelectRows)
    13. view.setAutoScroll(False)
    14. view.resizeColumnsToContents()
    15. view.resizeRowsToContents()
    16. #view.setSortingEnabled(True)
    17. view.verticalHeader().setVisible(False) #Rownumbers
    18. view.setMinimumWidth(view.horizontalHeader().length())
    19.  
    20. #The important part: First set the model, then the view on the box
    21. box.setModel(model)
    22. box.setView(view)
    To copy to clipboard, switch view to plain text mode 
    As with every solution, it comes with new problems. In the previous example, it is possible to set the QComboBox with any value from the QTable. Or, in other words, if you click the first value from the first row the QComboBox will show a different value as when you would click the second value from the first row.

    I'm open to suggestions as how to solve that problem.
    Last edited by bascuppen; 21st June 2011 at 12:07. Reason: updated contents

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
  •  
Qt is a trademark of The Qt Company.