Qtableview delegate
-
Hi,
I'm using Qtableview (QStandardItemModel ) and i used delegate in columns so only double type data is used, so its working but one issue is when i'm try to del data from my columns which i used delegate then its not deleting i can change the data why not delete happening ?
shared the code--#include< QStyledItemDelegate> class IntDelegate : public QStyledItemDelegate { public: IntDelegate(QObject* parent = 0); QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const; void setEditorData(QWidget* editor, const QModelIndex& index) const; void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const; private: }; //cpp file #include "IntDelegate.h" #include <QDoubleValidator> IntDelegate::IntDelegate(QObject* parent) :QStyledItemDelegate(parent) { } QWidget* IntDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { QLineEdit* editor = new QLineEdit(parent); editor->setValidator(new QDoubleValidator); return editor; } void IntDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { double value = index.model()->data(index, Qt::EditRole).toDouble(); QLineEdit* line = static_cast<QLineEdit*>(editor); line->setText(QString().setNum(value)); } void IntDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { QLineEdit* line = static_cast<QLineEdit*>(editor); QString value = line->text(); model->setData(index, value); } void IntDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { editor->setGeometry(option.rect); } //use IntDelegate* intdelegate; intdelegate = new IntDelegate(); ui.tableView->setItemDelegateForColumn(5, intdelegate); ui.tableView->setItemDelegateForColumn(4, intdelegate); ui.tableView->setItemDelegateForColumn(8, intdelegate);
Any change req. in delegate file ? or I done something wrong ?
-
Hi,
I'm using Qtableview (QStandardItemModel ) and i used delegate in columns so only double type data is used, so its working but one issue is when i'm try to del data from my columns which i used delegate then its not deleting i can change the data why not delete happening ?
shared the code--#include< QStyledItemDelegate> class IntDelegate : public QStyledItemDelegate { public: IntDelegate(QObject* parent = 0); QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const; void setEditorData(QWidget* editor, const QModelIndex& index) const; void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const; private: }; //cpp file #include "IntDelegate.h" #include <QDoubleValidator> IntDelegate::IntDelegate(QObject* parent) :QStyledItemDelegate(parent) { } QWidget* IntDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { QLineEdit* editor = new QLineEdit(parent); editor->setValidator(new QDoubleValidator); return editor; } void IntDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { double value = index.model()->data(index, Qt::EditRole).toDouble(); QLineEdit* line = static_cast<QLineEdit*>(editor); line->setText(QString().setNum(value)); } void IntDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { QLineEdit* line = static_cast<QLineEdit*>(editor); QString value = line->text(); model->setData(index, value); } void IntDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { editor->setGeometry(option.rect); } //use IntDelegate* intdelegate; intdelegate = new IntDelegate(); ui.tableView->setItemDelegateForColumn(5, intdelegate); ui.tableView->setItemDelegateForColumn(4, intdelegate); ui.tableView->setItemDelegateForColumn(8, intdelegate);
Any change req. in delegate file ? or I done something wrong ?
Because an empty string is not a vaid double.
Change
editor->setValidator(new QDoubleValidator);
toconst QRegularExpression doubleExpression(QStringLiteral(R"***(^(?:[-+]?\d*%1?\d+(?:%2[-+]?\d+)?)?$)***") .arg(QString(QRegularExpression::escape(editor->locale().decimalPoint())),QString(QRegularExpression::escape(editor->locale().exponential()))),QRegularExpression::CaseInsensitiveOption); editor->setValidator(new QRegularExpressionValidator(doubleExpression,editor));
Few other fixes:
- in
setEditorData
,line->setText(QString().setNum(value));
should becomeline->setText(line->locale().toString(value));
to work on languages that use different decimal separators from the USA - in
setModelData
,QString value = line->text(); model->setData(index, value);
should becomemodel->setData(index, line->locale().toDouble(line->text()));
to store the numbers as numbers and not strings
- in
-
Thanku , is any other chnage in code you shared bcz i'm getting error
QWidget* IntDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { QLineEdit* editor = new QLineEdit(parent); const QRegularExpression doubleExpression(QStringLiteral(R"***(^(?:[-+]?\d*%1?\d+(?:%2[-+]?\d+)?)?$)***") .arg(QString(QRegularExpression::escape(locale().decimalPoint())), QS tring(QRegularExpression::escape(locale().exponential()))), QRegularExpression::CaseInsensitiveOption); editor->setValidator(new QRegularExpressionValidator(doubleExpression, editor)); return editor; //here getting error identifier locale is undefined } void IntDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { double value = index.model()->data(index, Qt::EditRole).toDouble(); QLineEdit* line = static_cast<QLineEdit*>(editor); line->setText(line->locale().toString(value)); } void IntDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { QLineEdit* line = static_cast<QLineEdit*>(editor); QString value = line->text(); model->setData(line->locale().toDouble(line->text()), value); //error on line no suitable constructor exists to convert from "double" to "QModelIndex" }
-
Because an empty string is not a vaid double.
Change
editor->setValidator(new QDoubleValidator);
toconst QRegularExpression doubleExpression(QStringLiteral(R"***(^(?:[-+]?\d*%1?\d+(?:%2[-+]?\d+)?)?$)***") .arg(QString(QRegularExpression::escape(editor->locale().decimalPoint())),QString(QRegularExpression::escape(editor->locale().exponential()))),QRegularExpression::CaseInsensitiveOption); editor->setValidator(new QRegularExpressionValidator(doubleExpression,editor));
Few other fixes:
- in
setEditorData
,line->setText(QString().setNum(value));
should becomeline->setText(line->locale().toString(value));
to work on languages that use different decimal separators from the USA - in
setModelData
,QString value = line->text(); model->setData(index, value);
should becomemodel->setData(index, line->locale().toDouble(line->text()));
to store the numbers as numbers and not strings
- in