Results 1 to 8 of 8

Thread: Width QTableView for QCombobox with multiple columns

  1. #1
    Join Date
    Jun 2006
    Posts
    23
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Width QTableView for QCombobox with multiple columns

    Setting setModel, and setView(QTableView*) for QComboBox;
    All is ok, but cant to do QTableView wider then combobox width;
    Width is the same as combobox:-(
    How to make QTableView wodget wider?

  2. #2
    Join Date
    Jun 2006
    Posts
    23
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Width QTableView for QCombobox with multiple columns

    Solved:
    setMinimumWidth for QTableView

    Thanks

  3. #3
    Join Date
    Jun 2006
    Posts
    23
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Width QTableView for QCombobox with multiple columns

    But have other problem, cant to change in this QTableView - column width.
    All columns have the same width, and it cant be changed by:
    resizeColumnsToContents, setColumnWidth. Always the same

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Width QTableView for QCombobox with multiple columns

    J-P Nurmi

  5. #5
    Join Date
    Aug 2007
    Location
    Fresno - Colombia
    Posts
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Width QTableView for QCombobox with multiple columns

    I´m joing to your thread, but I need a how to get a QComboBox with multple columns like VBasic does.

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

    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?

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

  8. #8
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.