QComboBox and QSpinBox in QTableWidget with approp

2019-02-27 15:09发布

问题:

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

回答1:

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 );