我正在執行從QitemDelegate繼承的CheckBox,將其放入QTableView中。tableview中的複選框和itemdelegate
問題是,當我插入flush flush時,我需要它居中。
據我瞭解負責油漆的方法。我寫它如下:
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);
}
能舉個例子嗎?因爲據我看到,該函數的構造如下:drawCheck(QPainter *畫家,常量QStyleOptionViewItem&選項,QRect&rect,Qt ::狀態); – jackajack 2012-08-09 20:16:32
我添加了一個例子。如果你想讓它可編輯(而不僅僅是顯示),那麼你應該重寫你的tablemodels flags()函數。 – 2012-08-09 20:30:10
好的謝謝,我解決了問題的一部分。該檢查居中。現在的問題是,我把它放在QTableView中,點擊支票將被移到左邊而沒有選中。我必須給doubleclick來選擇 – jackajack 2012-08-09 20:42:35