Question

How to create a QTable widget which has 2 columnes, and in first column there is a QComboBox and in the second column there is a QSpinBox so that the combo box gets all the space of table and only a very small place leaves for QSpinBox (for 2-3 digits).

Was it helpful?

Solution

First, use setCellWidget() to set the QComboBox and QSpinBox as the widgets to be displayed in the appropriate cell.

Second, use horizontalHeader() to access the QHeaderView for the QTableView, then set the ResizeMode accordingly.

QTableWidget* table = new QTableWidget( this );
table->setColumnCount( 2 );
table->setRowCount( 1 );
table->setCellWidget ( 0, 0, new QComboBox( table ) );
table->setCellWidget ( 0, 1, new QSpinBox( table ) );
table->horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
table->horizontalHeader()->setResizeMode( 1, QHeaderView::ResizeToContents );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top