QtableWidget表格中添加控件怎么居中
1个回答
展开全部
tableWidget->setStyleSheet("QTableWidget::item{border:1px solid ;}");
//
//表格表头的显示与隐藏
tableWidget->verticalHeader()->setVisible(false); //隐藏列表头
tableWidget->horizontalHeader()->setVisible(false); //隐藏行表头
//tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
//设置行高
for(int i= 0; i< tableWidget->rowCount(); i++)
{
tableWidget->setRowHeight(i, 60);
}
//设置列宽
for(int i= 0; i< tableWidget->columnCount(); i++)
{
tableWidget->setColumnWidth(i, 185);
}
// 单元格中添加控件并居中
QLabel* label = new QLabel("gender");
QComboBox *comBox = new QComboBox();
comBox->setFixedSize(100, 25);
comBox->addItem("F");
comBox->addItem("M");
// 单元格中的控件需要通过布局管理
QWidget *widget = new QWidget;
QHBoxLayout *hLayout;
hLayout = new QHBoxLayout();
hLayout->addWidget( label);
hLayout->addWidget(comBox);
hLayout->setMargin(0);
hLayout->setAlignment(widget, Qt::AlignCenter);
hLayout->setContentsMargins(10, 0, 20, 0);
widget->setLayout(hLayout);
// 添加单元格
tableWidget->setCellWidget(0,0,widget);
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addWidget( tableWidget);
setLayout(mainLayout);
//
//表格表头的显示与隐藏
tableWidget->verticalHeader()->setVisible(false); //隐藏列表头
tableWidget->horizontalHeader()->setVisible(false); //隐藏行表头
//tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
//设置行高
for(int i= 0; i< tableWidget->rowCount(); i++)
{
tableWidget->setRowHeight(i, 60);
}
//设置列宽
for(int i= 0; i< tableWidget->columnCount(); i++)
{
tableWidget->setColumnWidth(i, 185);
}
// 单元格中添加控件并居中
QLabel* label = new QLabel("gender");
QComboBox *comBox = new QComboBox();
comBox->setFixedSize(100, 25);
comBox->addItem("F");
comBox->addItem("M");
// 单元格中的控件需要通过布局管理
QWidget *widget = new QWidget;
QHBoxLayout *hLayout;
hLayout = new QHBoxLayout();
hLayout->addWidget( label);
hLayout->addWidget(comBox);
hLayout->setMargin(0);
hLayout->setAlignment(widget, Qt::AlignCenter);
hLayout->setContentsMargins(10, 0, 20, 0);
widget->setLayout(hLayout);
// 添加单元格
tableWidget->setCellWidget(0,0,widget);
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addWidget( tableWidget);
setLayout(mainLayout);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询