Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - Centering a checkbox in a QTable

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);
like image 765
Mitch Avatar asked Feb 13 '13 06:02

Mitch


1 Answers

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.

like image 150
SingerOfTheFall Avatar answered Sep 16 '22 20:09

SingerOfTheFall