14

In QtCreater I added a table to my project. in my code I am generating some data to output into the table. I want to add a QCheckbox into each row to allow the row to be selected. All the content of the table is aligned left, how do I make only these checkboxes in the first column of every row align to the center?

I'm adding the QCheckbox using:

ui->data_table->setCellWidget(rowCount,0, new QCheckBox);
László Papp
  • 51,870
  • 39
  • 111
  • 135
Mitch
  • 519
  • 2
  • 7
  • 16

8 Answers8

22

Two thumbs up for Barry Mavin! You don't even have to subclass.

one line...

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

done!!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Ph0t0n
  • 970
  • 9
  • 10
10

I usually use a layout and a container widget for this. It is an ugly solution, but it works:

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);

So basically you will have:

Table Cell -> Widget -> Layout -> Checkbox

you'll have to consider it if you will need to access the checkbox through the table.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • It seems like something that should be much easier then that. I was looking through docs for a long time trying to find a function or something that did what I wanted. But what you posted works perfectly, I would of never figured that out on my own though. Thanks! – Mitch Feb 13 '13 at 08:17
  • @SingerOfTheFall, how to get the checkstatus of the checkbox? – Dark King Jan 18 '20 at 14:45
  • Alignment is perfect here. It's not when using stylesheet proposed by other answers (where checkbox ends up shifted to the right by some pixels). – jpo38 Mar 22 '22 at 14:58
7

This is an old post but in fact there is a much easier and lighter way of achieving this, just subclass QCheckBox and set the stylesheet to

margin-left:50%;
margin-right:50%;
slavoo
  • 5,798
  • 64
  • 37
  • 39
Barry Mavin
  • 71
  • 1
  • 1
  • 1
    Im in PyQt4 and this only works when setting the initial value. That is, if I resize the column it does not continue to center the checkbox, it only centers it when it is first created. – Spencer Nov 17 '17 at 04:11
5

It works for me, but my checkbox is not completely displayed.

To have a complete view of the widget, remove margins in layout :

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

slist
  • 51
  • 1
  • 1
2

As stated in similar question around Stack Overflow, it's currently an open BUG:

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

Jonaias
  • 375
  • 3
  • 10
1

can be center like this too using layout if want to add more customization

// 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

OR simply add left right margins

checkBox->setStyleSheet("margin-left:50%; margin-right:50%;");
user889030
  • 4,353
  • 3
  • 48
  • 51
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());
AnselmRu
  • 51
  • 2
0

For the regular rows of data, you can just adjust your model, testing the role in the data method. This seems to work for me:

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

This does not get solved for the header however - so if you have a header with a checkbox (like select all), that won't be solved. But this solves the data problem of aligning at least.

Seth D. Fulmer
  • 490
  • 1
  • 4
  • 21