Important: Please read the Qt Code of Conduct -

How to set QComboxItem in a QStyledItemDelegate

  • I have a QStyleItemDelegate assigned to a QTableView column. This delegate has both QLineEdit and QComboBox editors depending on the data. THis delegate works fine operating on user input, but I would like to know how to programatically set the value of item when the editor is a QComboBox. Is there an easy way to do this ? Any help is very much appreciated

  • Hi,

    I am not experienced with Qt's model/view framework so I almost don't know what I am talking about, but can "this": be what you are looking for?

    Since you are using a QTableView and not a QTableWidget, I guess you need to manipulate the model programmatically so that the data shows up in the UI.

  • Thanks for the hint, I looked at that call QAbstractItemDelegate::setEditorData() but not sure how to call it, as I do not have a handle to the first argument it requires (QWidget *editor), also not sure if it will work as it sets the value of the Editor, I want to set the value of the QStandardItem to a value found in a QStyledItemDelegate->Editor->QComboBox. Maybe QAbstractItemDelegate::setModelData() is the way to go, but again I do not have a handle to all the arguments this method requires.

  • Lifetime Qt Champion


    setEditorData is called for you when you activate the edition mode of your cell. You can reimplement it when you have custom editor. In that function you receive the index corresponding to the cell you are going to edit. With that you can retrieve the value and update your editor accordingly.

    setModelData is called for you when the editor is closed so you can store back the value to your model, so in that one you can call your QComboBox::currentIndex/text and store it in the model.

    Hope it helps

  • Hi

    Still not sure how to call setModelData() from outside the Delegate. I have a Custom delegate which inheerits from QStyedItemDelegate and basically just establishes the createEdtior() method. What I want to be set an index of QComboBox that is part of createEditor method. Any more suggestions on how I can set comboBox->setCurrentIndex(1) for example

  • Lifetime Qt Champion

    You don't call setModelData outside the delegate. If you want to modify the model outside the delegate you call the model's setData.

    Do it in setEditorData, however the content of the combo box should reflect the the content of the model

  • Could not get it to work. item->setData() and model->setData() for every role (UserRole,EditRole, etc) would never trigger a QStyledItemDelegate::setModelData();

    Maybe it is a bug

  • Lifetime Qt Champion

    Neither will, setModelData is called after you closed the editor

  • Yeah I know now. So do you have any advise on how I can achieve my goal here

  • Lifetime Qt Champion

    You wrote: comboBox->setCurrentIndex(1);

    Where does this "1" come from ? Is it a fixed value ?

  • Just forcing the index to be one so I can see something. The combobox has at least two entries

  • Subclass and reimplement QStyledItemDelegate::createEditor

    call QStyledItemDelegate::createEditor version there then check
    if widget returned from there is combobox, like below

    @QWidget* MyStyledItemDelegate::createEditor( QWidget parent,
    const QStyleOptionViewItem &option, const QModelIndex &index) const
    w = QStyledItemDelegate::createEditor( parent, option, index);
    if( w->inherits("QComboBox") )
    QComboBox* edit = static_cast<QComboBox*>( w );
    ... do smth

    return w;

  • Yes I have implemented createEditor(), it is used to create a combo box when a user clicks on a cell item, but how to call it from outside as you suggest is the question ? What do I use for the arguments. For instance what do I supply for the second argument -QStyeOptionViewItem

  • Lifetime Qt Champion

    just do it in setEditorData

    void MyDelegate::​setEditorData(QWidget * editor, const QModelIndex & index) const
    QComboBox * comboBox = qobject_cast<QComboBox*>(editor);
    if (comboBox) {

    Again, these functions are called for you, you don't call them

  • You do not call createEditor yourself, it will be called with appropriate parameters. All you need is replace base class version with yours and
    add the behavior you want.

    Also MyDelegate::​setEditorData mentioned above might be an option I think you goal was to change the behavior only once, so I think createEditor is better choice, but can't state it without testing.

  • Well I got it to work for one item, but for two or more items the second item fails. I think the call to edit gets on the event loop and so things are not whre they ought to be. Here is what I have

    @// main loop where I want to set the data:
    for (int i=i=0;i<childCount;i++) {
    valueDestin = (FieldValueItem)destination.child(i,AvailableValueCol);
    QModelIndex mi = valueDestin->index();
    if ((i == 2) || (i == 3)) {
    fieldValueDelegate->setSilent(true,1); // set index to item
    // qApp->processEvents(QEventLoop::WaitForMoreEvents,500);
    void FieldValueDelegate::setSilent(bool on, int ci)
    isSilent = on;
    comboIndex = ci;
    if (!isSilent)
    editCB = 0;
    void FieldValueDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
    qDebug() << "Set Editor Data" << FILE << LINE;
    QComboBox *cb = qobject_cast <QComboBox *> (editor);
    if (cb && isSilent) {


    If I just have i set to 2, or i set to 3 (that is one index) in the loop above the index will get set, and I will see the result in my treeview, but when I try to both indexes in it fails on the second pass . There is an error message from Qt on the second iiteration:
    editing failed

    I guess Qt was not made to do this kind of stuff. If you know a quick fix please let me know, otherwise I will just give this thread a rest. Thanks to all that tried to help

Log in to reply