复选框,并itemdelegate在实现代码如下(checkbox and itemdelegate

2019-06-26 18:11发布

我这样做,从QitemDelegate继承的复选框的实现,把它变成一个QTableView中。

问题是,我得到当插入左对齐,我需要它为中心。

据我所知,它负责画图的方法。 我把它写成如下:

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

什么是缺少居中显示?

所以我已经委派:

。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 1:

如果要扩展的QItemDelegate类,它有一个drawCheck()函数,你会画你nince,居中复选框。 您可以在paint()函数中使用它。

编辑:

下面是一个例子,假设你有一个叫做类BooleanEditor ,从什么继承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);
}

为了保持进入编辑模式时,中心的复选框,你可以这样做:

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

而在你ItemDelegates createEditor()方法返回此BooleanWidget类的一个实例。 在您的setModelData()和setEditorData()现在可以投你输入小部件此BooleanWidget:

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

然后使用IS / setChecked方法。



Answer 2:

这就是我所做的,以中心allign编辑器控制:

QWidget *checkBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    QCheckBox *editor = new QCheckBox(parent);
    editor->setTristate(allowTriState); //my class' variable
    // this does the trick :)
    editor->setStyleSheet("QCheckBox {margin-left: 43%; margin-right: 57%;}"); 
    // this should do it better
    // editor->setStyleSheet("QCheckBox {margin-left: auto; margin-right: auto;}");
    // but in my case the checkbox is slightly to the left of original
    return editor;
}


Answer 3:

我解决这个问题,以下项目委托:

booleanitemdelegate.h

#ifndef BOOLEANITEMDELEGATE_H
#define BOOLEANITEMDELEGATE_H

#include <QItemDelegate>



class BooleanItemDelegate : public QItemDelegate
{
public:
    BooleanItemDelegate(QObject *parent);

public:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;public:
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
};

#endif // BOOLEANITEMDELEGATE_H

booleanitemdelegate.cpp

#include "booleanitemdelegate.h"

BooleanItemDelegate::BooleanItemDelegate(QObject *parent):
    QItemDelegate(parent)
{

}

void BooleanItemDelegate::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);
}

bool BooleanItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if(event->type() == QEvent::MouseButtonRelease){
        model->setData(index, !model->data(index).toBool());
        event->accept();
    }
    return QItemDelegate::editorEvent(event, model, option, index);
}

我希望,我可以提供帮助。



Answer 4:

解决了这个问题,如下所示:

认为,从itemDelegate QStyledItemDelegate继承并重新实现方法“漆”和“editorEvent”。

在该方法中“editorEvent”发出指示选中了哪个行的信号。

下面是代码

class ItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT

signals:
    void clickSignal(int);


public:
    ItemDelegate(QObject *parent = 0)
        : QStyledItemDelegate(parent)
    {
    }

    void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    {
        QStyleOptionViewItemV4 viewItemOption(option);

        if (index.column() == 0) {
            const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
            QRect newRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
                                                QSize(option.decorationSize.width() + 5,option.decorationSize.height()),
                                                QRect(option.rect.x() + textMargin, option.rect.y(),
                                                      option.rect.width() - (2 * textMargin), option.rect.height()));
            viewItemOption.rect = newRect;
        }
        QStyledItemDelegate::paint(painter, viewItemOption, index);

    }

    virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
                             const QModelIndex &index)
    {
        Q_ASSERT(event);
        Q_ASSERT(model);

        // make sure that the item is checkable
        Qt::ItemFlags flags = model->flags(index);
        if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled))
            return false;
        // make sure that we have a check state
        QVariant value = index.data(Qt::CheckStateRole);
        if (!value.isValid())
            return false;
        // make sure that we have the right event type
        if (event->type() == QEvent::MouseButtonRelease) {
            const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
            QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
                                                  option.decorationSize,
                                                  QRect(option.rect.x() + (2 * textMargin), option.rect.y(),
                                                        option.rect.width() - (2 * textMargin),
                                                        option.rect.height()));

        } else {
            return false;
        }
        Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
                                ? Qt::Unchecked : Qt::Checked);




        emit(clickSignal(index.row()));

        return model->setData(index, state, Qt::CheckStateRole);
    }


};

类在那里我有一个连接QTableView中做到这一点:

connect(check,SIGNAL(clickSignal(int)),this,SLOT(CheckMark(int))); //check the itemDelegate

与被标记的检查执行的操作



Answer 5:

没有太多的大惊小怪直接设置矩形而不是:

BtnStyle.rect = option.rect;

构造一个新的,并查阅QRect移动,只要你喜欢x坐标向右之多:

QRect r = option.rect;
QRect r1(r.x() + 34, r.y(), r.width(), r.height());
BtnStyle.rect = r1;


文章来源: checkbox and itemdelegate in a tableview