0

I have QComboBox as an item delegate in QTableView bound with a table from MySQL database. When I choose an item, the corresponding value in the database does not change if I don't press the Enter key. I've tried using this answer:

QWidget * StatusComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QComboBox* box = new QComboBox(parent);
    box->setEditable(false);
    box->addItem("Zero");
    box->addItem("One");
    box->addItem("Two");
    box->addItem("Four");
    box->addItem("Five");
    connect(box, SIGNAL(currentIndexChanged(int)), this, SLOT(setData(int)));
    return box;
}

void StatusComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    int value = index.model()->data(index).toInt();
    QComboBox* box = static_cast<QComboBox*>(editor);
    box->setCurrentIndex(value);
//    box->showPopup();
}

void StatusComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QComboBox* box = static_cast<QComboBox*>(editor);
    int value = box->currentIndex();
    model->setData(index, value);
}

void StatusComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    QComboBox* box = static_cast<QComboBox*>(editor);
    box->setGeometry(option.rect);
}

void StatusComboDelegate::setData(int val)
{
    QComboBox *editor = qobject_cast<QComboBox *>(sender());
    emit commitData(editor);
}

but I still can't commit a value without pressing Enter. What can I do?

Michael
  • 5,095
  • 2
  • 13
  • 35

0 Answers0