gpt4 book ai didi

c++ - QStyledItemDelegate : commit QComboBox value to model on click

转载 作者:搜寻专家 更新时间:2023-10-31 02:06:06 24 4
gpt4 key购买 nike

我正在我的模型上为特定字段设置一个 QStyledItemDelegate,并从 QStyledItemDelegate::createEditor

返回一个 QComboBox
QComboBox* createEditor(QWidget* parent)
{
QComboBox* cb = new QComboBox(parent);

cb->addItem("UNDEFINED");
cb->addItem("TEST");
cb->addItem("OSE");
cb->addItem("TSE");

return cb;
}

void setEditorData(QWidget* editor, const QModelIndex& index)
{
QComboBox* cb = qobject_cast<QComboBox*>(editor);
if (!cb)
throw std::logic_error("editor is not a combo box");

QString value = index.data(Qt::EditRole).toString();
int idx = cb->findText(value);
if (idx >= 0)
cb->setCurrentIndex(idx);

cb->showPopup();
}

这工作正常,当我选择有问题的字段时,我会看到一个组合框。

combobox open

当我从下拉列表中选择一个选项时,组合框会关闭,并且该项目旁边会显示一个下拉图标:

selected

此时我想调用 QStyledItemDelegate::setModelData 函数,以便选择列表中的项目将数据提交给模型。

但是,我需要先按 Enter 来提交数据(这样下拉图标就会消失)

committed

void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index)
{
QComboBox* cb = qobject_cast<QComboBox*>(editor);
if (!cb)
throw std::logic_error("editor is not a combo box");

model->setData(index, cb->currentText(), Qt::EditRole);
}

问题:

当用户选择列表中的项目并且组合框列表关闭时,我如何配置我的QComboBox自动提交数据,而不需要额外按下输入?

最佳答案

当一个项目被选中时,你必须发出信号 commitDatacloseEditor ,如下例所示:

#include <QApplication>
#include <QStandardItemModel>
#include <QListView>
#include <QStyledItemDelegate>
#include <QComboBox>

class ComboBoxDelegate: public QStyledItemDelegate{
public:
using QStyledItemDelegate::QStyledItemDelegate;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
Q_UNUSED(option)
Q_UNUSED(index)

QComboBox* editor = new QComboBox(parent);
connect(editor, QOverload<int>::of(&QComboBox::activated),
this, &ComboBoxDelegate::commitAndCloseEditor);
editor->addItems({"UNDEFINED", "TEST", "OSE", "TSE"});

return editor;
}
void setEditorData(QWidget *editor, const QModelIndex &index) const{
QComboBox* cb = qobject_cast<QComboBox*>(editor);
if (!cb)
throw std::logic_error("editor is not a combo box");

QString value = index.data(Qt::EditRole).toString();
int idx = cb->findText(value);
if (idx >= 0)
cb->setCurrentIndex(idx);
cb->showPopup();
}
private:
void commitAndCloseEditor(){
QComboBox *editor = qobject_cast<QComboBox *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
};


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QListView view;

QStandardItemModel model;

for(int i=0; i<10; i++){
model.appendRow(new QStandardItem("UNDEFINED"));
}
view.setItemDelegate(new ComboBoxDelegate(&view));
view.setModel(&model);
view.show();
return a.exec();
}

关于c++ - QStyledItemDelegate : commit QComboBox value to model on click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50825564/

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