title: checkbox and itemdelegate in a tableview
body: <p>I'm doing an implementation of a CheckBox that inherits from QitemDelegate, to put it into a QTableView.</p>
  
  <p>the problem is that I get when inserted flush left and I need it centered.</p>
  
  <p>As I understand the method that is responsible for the Paint. I have it written as follows:</p>
  
  <pre><code>void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const
  {
  bool checkValue;
  
  QStyleOptionButton BtnStyle;
  BtnStyle.state = QStyle::State_Enabled;
  
  if(index.model()-&gt;data(index, Qt::DisplayRole).toBool() == true)
  {
  BtnStyle.state |= QStyle::State_On;
  checkValue = true;
  }else{
  BtnStyle.state |= QStyle::State_Off;
  checkValue = false;
  }
  
  
  BtnStyle.direction = QApplication::layoutDirection();
  BtnStyle.rect = option.rect;
  QApplication::style()-&gt;drawControl(QStyle::CE_CheckBox,&amp;BtnStyle,painter );
  QApplication::style()-&gt;drawControl(QStyle::CE_CheckBox,&amp;BtnStyle,painter );
  }
  </code></pre>
  
  <p>what is missing to appear centered?</p>
  
  <p>so I have the delegated:</p>
  
  <p>.h</p>
  
  <pre><code>class BooleanWidget : public QWidget
  {
      Q_OBJECT
      QCheckBox * checkBox;
  
      public:
      BooleanWidget(QWidget * parent = 0)
      {
          checkBox = new QCheckBox(this);
          QHBoxLayout * layout = new QHBoxLayout(this);
          layout-&gt;addWidget(checkBox,0, Qt::AlignCenter);
  
      }
  
      bool isChecked(){return checkBox-&gt;isChecked();}
      void setChecked(bool value){checkBox-&gt;setChecked(value);}
  };
  
  class CheckBoxDelegate : public QItemDelegate
  {
      Q_OBJECT
  private:
      BooleanWidget *checkbox;
  
  public:
      CheckBoxDelegate(QObject *parent);
      ~CheckBoxDelegate();
      void setEditorData( QWidget *editor,const QModelIndex &amp;index )const;
      void setModelData( QWidget *editor,QAbstractItemModel *model,const QModelIndex &amp;index )const;
      QWidget *createEditor( QWidget *parent,const QStyleOptionViewItem &amp;/* option */,const QModelIndex &amp;/* index */ )const;
      void paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const;
  
  public slots:
      void changed( bool );
  
  };
  </code></pre>
  
  <p>.cpp</p>
  
  <pre><code>void CheckBoxDelegate::changed( bool value )
  {
      BooleanWidget *checkbox = static_cast&lt;BooleanWidget*&gt;( sender() );
      emit commitData( checkbox );
      emit closeEditor( checkbox );
  }
  
  QWidget *CheckBoxDelegate::createEditor( QWidget *parent,const QStyleOptionViewItem &amp;/* option */,const QModelIndex &amp;/* index */ ) const
  {
    BooleanWidget *editor = new BooleanWidget( parent );
    connect( editor, SIGNAL( toggled ( bool ) ), this, SLOT( changed( bool ) ) );
  
    return editor;
  }
  
  void CheckBoxDelegate::setEditorData( QWidget *editor,const QModelIndex &amp;index ) const
  {
      int value = index.model()-&gt;data(index, Qt::DisplayRole).toInt();
  
      BooleanWidget *checkbox = static_cast&lt;BooleanWidget*&gt;(editor);
  
      if(value == 1)
      {
          checkbox-&gt;setChecked(true);
      }
      else
      {
          checkbox-&gt;setChecked(false);
      }
  
  
  }
  
  void CheckBoxDelegate::setModelData( QWidget *editor,QAbstractItemModel *model,const QModelIndex &amp;index ) const
  {
      BooleanWidget *checkBox = qobject_cast&lt;BooleanWidget*&gt;( editor );
      Qt::CheckState value;
  
      if(checkBox-&gt;isChecked())
          value = Qt::Checked;
      else
          value = Qt::Unchecked;
  
      model-&gt;setData( index, value, Qt::DisplayRole);
  }
  
  void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const
  {
      drawCheck(painter, option, option.rect, index.data().toBool() ? Qt::Checked : Qt::Unchecked);
      drawFocus(painter, option, option.rect);
  }
  </code></pre>