Qt-使QTable中的复选框居中

Qt - Centering a checkbox in a QTable

本文关键字:复选框 Qt- QTable      更新时间:2023-10-16

在QtCreater中,我为我的项目添加了一个表。在我的代码中,我正在生成一些要输出到表中的数据。我想在每行中添加一个QCheckbox,以允许选择该行。表中的所有内容都向左对齐,如何使每行第一列中的这些复选框与中心对齐?

我正在使用添加QCheckbox

ui->data_table->setCellWidget(rowCount,0, new QCheckBox);

为巴里·马文竖起大拇指!你甚至不需要划分子类。

一行。。。

pCheckBox->setStyleSheet("margin-left:50%; margin-right:50%;");

完成!!

我通常使用布局和容器小部件。这是一个丑陋的解决方案,但它有效:

QWidget * w = new QWidget();
QHBoxLayout *l = new QHBoxLayout();
l->setAlignment( Qt::AlignCenter );
l->addWidget( <add your checkbox here> );
w->setLayout( l );
ui->data_table->setCellWidget(rowCount,0, w);

所以基本上你会有:

Table Cell -> Widget -> Layout -> Checkbox

如果您需要通过表访问复选框,则必须考虑它。

这是一篇老文章,但实际上有一种更简单、更轻松的方法可以实现这一点,只需将QCheckBox子类化并将stylesheet设置为

margin-left:50%;
margin-right:50%;

它对我有效,但我的复选框没有完全显示。

要获得小部件的完整视图,请删除布局中的边距:

l->setContentsMargins(0,0,0,0);

如Stack Overflow的类似问题所述,它目前是一个开放的BUG:

https://bugreports.qt-project.org/browse/QTBUG-5368

如果想添加更多自定义,也可以使用布局来居中

// Create a widget that will contain a checkbox
 QWidget *checkBoxWidget = new QWidget();
 QCheckBox *checkBox = new QCheckBox();      // We declare and initialize the checkbox
 QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget); // create a layer with reference to the widget
 layoutCheckBox->addWidget(checkBox);            // Set the checkbox in the layer
 layoutCheckBox->setAlignment(Qt::AlignCenter);  // Center the checkbox
 layoutCheckBox->setContentsMargins(0,0,0,0);    // Set the zero padding
ui->my_table_view->setCellWidget(row_number,column_number, checkBoxWidget);  // set cell widget

或者简单地添加左右边距

checkBox->setStyleSheet("margin-left:50%; margin-right:50%;");
#if QT_VERSION < 0x046000
#include <QCommonStyle>
class MyStyle : public QCommonStyle {
public:
  QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget = 0) const {
    switch(subElement) {
      case QStyle::SE_CheckBoxIndicator: {
        QRect r = QCommonStyle::subElementRect(subElement, option, widget);
        r.setRect( (widget->width() - r.width())/2, r.top(), r.width(), r.height());
        return QRect(r);
      }
      default: return QCommonStyle::subElementRect(subElement, option, widget);
    }
  }
};
#else
#include <QProxyStyle>
#include <QStyleFactory>
class MyStyle: public QProxyStyle {
public:
  MyStyle():QProxyStyle(QStyleFactory::create("Fusion")) {}
  QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget = 0) const {
    switch(subElement) {
      case QStyle::SE_CheckBoxIndicator: {
        QRect r = QProxyStyle::subElementRect(subElement, option, widget);
        r.setRect( (widget->width() - r.width())/2, r.top(), r.width(), r.height());
        return QRect(r);
      }
      default: return QProxyStyle::subElementRect(subElement, option, widget);
    }
  }
};
#endif
QCheckBox *box = new QCheckBox();
box->setStyle(new MyStyle());

对于规则的数据行,您可以调整模型,测试数据方法中的角色。这似乎对我有效:

if(role == Qt::TextAlignmentRole)
    {
    return(Qt::AlignHCenter); // or AlignCenter - either one works I beleive
    }

然而,这并不能为标头解决——因此,如果您有一个带有复选框的标头(如全选),则无法解决。但这至少解决了对齐的数据问题。