for(int i=0; i<page.size(); i++){
User user= Poco::AnyCast<User>(*it);
ui.table->setItem(i,0,new QTableWidgetItem(user.userName));
ui.table->setItem(i,1,new QTableWidgetItem(user.sex));
ui.table->setItem(i,2,new QTableWidgetItem(user.age));
QPushButton* btn_edit = new QPushButton();
btn_edit = new QPushButton();
btn_edit->setText("Edit");
ui.table->setCellWidget(i,3,(QWidget*)btn_edit);
++it;
}
我使用函数setCellWidget()将QPushButton添加到单元格中, 我知道,如果它是QTableWidgetItem,我可以使用:
ui.table->item(0,3)->setTextAlignment(QT::AlignHCenter)
但它是一个小工具,
QTableWidgetItem item = ui.table->item(0,3);
该项目为空。
我可以使用
获取单元格ui.table->cellWidget(0,3).
如何使按钮在单元格中居中?
答案 0 :(得分:11)
试试这个:
QWidget* pWidget = new QWidget();
QPushButton* btn_edit = new QPushButton();
btn_edit->setText("Edit");
QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(btn_edit);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0, 0, 0, 0);
pWidget->setLayout(pLayout);
ui.table->setCellWidget(i, 3, pWidget);