gpt4 book ai didi

qt - TableView 中的复选框和 itemdelegate

转载 作者:行者123 更新时间:2023-12-03 02:19:47 24 4
gpt4 key购买 nike

我正在实现一个继承自QitemDelegate的CheckBox,并将其放入QTableView中。

问题是,当我插入时,我需要将其居中。

据我了解负责 Paint 的方法。我写的如下:

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

要显得居中缺少什么?

所以我有委托(delegate):

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

最佳答案

如果您要扩展QItemDelegate 类,它有一个 drawCheck()函数,什么会为您绘制一个漂亮的居中复选框。您可以在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 方法。

关于qt - TableView 中的复选框和 itemdelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11800946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com