Display numbers in a model as strings in table view
-
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 the code seems to work, but probably I don't know how to use it.
I've added the last line in my code but nothin appears in the tableview.Any ideas?
-
@Dimis said in Display numbers in a model as strings in table view:
nothin appears in the tableview
You don't see anything anymore? If you remove the last line everything is displayed?
@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(); }
-
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(); }