PDA

View Full Version : QCombobox as QTableView delegate



martonmiklos
12th June 2010, 10:38
Hello all,

I would like to display a QComboBox in my QTableView.
I have tried to use the QItemDelegate, but it only shows it when I editing the cell.
Now I am using the QStyledItemDelegate in the following way:


void FuseDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->save();
switch(index.model()->data(index, Qt::UserRole).toInt()) {
case FuseBitField_boolean: {
QCheckBox *editor = new QCheckBox();
editor->setGeometry(option.rect);
editor->render(painter);
} break;
case FuseBitField_enum: {
QComboBox *editor = new QComboBox();
editor->setGeometry(option.rect);
editor->render(painter);
} break;
default:
return QStyledItemDelegate::paint(painter, option, index);
}
painter->restore();
}


But it does not displays anything. Editing works fine.

Is it possible to show a a widget with a delegate in non editing mode, or I should base my design on QTableWidget, and use addCellWidget()?

Tavit
12th June 2010, 14:26
Dear martonmiklos ,
I think you should change the base Design(QTableWidget), that time only you will get that combobox as a cell widget.
So what ever you think that is right so please go head with that..

Thanks&Regards,
Tavit.

wysota
12th June 2010, 14:39
You can use setIndexWidget() but be aware of two things:
1. the widgets will not be tied to your model in any way - they will just occupy space in the table view and you have to interconnect them with your model by yourself
2. if you add to many such widgets, everything will become slow as hell.

An alternative is to have a delegate that will use QStyle API to render the looks of a combobox in non-edit mode and will create a real combobox only when a particular cell is edited. It's more work but a much better result.

martonmiklos
13th June 2010, 14:56
Hello,

Thank you for yout ideas.
@Tavit
I would like to solve my problem using a Model View solution, because using QTableWidget would case a lot of other programming problems, so it would be tha last way what I would choose.

@wysota
I will check it.
Now I have done:


ui->tableViewFuses->openPersistentEditor(fuseModel->index(i, 1));

and now all of my comboboxes and checkboxes are visible, but under the checkboxes the value of the binary field is visible too:
4786

wysota
13th June 2010, 15:00
Make your editor fill its background to avoid this effect.

martonmiklos
13th June 2010, 15:28
I have set autoFillBackground on my tableview, btu still the same issue.
I have overridden the following member of the QItemDelegate:
createEditor. setEditorData, setModelData, updateEditorGeometry
In which function should I fill the background?
Or should I reimplement the painte member too?

wysota
13th June 2010, 15:33
I have set autoFillBackground on my tableview, btu still the same issue.
The editor, not the view.

martonmiklos
13th June 2010, 16:23
Thank you very much it works fine.

So summarize my problem, and my solution:

If you need to display a custom QWidget in a view do the following:

Make your own subclass as it is written in the Spinbox Delegate Example.
Set autoFillBackground(true) to your editor widget in the createEditor method.
Set openPersistentEditor on your view's preferred rows/columns.
And voila your view will display always your preferred widget.