QTableWidget文本居中、按钮等控件居中显示方法。

QTableWidget文本居中、按钮等控件居中显示方法。

文本居中显示:

QTableWidgetItem *itemName = new QTableWidgetItem("名称");
itemName->setTextAlignment(Qt::AlignCenter);

控件居中显示:

QTableWidget可以通过setCellWidget来添加控件,但是默认不是居中显示,嵌套一个水平布局可以实现居中显示。

QPushButton *btnDelete = new QPushButton("删除");
QWidget *widgetDelete = new QWidget;
QHBoxLayout *layoutDelete = new QHBoxLayout;
layoutDelete->addWidget(btnDelete);
layoutDelete->setMargin(0);
layoutDelete->setAlignment(btnDelete, Qt::AlignCenter);
widgetDelete->setLayout(layoutDelete);
connect(btnDelete, SIGNAL(clicked()), this, SLOT(on_cellBtnDelete_clicked()));
ui->tableWidget->setCellWidget(i,5,widgetDelete);
//点击列表中的按钮
void frmStore::on_cellBtnDelete_clicked()
{
    QToolButton *senderObj=qobject_cast<QToolButton*>(sender());
    QWidget *widgetDelete  = qobject_cast<QWidget*>(senderObj->parent());
    QModelIndex idx = ui->tableWidget->indexAt(QPoint(widgetDelete->frameGeometry().x(),widgetDelete->frameGeometry().y()));
    QMessageBox::about(this,"提示",QString("点击按钮,第%1行,第%2列").arg(QString::number(idx.row())).arg(QString::number(idx.column())));
}

Qt中QToolButton和QPushButton按钮去掉边框、去掉背景、指定图标和设置鼠标悬浮小手

 

原创文章,作者:365开发网,如若转载,请注明出处:https://www.365codes.com/533.html