1

I'm having problems with my QTableView and QItemDelegate classes. For one column my delegate creates a simple combo box and everything works just fine. For my 2nd column I need a widget that has two combo boxes in a single widget.

I've written the following code in my QItemDelegate, just to be clear this only shows code for my 2nd column, the one that doesn't work. The other simple Combo-box isn't shown as it works fine:

QWidget *UserDefinedUnitsDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index ) const
{
    //set up a simple widget with a layout
    QWidget* pWidget = new QWidget(parent);
    QHBoxLayout* hLayout = new QHBoxLayout(pWidget);
    pWidget->setLayout(hLayout);

    //add two combo boxes to the layout
    QComboBox* comboEditor = new QComboBox(pWidget);    
    QComboBox* comboEditor2 = new QComboBox(pWidget);   

    //now add both editors to this
    hLayout->addWidget(comboEditor);
    hLayout->addWidget(comboEditor2);
    return pWidget;
}

Now this displays just fine but when I edit it and click elsewhere it doesn't stop editing. Can anyone offer any pointers?

Edit: So i need to call CommitData() and closeEditor() at some point. Can anyone offer pointers on where to call these?

Thanks.

6
  • How your questions is related to QItemDelegate? Show some code. Commented Apr 14, 2014 at 11:43
  • updated to show that this code is within a QItemDelegate::createEditor method. Thanks for pointing it out, it wasn't very clear. Commented Apr 14, 2014 at 12:58
  • Where do you call QAbstractItemView::closeEditor / commitData? Commented Apr 14, 2014 at 13:24
  • I've not set that up as when I used a simple ComboBox it appears to do that itself. Commented Apr 14, 2014 at 13:30
  • Yes. But you are using your custom editor. Commented Apr 14, 2014 at 14:30

1 Answer 1

1

You can keep the editor widget as a member of class and emit commitData when the current index of one of the comboboxes has changed. So you can connect currentIndexChanged(int) to a slot and emit commitData from there:

QWidget *UserDefinedUnitsDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index ) const
{
    //set up a simple widget with a layout
    pWidget = new QWidget(parent);
    QHBoxLayout* hLayout = new QHBoxLayout(pWidget);
    pWidget->setLayout(hLayout);

    //add two combo boxes to the layout
    QComboBox* comboEditor = new QComboBox(pWidget);    
    QComboBox* comboEditor2 = new QComboBox(pWidget);   

    connect(comboEditor,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int)));
    connect(comboEditor2,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int)));

    //now add both editors to this
    hLayout->addWidget(comboEditor);
    hLayout->addWidget(comboEditor2);
    return pWidget;
}

void UserDefinedUnitsDelegate::setData(int val)
{
    emit commitData(pWidget);
}
1
  • Hmm, it would work but ideally it wouldn't set the data 'til you've finished (possibly) editing both comboboxes. All I can think is to check for clicking elsewhere in the view. Commented Apr 14, 2014 at 17:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.