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

C++ QStyledItemDelegate:单击时将QComboBox值提交给模型,c++,qt,qt5,qcombobox,qitemdelegate,C++,Qt,Qt5,Qcombobox,Qitemdelegate,我正在为特定字段在模型上设置QStyledItemDelegate,并从QStyledItemDelegate::createEditor QComboBox* createEditor(QWidget* parent) { QComboBox* cb = new QComboBox(parent); cb->addItem("UNDEFINED"); cb->addItem("TEST"); cb->addItem("OSE"); c

我正在为特定字段在模型上设置
QStyledItemDelegate
,并从
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();
}
问题:


当用户在列表中选择一项且组合框列表关闭时,如何将我的
QComboBox
配置为自动提交数据,如以下示例所示,当选择某个项目时,您需要发出信号
commitData
closeEditor
,而不需要再按一下
Enter

#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();
}
#包括
#包括
#包括
#包括
#包括
类ComboBoxDelegate:公共QStyledItemDelegate{
公众:
使用QStyledItemDelegate::QStyledItemDelegate;
QWidget*createEditor(QWidget*父项、常量QStyleOptionViewItem和option、常量QModelIndex和索引)常量{
Q_未使用(可选)
Q_未使用(索引)
QCOMBOX*编辑器=新的QCOMBOX(父级);
连接(编辑器,QOverload::of(&QComboBox::已激活),
这是(&ComboBoxDelegate::commitAndCloseEditor);
编辑器->附加项({“未定义”、“测试”、“OSE”、“TSE”});
返回编辑器;
}
void setEditorData(QWidget*编辑器,常量QModelIndex和index)常量{
QComboBox*cb=qobject\u cast(编辑器);
如果(!cb)
抛出std::logic_错误(“编辑器不是组合框”);
QString值=index.data(Qt::EditRole).toString();
int idx=cb->findText(值);
如果(idx>=0)
cb->setCurrentIndex(idx);
cb->showPopup();
}
私人:
void committencloseeditor(){
QComboBox*编辑器=qobject_cast(sender());
发出提交数据(编辑器);
发出关闭编辑器(编辑器);
}
};
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
QListView视图;
QS标准模型;
对于(int i=0;i
#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();
}