Qt - Centering a checkbox in a QTable
Asked Answered
L

8

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);
Lauder answered 13/2, 2013 at 6:42 Comment(0)
B
11

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.

Bekha answered 13/2, 2013 at 7:56 Comment(3)
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!Lauder
@SingerOfTheFall, how to get the checkstatus of the checkbox?Stuckup
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).Emprise
O
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!!

Orography answered 21/10, 2014 at 21:20 Comment(0)
B
11

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.

Bekha answered 13/2, 2013 at 7:56 Comment(3)
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!Lauder
@SingerOfTheFall, how to get the checkstatus of the checkbox?Stuckup
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).Emprise
R
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%;
Rockandroll answered 24/2, 2014 at 6:48 Comment(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.Sequoia
A
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);

Ackley answered 27/8, 2013 at 15:49 Comment(0)
B
2

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

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

Bort answered 21/11, 2013 at 18:17 Comment(0)
M
2

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%;");
Melodic answered 31/12, 2019 at 5:48 Comment(2)
For some reason margin-left:50%; margin-right:50%; is giving me off-center with respect to the column heading. Any ideas.Foliated
I ended up using the first method which worked correctly without the offset.Foliated
K
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());
Kief answered 20/5, 2016 at 5:7 Comment(0)
A
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.

Ariana answered 4/8, 2023 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.