Hi community,
in my code I have added a view derived from QTableView having 2 columns and x rows ( x is not fixed but grows ).
The first column contains text and there I show the content of the model that stores internally a string of strings.
The second columns have show a QComboBox where the user should be able to choose one of the combobox options (also a QString). I am using a class derived from QStyledItemDelegate for that purpose.
I have no problems with the first column, I can correctly add new rows with the right text.
My problem is with the column #2, I can show the combobox with the options but, after choosing one, if I click anywhere in the view, the combobox value clears.
So seems I am doing something wrong in storing and read the corect combobox value.
Here the code I used to create the item delegate
*.h
#ifndef SELECTEDLISTITEMDELEGATE_H
#define SELECTEDLISTITEMDELEGATE_H
#include <QStyledItemDelegate>
class SelectedListItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
SelectedListItemDelegate
(QObject *parent
= nullptr
);
~SelectedListItemDelegate() override;
};
#endif
#ifndef SELECTEDLISTITEMDELEGATE_H
#define SELECTEDLISTITEMDELEGATE_H
#include <QStyledItemDelegate>
class SelectedListItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
SelectedListItemDelegate(QObject *parent = nullptr);
~SelectedListItemDelegate() override;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
};
#endif
To copy to clipboard, switch view to plain text mode
and the cpp
#include "SelectedListItemDelegate.h"
#include <QComboBox>
#include <QDebug>
SelectedListItemDelegate
::SelectedListItemDelegate(QObject* parent
) : QStyledItemDelegate(parent)
{
}
SelectedListItemDelegate::~SelectedListItemDelegate()
{
}
{
Q_UNUSED(option)
// I considere 3 options just as an example
const int row = index.row();
comboBox
->addItem
(QString("Option %1").
arg(row
));
comboBox
->addItem
(QString("Option %1").
arg(row
));
comboBox
->addItem
(QString("Option %1").
arg(row
));
return comboBox;
}
// This routine does not set any value in the combobox
void SelectedListItemDelegate
::setEditorData(QWidget *editor,
const QModelIndex &index
) const {
QString value
= index.
model()->data
(index, Qt
::EditRole).
toString();
qDebug() << "Value:" << value; // Value is empty
QComboBox *comboBox
= qobject_cast<QComboBox
*>
(editor
);
comboBox->setCurrentIndex(comboBox->findText(value));
}
// Seems that the combobox value is not correctly written because is not kept in the cell. If I click outside its cleared
{
QComboBox *comboBox
= qobject_cast<QComboBox
*>
(editor
);
QString value
= comboBox
->currentText
();
model->setData(index, value, Qt::EditRole);
}
#include "SelectedListItemDelegate.h"
#include <QComboBox>
#include <QDebug>
SelectedListItemDelegate::SelectedListItemDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{
}
SelectedListItemDelegate::~SelectedListItemDelegate()
{
}
QWidget *SelectedListItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option)
QComboBox *comboBox = new QComboBox(parent);
// I considere 3 options just as an example
const int row = index.row();
comboBox->addItem(QString("Option %1").arg(row));
comboBox->addItem(QString("Option %1").arg(row));
comboBox->addItem(QString("Option %1").arg(row));
return comboBox;
}
// This routine does not set any value in the combobox
void SelectedListItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();
qDebug() << "Value:" << value; // Value is empty
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
comboBox->setCurrentIndex(comboBox->findText(value));
}
// Seems that the combobox value is not correctly written because is not kept in the cell. If I click outside its cleared
void SelectedListItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
QString value = comboBox->currentText();
model->setData(index, value, Qt::EditRole);
}
To copy to clipboard, switch view to plain text mode
Can I have a help in understanding what's I am doing wrong?
Thanx
Bookmarks