【问题标题】:Qt - Centering a checkbox in a QTableQt - 在 QTable 中将复选框居中
【发布时间】:2013-01-28 16:00:41
【问题描述】:

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

我正在添加QCheckbox 使用:

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

【问题讨论】:

    标签: c++ qt qt-creator qtablewidgetitem qcheckbox


    【解决方案1】:

    为 Barry Mavin 竖起两个大拇指!你甚至不必子类化。

    一行...

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

    完成!

    【讨论】:

      【解决方案2】:

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

      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
      

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

      【讨论】:

      • 这似乎应该更容易。我很长一段时间都在浏览文档,试图找到一个功能或做我想要的东西。但是你发布的内容很完美,不过我自己永远也不会想到这一点。谢谢!
      • @SingerOfTheFall,如何获取复选框的检查状态?
      【解决方案3】:

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

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

      【讨论】:

      • 我在 PyQt4 中,这仅在设置初始值时有效。也就是说,如果我调整列的大小,它不会继续使复选框居中,它只会在第一次创建时居中。
      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

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

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

          【讨论】:

          • 出于某种原因margin-left:50%; margin-right:50%; 让我偏离了列标题的中心。任何想法。
          • 我最终使用了第一种在没有偏移的情况下正常工作的方法。
          【解决方案7】:
          #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());
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-04-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多