Qt - Centering a checkbox in a QTable

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.


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

one line...

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

done!!