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

Ste*_*mer 2 c++ qt qitemdelegate qcombobox qt5

我正在QStyledItemDelegate为特定字段设置模型,并返回QComboBox来自QStyledItemDelegate::createEditor

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();
}
Run Code Online (Sandbox Code Playgroud)

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

组合框打开

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

已选择

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

但是,我需要先按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);
}
Run Code Online (Sandbox Code Playgroud)

问题:

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

eyl*_*esc 5

当选择一个项目时,您必须发出信号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();
}
Run Code Online (Sandbox Code Playgroud)