QStyledItemDelegate:在单击时将 QComboBox 值提交给模型

QStyledItemDelegate: commit QComboBox value to model on click

我在模型上为特定字段设置 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();
}

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

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

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

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

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 配置为 在用户 select 列表中的项目和组合框列表关闭时自动 提交数据,而不是要求额外按下 Enter?

您必须在选择项目时发出信号 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();
}