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. [kind of solved] subclassed QAbstractTableModel::setData does not get called
Qt 6.11 is out! See what's new in the release blog

[kind of solved] subclassed QAbstractTableModel::setData does not get called

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 5 Posters 3.0k Views 2 Watching
  • 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.
  • JonBJ JonB

    @mrjj
    Since @andi456 states:

    The next step would be to make some columns editable, which does seem to work as well.
    But trying to enter data in a editable column does not result in a call for the reimplemented setData method.

    I assume that means the cell is editable, else the OP would surely have said so....

    A Offline
    A Offline
    andi456
    wrote on last edited by
    #5

    @JonB

    Yes the cell is editable, but after entering some data from the keyboard and pressing enter, the original data shows up again, without setData being called, from what I can tell.

    Btw., sorry for the somehow convoluted output, but I'll try to correct it in a minute.

    The declarations of the model look like this:```
    #ifndef ZAUBERTABLEMODEL_H
    #define ZAUBERTABLEMODEL_H

    #include <QAbstractTableModel>
    #include <nlohmann/json.hpp>
    #include <zauberdata.h>

    class ZauberTableModel : public QAbstractTableModel {

     Q_OBJECT
     public:
         ZauberTableModel(int tabnummer, nlohmann::json j_zauberliste, QObject *parent);
         int rowCount(const QModelIndex &parent = QModelIndex()) const  Q_DECL_OVERRIDE;
         int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
         QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const  Q_DECL_OVERRIDE;
         QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
         int get_tabnum() const;
         bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE; //override;
         Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;//override;
     private:
         int get_spalte_from_header(QString headername) const;
     int zeilenjustierung(int zidx) const;
         ZauberData *Zauberei;
         nlohmann::json j_zauber;
     int tnum;
    

    };
    #endif

    mrjjM 1 Reply Last reply
    0
    • A andi456

      @JonB

      Yes the cell is editable, but after entering some data from the keyboard and pressing enter, the original data shows up again, without setData being called, from what I can tell.

      Btw., sorry for the somehow convoluted output, but I'll try to correct it in a minute.

      The declarations of the model look like this:```
      #ifndef ZAUBERTABLEMODEL_H
      #define ZAUBERTABLEMODEL_H

      #include <QAbstractTableModel>
      #include <nlohmann/json.hpp>
      #include <zauberdata.h>

      class ZauberTableModel : public QAbstractTableModel {

       Q_OBJECT
       public:
           ZauberTableModel(int tabnummer, nlohmann::json j_zauberliste, QObject *parent);
           int rowCount(const QModelIndex &parent = QModelIndex()) const  Q_DECL_OVERRIDE;
           int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
           QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const  Q_DECL_OVERRIDE;
           QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
           int get_tabnum() const;
           bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE; //override;
           Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;//override;
       private:
           int get_spalte_from_header(QString headername) const;
       int zeilenjustierung(int zidx) const;
           ZauberData *Zauberei;
           nlohmann::json j_zauber;
       int tnum;
      

      };
      #endif

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @andi456
      Can you show the flag function ?
      (just to be sure)

      Nothing pops up reading the code.

      you could try run it through
      https://doc.qt.io/qt-5/qabstractitemmodeltester.html

      A 1 Reply Last reply
      0
      • mrjjM mrjj

        @andi456
        Can you show the flag function ?
        (just to be sure)

        Nothing pops up reading the code.

        you could try run it through
        https://doc.qt.io/qt-5/qabstractitemmodeltester.html

        A Offline
        A Offline
        andi456
        wrote on last edited by andi456
        #7

        @mrjj Of course:

        Qt::ItemFlags ZauberTableModel::flags(const QModelIndex &index) const {
             int editspalte = ZauberTableModel::get_spalte_from_header("Beschreibung"); 
             int spalte = index.column();
         Qt::ItemFlags f;// = ZauberTableModel::flags(index);;
         std::cout << "Editierbar " << editspalte << std::endl;
         if (spalte == editspalte) {
             f =  Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
         } else { 
             f = Qt::ItemIsEnabled;
         }    
             return f;
        }
        

        So I hope I've boiled down my code to the more relevant parts in my post above.

        There's one thing, though, that I forgot to mention. I had to include the cpp file of my subclassed model in order to avoid a linker error after compiling, which is not needed when compiling other examples...

        mrjjM 1 Reply Last reply
        0
        • A andi456

          @mrjj Of course:

          Qt::ItemFlags ZauberTableModel::flags(const QModelIndex &index) const {
               int editspalte = ZauberTableModel::get_spalte_from_header("Beschreibung"); 
               int spalte = index.column();
           Qt::ItemFlags f;// = ZauberTableModel::flags(index);;
           std::cout << "Editierbar " << editspalte << std::endl;
           if (spalte == editspalte) {
               f =  Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
           } else { 
               f = Qt::ItemIsEnabled;
           }    
               return f;
          }
          

          So I hope I've boiled down my code to the more relevant parts in my post above.

          There's one thing, though, that I forgot to mention. I had to include the cpp file of my subclassed model in order to avoid a linker error after compiling, which is not needed when compiling other examples...

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #8

          Hi
          it does look fine.

          did you check that
          f = Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
          is indeed used for the cells you try to edit ?

          A 1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            it does look fine.

            did you check that
            f = Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
            is indeed used for the cells you try to edit ?

            A Offline
            A Offline
            andi456
            wrote on last edited by andi456
            #9

            @mrjj Yes, I can double click on the cells and edit them, while double clicking on the others does not work.

            I've also tried the test class, but end up with a "undefined reference" error like this:
            /usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: tst_tester.o: in function ZauberTableModel::ZauberTableModel(int, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >, QObject*)': /home/andi/Projekte/qt/build-qtesttest-Desktop-Debug/../qtesttest/zaubertablemodel.cpp:6: undefined reference to vtable for ZauberTableModel'

            But I'm not sure, if I have setup the test correctly. As I mentioned this also happens, when i just use "#include zaubertablemodel.h" in ui_mainwindow.h. Although I've added the source files to the test project, the above error persists. Somehow the Constructor seems to be undefined.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andi456
              wrote on last edited by andi456
              #10

              It seems, as if QAbstractTableModel subclasses do not like nlohmann::json parameters to be passed to their constructor. I've started another project with basically the same subclass and it shows the same error of an undefined reference to vtable. I guess the setData method would not get called, even if i found a way to get it to compile.

              Could this be an issue of the nlohmann::json library? Or what could one do to further investigate the issue?

              JonBJ 1 Reply Last reply
              0
              • A andi456

                It seems, as if QAbstractTableModel subclasses do not like nlohmann::json parameters to be passed to their constructor. I've started another project with basically the same subclass and it shows the same error of an undefined reference to vtable. I guess the setData method would not get called, even if i found a way to get it to compile.

                Could this be an issue of the nlohmann::json library? Or what could one do to further investigate the issue?

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #11

                @andi456 said in subclassed QAbstractTableModel::setData does not get called:

                as if QAbstractTableModel subclasses do not like nlohmann::json parameters to be passed to their constructor.

                Don't know what you mean. QAbstractTableModel doesn't know anything about JSON. What you do with nlohmann::json parameters is up to you.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andi456
                  wrote on last edited by andi456
                  #12

                  Well, that's why I tried to pass a nlohmann::json argument to the constructor, which results in the obscure "undefined reference to vtable" error for the QAbstractTableModel subclass. After some research on the internet this error can have all kinds of causes. But I do not see any obvious error in my code.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    What build system do you use? Is the moc file for zaubertablemodel.h generated, compiled and linked?

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      Hi,

                      Might be a silly question but do you build the class code along with your test ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      A 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Hi,

                        Might be a silly question but do you build the class code along with your test ?

                        A Offline
                        A Offline
                        andi456
                        wrote on last edited by
                        #15

                        @SGaist No, I've written both a minimal test project and a test. The errors were similar. After running qmake on the minimal testproject, new errors occurred:
                        :-1: Fehler: moc_ztablemodel.o:(.data.rel.ro._ZTV11ZTableModel[_ZTV11ZTableModel]+0xb0): undefined reference to `ZTableModel::headerData(int, Qt::Orientation, int) const'

                        And so on for all the reimplemented methods of QAbstractTableModel.

                        The moc_* files are there obviously but cannot be linked somehow?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

                          Missing symbols suggests that you might be:

                          • not building the class sources
                          • not implementing the methods

                          One thing you can is start from an empty class and the gradually add the methods to see where it breaks

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andi456
                            wrote on last edited by andi456
                            #17

                            Don't i have to implement at least

                            code_text
                            QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const  Q_DECL_OVERRIDE;
                            int rowCount(const QModelIndex &parent = QModelIndex()) const  Q_DECL_OVERRIDE;
                            int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;```
                            code_text
                            

                            ? That's what I did in my minimal test class .

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #18

                              @andi456 said in subclassed QAbstractTableModel::setData does not get called:

                              That's what I did in my minimal test class .

                              But you declared headerData() without providing an implementation.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              A 1 Reply Last reply
                              1
                              • Christian EhrlicherC Christian Ehrlicher

                                @andi456 said in subclassed QAbstractTableModel::setData does not get called:

                                That's what I did in my minimal test class .

                                But you declared headerData() without providing an implementation.

                                A Offline
                                A Offline
                                andi456
                                wrote on last edited by
                                #19

                                @Christian-Ehrlicher The relevant parts of the implementation can be found in my first post in the second code block. I just copied the necessary parts to my minimal test example, created with qtcreator.

                                Christian EhrlicherC JonBJ 2 Replies Last reply
                                0
                                • A andi456

                                  @Christian-Ehrlicher The relevant parts of the implementation can be found in my first post in the second code block. I just copied the necessary parts to my minimal test example, created with qtcreator.

                                  Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #20

                                  @andi456 but even there I see no headerData() implementation...

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  1 Reply Last reply
                                  1
                                  • A Offline
                                    A Offline
                                    andi456
                                    wrote on last edited by
                                    #21

                                    Here it is:

                                    QVariant ZTableModel::headerData(int section, Qt::Orientation orientation, int role) const
                                    {
                                    QList<QString> headerliste = ZTableModel::get_header(j_zl);
                                        if (role != Qt::DisplayRole) {
                                        return QVariant();
                                    }
                                    if (orientation == Qt::Horizontal) {
                                        for (int i=0; i < headerliste.size(); ++i) {
                                            QString s = headerliste.at(i);
                                        if (i == section) {
                                            return s;
                                            break;
                                        }
                                        }
                                    }
                                    
                                    JonBJ 1 Reply Last reply
                                    0
                                    • A andi456

                                      @Christian-Ehrlicher The relevant parts of the implementation can be found in my first post in the second code block. I just copied the necessary parts to my minimal test example, created with qtcreator.

                                      JonBJ Online
                                      JonBJ Online
                                      JonB
                                      wrote on last edited by
                                      #22

                                      @andi456
                                      @Christian-Ehrlicher said you had not implemented headerData().
                                      The error messages you pasted includes

                                      undefined reference to ZTableModel::headerData(int, Qt::Orientation, int) const

                                      Do you not think this indicates a problem?

                                      1 Reply Last reply
                                      0
                                      • A andi456

                                        Here it is:

                                        QVariant ZTableModel::headerData(int section, Qt::Orientation orientation, int role) const
                                        {
                                        QList<QString> headerliste = ZTableModel::get_header(j_zl);
                                            if (role != Qt::DisplayRole) {
                                            return QVariant();
                                        }
                                        if (orientation == Qt::Horizontal) {
                                            for (int i=0; i < headerliste.size(); ++i) {
                                                QString s = headerliste.at(i);
                                            if (i == section) {
                                                return s;
                                                break;
                                            }
                                            }
                                        }
                                        
                                        JonBJ Online
                                        JonBJ Online
                                        JonB
                                        wrote on last edited by
                                        #23

                                        @andi456 said in subclassed QAbstractTableModel::setData does not get called:

                                        Here it is:

                                        Your post crossed with mine.

                                        Could you please delete all object files etc. and do a clean rebuild from scratch. If you still get an error could you copy & paste that/those.

                                        1 Reply Last reply
                                        0
                                        • A Offline
                                          A Offline
                                          andi456
                                          wrote on last edited by andi456
                                          #24

                                          I see, got the minimal example to compile. It was a matter of the header declarations that i forgot to comment out.. Thank you.. I'll see what happens after the implementation of setData and flags and report back later.

                                          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