How to Add unit with data in columns ?
-
Hi,
1->How to add unit or text with data in tableview column ?
QAbstractItemModel* table1 = ui.tableView->model();
table1->setData(table1->index(0, 1), speed + QString("rpm"));
as i try this then rpm is coming but speed value come as some special character.
2-> One column is user editable so when user enter any value then unit should add in with numeric value.
3->as 2 same i need to do for a Qlineedit when user enter value unit should add.
for qlineedit i tried setinputmask()
ui.lineEdit_4->setInputMask("0000 unit"); // this is only way for lineedit
How to do this? -
How to add unit or text with data in tableview column ?
Subclass
QStyledItemDelegateand reimplementdisplayText. Something like:QString displayText(const QVariant &value, const QLocale &locale) const override{ return QStyledItemDelegate::displayText(value,locale) + QStringLiteral(" rpm"); }same i need to do for a Qlineedit
Don't use lineedit, use
QSpinBox/QDoubleSpinBox(you can callspinBox->setButtonSymbols(QAbstractSpinBox::NoButtons)if you don't want the buttons). They already have support for prefix and suffix -
How to add unit or text with data in tableview column ?
Subclass
QStyledItemDelegateand reimplementdisplayText. Something like:QString displayText(const QVariant &value, const QLocale &locale) const override{ return QStyledItemDelegate::displayText(value,locale) + QStringLiteral(" rpm"); }same i need to do for a Qlineedit
Don't use lineedit, use
QSpinBox/QDoubleSpinBox(you can callspinBox->setButtonSymbols(QAbstractSpinBox::NoButtons)if you don't want the buttons). They already have support for prefix and suffix -
you can make the suffix a property of your delegate:
class SuffixDelegate : public QStyledItemDelegate{ Q_OBJECT Q_DISABLE_COPY(SuffixDelegate) public: explicit SuffixDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent){} QString displayText(const QVariant &value, const QLocale &locale) const override{ return QStyledItemDelegate::displayText(value,locale) + m_suffix; } void setSuffix(const QString& val) { m_suffix = val; } const QString& suffix() const {return m_suffix;} private: QString m_suffix; };Now you can:
auto rpmDelegate = new SuffixDelegate(this); rpmDelegate->setSuffix(QStringLiteral(" rpm")); auto hpDelegate = new SuffixDelegate(this); hpDelegate->setSuffix(QStringLiteral(" hp")); view->setItemDelegateForColumn(9,rpmDelegate); view->setItemDelegateForColumn(8,hpDelegate ); -
Thanks its working!!
But i have dependency
1->Table1 have 11 column in that i used integerdelegate for 6 column including the "column 8' in this column i need to set suffix also,
2->and 4 column are non editable using noneditabledelegate, i need to set suffix for column 9 and make as non editable also.
How can i do this ? -
Hi,
It's not the role of the delegate to decide what is editable or not, the model flag function is there for that reason.
What does your "noneditabledelegate" do ?
If you need several different delegates to support suffix, then derive them from the original one providing support for suffix.
-
Hi,
It's not the role of the delegate to decide what is editable or not, the model flag function is there for that reason.
What does your "noneditabledelegate" do ?
If you need several different delegates to support suffix, then derive them from the original one providing support for suffix.
-
@n-2204 said in How to Add unit with data in columns ?:
What does your "noneditabledelegate" do ?
to make columns non editable
As already said, that's not the role of the delegate but the model flags method.
-
@n-2204 said in How to Add unit with data in columns ?:
What does your "noneditabledelegate" do ?
to make columns non editable
As already said, that's not the role of the delegate but the model flags method.
-
I have two Qtableview, in both tables need to make some columns as non editable so for both table I need to create a separate class to set model flags ?
@n-2204
Setting flags is done on the model, not the view. Delegates are set on the view, not the model.If you prefer you can have your own separate sub-classes for either/both the models and the views. In that case you would indeed need separate classes for model flags/item delegates. (In the case of delegates you can also have separate sub-classes for the view but both using the same delegate class, if desired.)
Or, you can share the same model and/or view sub-class for both your tables, instead of having different dedicated ones. You would put into them some member field of your own to indicates which model/view each instance represents. Then you can use that in your flags or delegate methods to decide e.g. which columns to edit or hide based on which model/view your instance represents.