QtableWidget表格中添加控件怎么居中

QtableWidget表格中添加控件怎么居中,第1张

tableWidget->setStyleSheet("QTableWidget::item{border:1px solid }")

//

//表格表头的显示与隐藏

tableWidget->verticalHeader()->setVisible(false) //隐藏列表头

tableWidget->horizontalHeader()->setVisible(false)//隐藏行表头

//tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers)

//设置行高

for(int i= 0i<tableWidget->rowCount()i++)

{

tableWidget->setRowHeight(i, 60)

}

//设置列宽

for(int i= 0i<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)

1 定义一个按钮

[cpp] view plain copy

QPushButton * pBtn = new QPushButton()

2 链接信号与曹

[cpp] view plain copy

connect(pBtn, SIGNAL(clicked()), this, SLOT(OnBtnClicked()))

3 按钮添加到单元格内

[cpp] view plain copy

table->setCellWidget(0,0,pBtn)//如果点击按钮出现崩溃现象,就添加QTableWidgetItem 到按钮的那个单元格

4 实现按钮的事件

[cpp] view plain copy

void myPic::OnBtnClicked(void)

{

QPushButton * senderObj=qobject_cast<QPushButton *>(sender())

if(senderObj == 0)

{

return

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11251002.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-14
下一篇2023-05-14

发表评论

登录后才能评论

评论列表(0条)

    保存