需要幫忙...
uj5u.com熱心網友回復:
必須單擊單元格 3 次才能更改值的原因是因為您需要雙擊編輯(創建編輯器)并再次單擊呼叫UpdateUseButton。您不需要這樣做,您可以在創建編輯器時執行單擊。實際上,您甚至不需要 .,UpdateUseButton因為您可以在setModelData. 將以下插槽添加到您的PushButtonDelegate:
void commitAndCloseEditor();
然后像這樣實作類:
QWidget* PushButtonDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (index.model()->headerData(index.column(), Qt::Horizontal, Qt::DisplayRole).toString() == constants::kUse)
{
const auto btn = new QPushButton(parent);
const auto value = index.data().toString();
btn->setCheckable(true);
btn->setChecked(value == "Yes" ? true : false);
btn->setFocusPolicy(Qt::NoFocus);
// You don't need this, you can update your data in setModelData instead
// connect(btn, SIGNAL(clicked(bool)), this, SLOT(UpdateUseButton(bool)));
// the click will commit the data and close the editor
// we connect it with a queued connection so that the slot is called after this function exits
connect(btn, &QPushButton::clicked, this, &PushButtonDelegate::commitAndCloseEditor, Qt::QueuedConnection);
emit btn->clicked(false); // perform a click programatically to that the commitAndCloseEditor will be called after we exit this function
return btn;
}
else
{
return QStyledItemDelegate::createEditor(parent, option, index);
}
}
void PushButtonDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
if (index.model()->headerData(index.column(), Qt::Horizontal, Qt::DisplayRole).toString() == constants::kUse)
{
//nothing to do when you are in your delegate column
}
else
{
QStyledItemDelegate::setEditorData(editor, index);
}
}
void PushButtonDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
if (index.model()->headerData(index.column(), Qt::Horizontal, Qt::DisplayRole).toString() == constants::kUse)
{
// set your model data here
model->setData(index, (index.data().toString() == "Yes") ? "No" : "Yes");
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void PushButtonDelegate::commitAndCloseEditor()
{
auto* editor = qobject_cast<QWidget*>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
請注意,我使用 aQt::QueuedConnnection來連接clicked信號。這確保了commitAndCloseEditor插槽將在兩者之后被createEditor呼叫setEditorData退出(通過在每個函式的開頭使用 print 陳述句很容易檢查)。如果您洗掉排隊的連接,則將在回傳實際編輯器commitAndCloseEditor之前呼叫createEditor,因此該插槽將不會執行任何操作,因為編輯器尚未打開。
現在,對于該paint方法......這似乎btn_->setChecked是未在按鈕中正確顯示背景顏色的原因。我不確定它為什么會這樣,但一種解決方法是使用這樣的樣式表:
void PushButtonDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (index.model()->headerData(index.column(), Qt::Horizontal, Qt::DisplayRole).toString() == constants::kUse)
{
painter->save();
btn_->setGeometry(option.rect);
const auto value = index.data().toString();
btn_->setText(value);
QString stylesheet = QString("background-color: %1").arg((value == "Yes") ? "green" : "red" );
btn_->setStyleSheet(stylesheet);
const QPixmap map = btn_->grab();
painter->drawPixmap(option.rect.x(), option.rect.y(), map);
painter->restore();
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
現在應該通過雙擊切換值,結果(在我的 Windows 框中)如下所示:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/509843.html
標籤:C qt小部件qt5
