Display numbers in a model as strings in table view
-
Hi all,
I'm new to Qt and I need your help in the task I'm trying to do.
I have a table model with numbers which is set and displayed in a table view.
Is there any way to display each number as a different string (word) depending on the number? I would like to keep the data stored in the model as numbers/integers.If the information I gave is not sufficient please let me know, as I'm not sure I'm explaining my case well enough.
Thank you in advance.
-
#ifndef WORDIFIER_H #define WORDIFIER_H #include <QStyledItemDelegate> #include <QVariant> class Wordifier : public QStyledItemDelegate{ Q_OBJECT Q_DISABLE_COPY(Wordifier) public: Wordifier(QObject* parent = Q_NULLPTR) : QStyledItemDelegate(parent){} QString displayText(const QVariant &value, const QLocale &locale) const Q_DECL_OVERRIDE{ if(value.canConvert(QMetaType::Int)){ switch(value.toInt()){ case 1: return QStringLiteral("One"); case 2: return QStringLiteral("Two"); case 3: return QStringLiteral("Three"); } } return QStyledItemDelegate::displayText(value,locale); }; #endif
then in your call
tableView->setItemDelegate(new Wordifier(this));
-
Thank you VRonin for your reply.
As I mentioned I'm new to to Qt and C++. So my apologies if my question is silly.
The code you gave me should I input it in my header file and then create an instance of this class in my .cpp file? Also I guess I need to include the QStleItemDelegate class in the header file, isn't it? -
Many thanks again for this.
An error appears in the line " QString displayText(const QVariant &value, const QLocale &locale) const Q_DECL_OVERRIDE
" saying "expected {".
I tried to figure out where this is missing, but I couldn't. Any ideas?EDIT:
I think I fixed it. I added a {} in the line before. Now no error appears.
I'll try to see what's happening and how this works and get back with any questions.Many thanks again for your time.
-
@VRonin Sorry my bad, when I include the last line, these errors return:
mainwindow.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Wordifier::metaObject(void)const " (?metaObject@Wordifier@@UEBAPEBUQMetaObject@@XZ)
mainwindow.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __cdecl Wordifier::qt_metacast(char const *)" (?qt_metacast@Wordifier@@UEAAPEAXPEBD@Z)
mainwindow.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __cdecl Wordifier::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Wordifier@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
debug\tool1_1.exe:-1: error: LNK1120: 3 unresolved externals
-
I tested it on this minimal example and it works:
#include <QApplication> #include <QTableView> #include <QStandardItemModel> #include <QStyledItemDelegate> #include <QVariant> class Wordifier : public QStyledItemDelegate{ Q_DISABLE_COPY(Wordifier) public: Wordifier(QObject* parent = Q_NULLPTR) : QStyledItemDelegate(parent){} QString displayText(const QVariant &value, const QLocale &locale) const Q_DECL_OVERRIDE{ if(value.canConvert(QMetaType::Int)){ switch(value.toInt()){ case 1: return QStringLiteral("One"); case 2: return QStringLiteral("Two"); case 3: return QStringLiteral("Three"); default: break; } } return QStyledItemDelegate::displayText(value,locale); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QStandardItemModel model; model.insertColumn(0); model.insertRows(0,3); model.setData(model.index(0,0),1); model.setData(model.index(1,0),2); model.setData(model.index(2,0),QStringLiteral("Test")); QTableView tableView; tableView.setItemDelegate(new Wordifier(&tableView)); tableView.setModel(&model); tableView.show(); return a.exec(); }