QTableWidget 中的 QComboBox 和 QSpinBox 适当对齐

2022-01-02 00:00:00 qt qtablewidget spinner c++ combobox

如何创建一个 QTable 小部件,它有 2 列,第一列有一个 QComboBox,第二列有一个 QSpinBox,这样组合框就获得了表格的所有空间,只有很小的地方留给QSpinBox(适用于 2-3 位数字).

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

推荐答案

首先使用 setCellWidget()QComboBoxQSpinBox 设置为要在相应单元格中显示的小部件.

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

其次,使用horizo??ntalHeader()访问QHeaderViewQTableView,然后设置 ResizeMode 相应.

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

相关文章