PDA

View Full Version : Width QTableView for QCombobox with multiple columns



visor_ua
7th January 2008, 19:01
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?

visor_ua
7th January 2008, 19:07
Solved:
setMinimumWidth for QTableView

Thanks

visor_ua
7th January 2008, 19:37
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:confused:

jpn
7th January 2008, 19:57
Try QHeaderView (accessible via QTableView::horizontalHeader()).

haldrik
24th August 2008, 00:29
I´m joing to your thread, but I need a how to get a QComboBox with multple columns like VBasic does.

spirit
24th August 2008, 06:54
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?

ness
2nd December 2009, 10:47
Hello,

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


QTableView* coilView = new QTableView(this); //create the tableview
ui->coilComboBox->setView(coilView); //set it to the comboBox before making changes
coilView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded ); //fine tuning of some options
coilView->setSelectionMode(QAbstractItemView::SingleSelectio n);
coilView->setSelectionBehavior(QAbstractItemView::SelectRows );
coilView->setAutoScroll(false);
ui->coilComboBox->setModel(model); //set the model
coilView->resizeColumnsToContents(); //resise to content after setting model (needs data)
coilView->resizeRowsToContents();
coilView->setMinimumWidth(coilView->horizontalHeader()->length()); //to have a good width on the drop down widget


Hope it helps :)

bascuppen
21st June 2011, 10:54
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)


box = QtGui.QComboBox()
box.setObjectName('somename')

view = QtGui.QTableView()
model = QtSql.QSqlQueryModel()
model.setQuery('select something as somename, somethingother from somewhere')
view.setModel(model)

#Do with the view whatever you like
view.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNe eded)
view.setSelectionMode(QAbstractItemView.SingleSele ction)
view.setSelectionBehavior(QAbstractItemView.Select Rows)
view.setAutoScroll(False)
view.resizeColumnsToContents()
view.resizeRowsToContents()
#view.setSortingEnabled(True)
view.verticalHeader().setVisible(False) #Rownumbers
view.setMinimumWidth(view.horizontalHeader().lengt h())

#The important part: First set the model, then the view on the box
box.setModel(model)
box.setView(view)

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.