Regarding QItemDelegate for combobox
-
Hi
I am using QTableView & it has 3 columns.
In the 3rd column I wanted to set ComboBox in alternate cells.Below is my CreateEditor function
@
QWidget *ComboBoxDelegate::createEditor(QWidget parent,const QStyleOptionViewItem &/ option */,const QModelIndex & index ) const
{
QComboBox *editor = new QComboBox(parent);
editor->setEditable(true);
editor->addItem("1");
return editor;
}
@
But this above function will create combobox in all the cells in which user double clicks how to restrict for alternate rows.And also is it possible to show combobox on single click instead off user double clicking in each ce
Regards
Indrajeet -
Hi,
In order to allow single click you can try
@ui->tableView->setEditTriggers(QAbstractItemView::AllEditTriggers);@
and for alternate rows in the createEditor you can try
@QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(!(index.row()%2==0))
{
QComboBox editor = new QComboBox(parent);
editor->setEditable(true);
editor->addItem("1");
editor->installEventFilter(const_cast<ComboBoxDelegate>(this));
return editor;
}
return 0;
}@
This works but i am not sure whether it should return 0 or something else. -
Hi Sam
Thanks For your reply.
Actually i have the same problem of return value.
I tried with return 0;
I have set all cells in 3rd column as editable but if I return 0 from createEditor() combobox is not appearing in that cell but that cell is also not able to edit so how to make it editable again.
-
Hi
I have created my Item cells for 3rd column in Table View as below
@
QStandardItem *temp3 = new QStandardItem("");
obj->tablemodel->setItem( i, 2 ,temp3 );
temp3->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled |Qt::ItemIsEditable);
@I have assigned ItemDelegate to my TableView Column As below
@
ComboBoxDelegate* mycombodelegate = new ComboBoxDelegate(this);
ui.tableView->setItemDelegateForColumn(2,mycombodelegate);
@My create Editor Function is as I have posted in my 1st post of this thread.
So Now If I return 0 from createEditor() the other cells in 3rd column which I have created as editable are no more editable why is it so?
-
Hi Sam
Thanks for send the code.
But what i meant to say is as we are creating the combobox in alternate cells the cells in which combobox is not there those should be user editable right means user can enter any value in those cells but those cells are becoming non editable why?
-
Here is another approach.
@QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(!(index.row()%2==0))
{
QComboBox editor = new QComboBox(parent);
editor->setEditable(true);
editor->installEventFilter(const_cast<ComboBoxDelegate>(this));
return editor;
}
else
{
QLineEdit editor = new QLineEdit(parent);
editor->installEventFilter(const_cast<ComboBoxDelegate>(this));
return editor;
}}
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if(!(index.row()%2==0))
{
QComboBox combo = static_cast<QComboBox>(editor);
combo->addItems(Dialog::getCity());
// int indx = Dialog::getCity().indexOf(index.data(Qt::DisplayRole).toString());
combo->setCurrentIndex(indx);
}else { QLineEdit *lineEdit = static_cast<QLineEdit*>(editor); //int indx = Dialog::getCity().indexOf(index.data(Qt::DisplayRole).toString()); //lineEdit->setText(Dialog::getCity().at(indx)); lineEdit->setText("What ever text you want to display"); }
}
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if(!(index.row()%2==0))
{
QComboBox combo = static_cast<QComboBox>(editor);
model->setData(index,combo->currentText());
}
else
{
QLineEdit lineEdit = static_cast<QLineEdit>(editor);
model->setData(index,lineEdit->text());
}}
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}@ -
The correct solution would be to return the base class' editor in case you do not return your own:
@
QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(!(index.row()%2==0))
{
QComboBox editor = new QComboBox(parent);
editor->setEditable(true);
editor->installEventFilter(const_cast<ComboBoxDelegate>(this));
return editor;
} else {
return QStyledItemDelegate::createEditor(parent, option, index);
}
}
@And the same in the other methods too, of course. The standard implementation takes care of returning the correct widget in resepect to the type of the data, e.g. a QDateEdit for a QDate value, etc.
-
Hi Volker
Thanks for the reply.
I did the same you suggested and it is working fine.
But the proble which i am facing is when I return below mentioned line
from createEditor(),setEditorData(),setModelData()@
return QStyledItemDelegate::createEditor(parent, option, index);
@If i have some text already present in my QTableView cell and if user clicks in that cell the text which is there is disappearing and that cell is becoming editable which it should be but i also want to have that text for user to show how to do this?