Pergunta

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);
Foi útil?

Solução 2

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.

Outras dicas

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

one line...

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

done!!

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

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

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

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

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%;");
#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());
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top