checkbox and itemdelegate in a tableview

jackajack picture jackajack · Aug 3, 2012 · Viewed 15.2k times · Source

I'm doing an implementation of a CheckBox that inherits from QitemDelegate, to put it into a QTableView.

the problem is that I get when inserted flush left and I need it centered.

As I understand the method that is responsible for the Paint. I have it written as follows:

void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
bool checkValue;

QStyleOptionButton BtnStyle;
BtnStyle.state = QStyle::State_Enabled;

if(index.model()->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()->drawControl(QStyle::CE_CheckBox,&BtnStyle,painter );
QApplication::style()->drawControl(QStyle::CE_CheckBox,&BtnStyle,painter );
}

what is missing to appear centered?

so I have the delegated:

.h

class BooleanWidget : public QWidget
{
    Q_OBJECT
    QCheckBox * checkBox;

    public:
    BooleanWidget(QWidget * parent = 0)
    {
        checkBox = new QCheckBox(this);
        QHBoxLayout * layout = new QHBoxLayout(this);
        layout->addWidget(checkBox,0, Qt::AlignCenter);

    }

    bool isChecked(){return checkBox->isChecked();}
    void setChecked(bool value){checkBox->setChecked(value);}
};

class CheckBoxDelegate : public QItemDelegate
{
    Q_OBJECT
private:
    BooleanWidget *checkbox;

public:
    CheckBoxDelegate(QObject *parent);
    ~CheckBoxDelegate();
    void setEditorData( QWidget *editor,const QModelIndex &index )const;
    void setModelData( QWidget *editor,QAbstractItemModel *model,const QModelIndex &index )const;
    QWidget *createEditor( QWidget *parent,const QStyleOptionViewItem &/* option */,const QModelIndex &/* index */ )const;
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

public slots:
    void changed( bool );

};

.cpp

void CheckBoxDelegate::changed( bool value )
{
    BooleanWidget *checkbox = static_cast<BooleanWidget*>( sender() );
    emit commitData( checkbox );
    emit closeEditor( checkbox );
}

QWidget *CheckBoxDelegate::createEditor( QWidget *parent,const QStyleOptionViewItem &/* option */,const QModelIndex &/* 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 &index ) const
{
    int value = index.model()->data(index, Qt::DisplayRole).toInt();

    BooleanWidget *checkbox = static_cast<BooleanWidget*>(editor);

    if(value == 1)
    {
        checkbox->setChecked(true);
    }
    else
    {
        checkbox->setChecked(false);
    }


}

void CheckBoxDelegate::setModelData( QWidget *editor,QAbstractItemModel *model,const QModelIndex &index ) const
{
    BooleanWidget *checkBox = qobject_cast<BooleanWidget*>( editor );
    Qt::CheckState value;

    if(checkBox->isChecked())
        value = Qt::Checked;
    else
        value = Qt::Unchecked;

    model->setData( index, value, Qt::DisplayRole);
}

void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    drawCheck(painter, option, option.rect, index.data().toBool() ? Qt::Checked : Qt::Unchecked);
    drawFocus(painter, option, option.rect);
}

Answer

Bal&#225;zs &#201;des picture Balázs Édes · Aug 3, 2012

If you are extending the QItemDelegate class, it has a drawCheck() function, what will draw you a nince, centered checkbox. You can use it in the paint() function.

Edit:

Here is an example, assuming you have a class called BooleanEditor, what inherits from QItemDelegate:

void BooleanEditor::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    drawCheck(painter, option, option.rect, index.data().toBool() ? Qt::Checked : Qt::Unchecked);
    drawFocus(painter, option, option.rect);
}

For keeping the checkbox centered when entering the edit mode, you can do something like this:

class BooleanWidget : public QWidget
{
    Q_OBJECT
    QCheckBox * checkBox;

    public:
    BooleanWidget(QWidget * parent = 0)
    {
        checkBox = new QCheckBox(this);
        QHBoxLayout * layout = new QHBoxLayout(this);
        layout->addWidget(checkBox,0, Qt::AlignCenter);
    }

    bool isChecked(){return checkBox->isChecked();}
    void setChecked(bool value){checkBox->setChecked(value);}
};

And in your ItemDelegates createEditor() method return an instance of this BooleanWidget class. In your setModelData() and setEditorData() you can now cast your input widget to this BooleanWidget:

BooleanWidget * widget = qobject_cast<BooleanWidget*>(editor);

and then use the is/setChecked method.