Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Display numbers in a model as strings in table view
QtWS25 Last Chance

Display numbers in a model as strings in table view

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Dimis

    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?

    VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #4

    Correct, I also added the include guards and includes to the code above. Put everything in a header file, #include it in the .cpp where you are creating the table view and call that last line

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dimis
      wrote on last edited by Dimis
      #5

      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.

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #6

        It's actually the line above the problem (i forgot {} in the constructor). Fixed now

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        D 1 Reply Last reply
        1
        • VRoninV VRonin

          It's actually the line above the problem (i forgot {} in the constructor). Fixed now

          D Offline
          D Offline
          Dimis
          wrote on last edited by
          #7

          @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?

          VRoninV 1 Reply Last reply
          0
          • D Dimis

            @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?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #8

            @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?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            D 1 Reply Last reply
            0
            • VRoninV VRonin

              @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?

              D Offline
              D Offline
              Dimis
              wrote on last edited by
              #9

              @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

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #10

                re-run qmake

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                D 1 Reply Last reply
                0
                • VRoninV VRonin

                  re-run qmake

                  D Offline
                  D Offline
                  Dimis
                  wrote on last edited by
                  #11

                  @VRonin I did it and no error returns but nothing appears in the table view.
                  Maybe I'm missing something?

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #12

                    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();
                    }
                    

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    D 1 Reply Last reply
                    1
                    • VRoninV VRonin

                      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();
                      }
                      
                      D Offline
                      D Offline
                      Dimis
                      wrote on last edited by
                      #13

                      @VRonin This works for me as well. I'll figure it out.
                      Many thanks again for your kind contribution and time.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved