Qt - 如何将复选框居中放置于QTable中

14
在QtCreater中,我添加了一个表格到我的项目中。在我的代码中,我生成一些数据以输出到表格中。我想在每行中添加一个 QCheckbox 以允许选择该行。表格中的所有内容都左对齐,如何使每行第一列中的这些复选框居中对齐?
我正在使用以下方式添加 QCheckbox
ui->data_table->setCellWidget(rowCount,0, new QCheckBox);
8个回答

22

Barry Mavin值得点赞!甚至不需要子类化。

一行文字...

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

完成了!!


10

我通常使用布局和容器小部件来解决这个问题。虽然它不太美观,但是它可以解决问题:

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

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


这似乎应该比那容易得多。我花了很长时间查找文档,试图找到一个能够实现我想要的功能的函数或其他东西。但是你发布的内容完美地解决了我的问题,我自己永远也想不到。谢谢! - Mitch
@SingerOfTheFall,如何获取复选框的选中状态? - Dark King
这里的对齐是完美的。但是在使用其他答案提供的样式表时(复选框会向右移动几个像素),它并不完美。 - jpo38

7

这是一篇旧帖子,但实际上有更简单、更轻量级的方法来实现此功能,只需子类化QCheckBox并将stylesheet设置为

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

1
我在PyQt4中,这只有在设置初始值时才起作用。也就是说,如果我调整列的大小,它不会继续居中复选框,它只会在第一次创建时居中。 - Spencer

5

这个方法对我很有效,但我的复选框没有完全显示。

为了完整地显示该部件,需要在布局中去除边距:

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


2

1

如果想要添加更多自定义选项,也可以使用布局将其居中。

// 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%;");

1
由于某些原因,“margin-left:50%; margin-right:50%;”使得我的列标题相对于中心偏离。有什么想法吗? - drescherjm
我最终使用了第一种方法,它在没有偏移量的情况下正常工作。 - drescherjm

0
#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());

0
对于常规的数据行,你可以调整你的模型,在数据方法中测试这个角色。这对我来说似乎是有效的。
if(role == Qt::TextAlignmentRole)
    {
    return(Qt::AlignHCenter); // or AlignCenter - either one works I beleive
    }

这个问题无法解决标题的情况 - 如果您有一个带复选框(例如全选)的标题,那将无法解决。但是,这至少解决了数据对齐的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接