[solved] openPersistentEditor(index) not working
-
Dear friends,
Can you help me please?
I'm trying to apply QSpinbox delegate to my application.
I tried to imitate Spin Box Delegate Example in Qt creator and "QItemDelegate tutorial video":http://www.youtube.com/watch?v=EJf-vZ6FQfc.
I successfully created the delegates. But openPersistentEditor() method not working. Which use to display the data already in edit mode.
What is missing?
Here my codes.
@ QStandardItemModel *item_model;
ComponentDelegate *component_delegate;item_model = new QStandardItemModel(4,2,this); component_delegate = new ComponentDelegate(this); for (int row = 0; row < 4; ++row) { for (int col = 0; col < 2; ++col) { QModelIndex index = item_model->index(row,col,QModelIndex()); ui->height_amperate_tableview->openPersistentEditor(index); item_model->setData(index,0); } } ui->height_amperate_tableview->horizontalHeader()->setStretchLastSection(true); ui->height_amperate_tableview->setModel(item_model); ui->height_amperate_tableview->setItemDelegate(component_delegate);@
@class ComponentDelegate:public QItemDelegate
{
public:
ComponentDelegate(QObject *parent): QItemDelegate(parent){}QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QSpinBox *editor=new QSpinBox(parent); editor->setMinimum(0); editor->setMaximum(100); return editor; } void setEditorData(QWidget *editor, const QModelIndex &index) const { int value = index.model()->data(index,Qt::EditRole).toInt(); QSpinBox *spinbox = static_cast<QSpinBox*>(editor); spinbox->setValue(value); } void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QSpinBox *spinbox = static_cast<QSpinBox*>(editor); spinbox->interpretText(); int value = spinbox->value(); model->setData(index,value,Qt::EditRole); } void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); }
};@
Thanks in advance.
-
Hi,
One thing from your code that look strange is that you are opening the persistent editor before setting your delegate.
-
Thank you for replying me.
As you mentioned I set the delegate before opening the persistent editor.
@
QStandardItemModel *item_model;
ComponentDelegate *component_delegate;item_model = new QStandardItemModel(4,2,this);
component_delegate = new ComponentDelegate(this);ui->height_amperate_tableview->horizontalHeader()->setStretchLastSection(true);
ui->height_amperate_tableview->setModel(item_model);
ui->height_amperate_tableview->setItemDelegate(component_delegate);
for (int row = 0; row < 4; ++row) {
for (int col = 0; col < 2; ++col) {
QModelIndex index = item_model->index(row,col,QModelIndex());
ui->height_amperate_tableview->openPersistentEditor(index);
item_model->setData(index,0);
}
}@
It is working Now.
Thank you very much for your help.
-
You're welcome !
Since your view is working please update the thread title prepending [solved] so other forum users may know a solution has been found :)
-
just forgot. :)
thanks.