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
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 5 Posters 2.7k 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.
  • A Offline
    A Offline
    andi456
    wrote on last edited by andi456
    #1

    Hi,

    trying to build a bunch of TableViews displayed in Tabs of a TabWidget from the data provided by a .json file with a library external to qt called nlohmann/json.hpp (I haven't noticed the existence of the QJson library until yesterday), I had to subclass QAbstractTableModel which successfully displays the data. 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. But I cannot figure out why.

    For my little project I mimicked the QtCreator approach with a ui_mainwindow.h file written by hand, because I wanted to be able to use lists of widget (pointers):

    #ifndef UI_MAINWINDOW_H
    #define UI_MAINWINDOW_H
    #include <iostream>
    #include <fstream>
    #include <QtCore/QVariant>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QFrame>
    #include <QtWidgets/QLabel>
    #include <QtWidgets/QMainWindow>
    #include <QtWidgets/QWidget>
    #include <QtWidgets/QTabWidget>
    #include <QtWidgets/QHBoxLayout>
    #include <QList>
    #include <QTableView>
    #include <QtWidgets/QHeaderView>
    #include <nlohmann/json.hpp>
    #include "zauberdata.h"
    #include "zaubertablemodel.h"
    #include "zaubertablemodel.cpp"
    
    QT_BEGIN_NAMESPACE
    
    class Ui_MainWindow
    {
    public:
        QWidget *centralwidget;
        QWidget *tabwidget;
        QWidget *tabwidget2;
        QLabel *TestLabel;
        QList<QHBoxLayout *> tabs_hLayout;
       // ZauberTableModel *model;
        //QTableView *tabletester;
        QTabWidget *Tabulator;
        ZauberData *Zauberei;
        nlohmann::json j_zauberei;
        QList<QWidget *> get_tablist (int tabnum) {
            QList<QWidget*> tablist; 
            for (int i=0; i < tabnum; ++i) {
    	    tablist.append(new QWidget());
    	}
    	return tablist;
        }
        QList<QTableView *> tableviews;
        QList<ZauberTableModel *> models;
    
        nlohmann::json load_zauber_json_from_file(std::string filename) {
            std::ifstream imagicstream(filename);
    	nlohmann::json j_zauberliste;
    	if (!imagicstream.is_open()) {
    		std::cout << "Zauber laden schlug fehl!";
    	} else {
    		std::cout << "should work" << std::endl;
    		imagicstream >> j_zauberliste ;
    		int len = j_zauberliste.size();
    		std::cout << "Datei erfolgreich geladen " << std::to_string(len) << "Eintraege geladen von " << filename << std::endl;
    	}
    	return j_zauberliste;
        }	
    
        void setupUi(QMainWindow *MainWindow)
        {
            j_zauberei = load_zauber_json_from_file("/home/andi/Projekte/qt/pathfinder-json/waldläuferzauber-erw.json");
    	Zauberei = new ZauberData(j_zauberei);
    	QList<int> zeilenliste = Zauberei->get_zeilen_from_json();
            MainWindow->resize(800, 600);
    	centralwidget = new QWidget(MainWindow);
    	Tabulator = new QTabWidget(centralwidget);
    	Tabulator->setGeometry(QRect(4, 4, 700, 400));
    	TestLabel = new QLabel(tabwidget);
    	QList<QWidget *> tabs = get_tablist(zeilenliste.size());
            QList<QTableView *> tableviews;
    	QList<ZauberTableModel *> models;
    	QList<QHBoxLayout *> tabs_hLayout;
    	for (int i=0; i<zeilenliste.size(); ++i) {
    	    tabs.at(i)->setObjectName(QString("TabGrad%1").arg(i));
    	    QString s = "Grad " + QString::number(i);
    	    Tabulator->addTab(tabs.at(i), s);
                tableviews.append(new QTableView(tabs.at(i)));
    	    int zeilen = zeilenliste.at(i);
    	    std::cout << "Zeilenzahl: " << zeilen << std::endl;
    	    models.append(new ZauberTableModel(i, j_zauberei, centralwidget));
    	    tabs_hLayout.append(new QHBoxLayout(tabs.at(i)));
    	}    
    	for (int i=0; i<zeilenliste.size(); ++i) {
    	    tableviews.at(i)->setModel(models.at(i));
    	    tableviews.at(i)->horizontalHeader()->setSectionsMovable(true);
                tabs_hLayout.at(i)->addWidget(tableviews.at(i));
    	}
    
    	MainWindow->setCentralWidget(centralwidget);
        }	
    };
    
    #include "zaubertablemodel.h"
    #include "zauberdata.h"
    #include <nlohmann/json.hpp>
    #include <string>
      
    
        QVariant ZauberTableModel::data(const QModelIndex &index, int role) const {
            std::cout << "data Methode" << std::endl;
            if (role == Qt::DisplayRole) {
    	int zeile, 
    	spalte;
    	int iteration = -1;
    	zeile = index.row();
    	//std::cout << " zeilen " << zeile << std::endl;
    	zeile = ZauberTableModel::zeilenjustierung(zeile);
    	//std::cout << "OK zeilen " << zeile << std::endl;
    	spalte = index.column();
    	nlohmann::json row_obj = Zauberei->get_row_obj(zeile);
    	for (auto& el :row_obj.items()) {
    	    ++iteration;
    	    std::string tn = el.value().type_name();
    	    //std::cout << "Urtyp " << el.value().type() << std::endl;
    	    int bool_comp = tn.compare("boolean");
    	    int number_comp = tn.compare("number");
    	    if ((el.value().type()==nlohmann::json::value_t::string) && (iteration==spalte)) {
    	        //std::cout << "Type " << el.value().type_name() << " Key " << el.key() << std::endl;
    		std::cout << "Schlüssel "<< el.key() << " Spalte " << iteration << "Zeile "<< zeile << std::endl;
    	        QString s = QString::fromStdString(el.value());
    	        return s;
    	        break;
    	    } else if ((bool_comp == 0) && (iteration==spalte)) {
    		std::cout << "Schlüssel "<< el.key() << " Spalte " << iteration << "Zeile "<< zeile << std::endl;
    	        bool flag = el.value();
    	//	std::cout << "Flags auch?" << std::endl;
    	        //QString s = QString::number(flag);
    		return flag;
    		break;
    	    } else if ((el.value().type()==nlohmann::json::value_t::number_integer) && (iteration==spalte)) {
    	        int counter = el.value();
    		std::cout << "Zahlen auch? " << counter << std::endl;
    	        //QString s = QString::number(counter);
    		return counter;
    		break;
    	    } else if ((number_comp == 0)  && (iteration == spalte)) {
    	 //       std::cout << "Wie jetzt?" << std::endl;
    		std::cout << "Schlüssel "<< el.key() << " Spalte " << iteration << "Zeile "<< zeile << std::endl;
    		int counter = el.value();
    		//QString s = QString::number(counter);
    	        return counter;
    		break;
                }		
    	} 
    	}
    	return QVariant();
        }	
    
        bool ZauberTableModel::setData(const QModelIndex &index, const QVariant &value, int role) 
         {
         std::cout << "Dies ist die setData Methode" << std::endl;
         if (role == Qt::EditRole) {
             int zeile,
             spalte;
             zeile = ZauberTableModel::zeilenjustierung(index.row());
             spalte = index.column();
             nlohmann::json row_obj = Zauberei->get_row_obj(zeile);
             QList<QString> headerliste = Zauberei->get_header();
             for (int i=0; i<headerliste.size(); ++i) {
    	     if (i == spalte) {
    		 QString qs = headerliste.at(i);
                     std::string spaltenname = qs.toStdString();
    	             QString s;
    		     QVariant qvtest = value;
    
    	         if ((value.canConvert<QString>()) && (qvtest.convert(QVariant::String)))
    	         {
    		     s = value.toString();
    	             std::string neuerstringwert = s.toStdString();
    	             row_obj[spaltenname] = neuerstringwert;
    	         }  
    	         else if ((value.canConvert<int>()) && (qvtest.convert(QVariant::Int))) {
    	            int neuerzahlwert = value.toInt();
    	            row_obj[spaltenname] = neuerzahlwert;
    	         }
    	         else if ((value.canConvert<bool>()) && (qvtest.convert(QVariant::Bool)))
    	         {
    		   std::cout << "Überhaupt angekommen?" << std::endl;
    	           bool neuerboolwert = value.toBool();
    		   row_obj[spaltenname] = neuerboolwert;
                     }
    	     }    
           }	 
       emit dataChanged(index, index, {role});    
       return true;
       } else {
       return false;
        }
      } 
        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;
        }	 
    
    

    The above shows the code of the subclassed QAbstractTableModel. So how could I debug this? Could it be that setData does not get called because I use a QList of models? Or should I learn to use the QJson class. (Btw. I know the code is still ugly, but I first wanted to get the basics to work before cleaning up.)

    Kind regards,

    Andreas

    JonBJ 1 Reply Last reply
    0
    • A andi456

      Hi,

      trying to build a bunch of TableViews displayed in Tabs of a TabWidget from the data provided by a .json file with a library external to qt called nlohmann/json.hpp (I haven't noticed the existence of the QJson library until yesterday), I had to subclass QAbstractTableModel which successfully displays the data. 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. But I cannot figure out why.

      For my little project I mimicked the QtCreator approach with a ui_mainwindow.h file written by hand, because I wanted to be able to use lists of widget (pointers):

      #ifndef UI_MAINWINDOW_H
      #define UI_MAINWINDOW_H
      #include <iostream>
      #include <fstream>
      #include <QtCore/QVariant>
      #include <QtWidgets/QApplication>
      #include <QtWidgets/QFrame>
      #include <QtWidgets/QLabel>
      #include <QtWidgets/QMainWindow>
      #include <QtWidgets/QWidget>
      #include <QtWidgets/QTabWidget>
      #include <QtWidgets/QHBoxLayout>
      #include <QList>
      #include <QTableView>
      #include <QtWidgets/QHeaderView>
      #include <nlohmann/json.hpp>
      #include "zauberdata.h"
      #include "zaubertablemodel.h"
      #include "zaubertablemodel.cpp"
      
      QT_BEGIN_NAMESPACE
      
      class Ui_MainWindow
      {
      public:
          QWidget *centralwidget;
          QWidget *tabwidget;
          QWidget *tabwidget2;
          QLabel *TestLabel;
          QList<QHBoxLayout *> tabs_hLayout;
         // ZauberTableModel *model;
          //QTableView *tabletester;
          QTabWidget *Tabulator;
          ZauberData *Zauberei;
          nlohmann::json j_zauberei;
          QList<QWidget *> get_tablist (int tabnum) {
              QList<QWidget*> tablist; 
              for (int i=0; i < tabnum; ++i) {
      	    tablist.append(new QWidget());
      	}
      	return tablist;
          }
          QList<QTableView *> tableviews;
          QList<ZauberTableModel *> models;
      
          nlohmann::json load_zauber_json_from_file(std::string filename) {
              std::ifstream imagicstream(filename);
      	nlohmann::json j_zauberliste;
      	if (!imagicstream.is_open()) {
      		std::cout << "Zauber laden schlug fehl!";
      	} else {
      		std::cout << "should work" << std::endl;
      		imagicstream >> j_zauberliste ;
      		int len = j_zauberliste.size();
      		std::cout << "Datei erfolgreich geladen " << std::to_string(len) << "Eintraege geladen von " << filename << std::endl;
      	}
      	return j_zauberliste;
          }	
      
          void setupUi(QMainWindow *MainWindow)
          {
              j_zauberei = load_zauber_json_from_file("/home/andi/Projekte/qt/pathfinder-json/waldläuferzauber-erw.json");
      	Zauberei = new ZauberData(j_zauberei);
      	QList<int> zeilenliste = Zauberei->get_zeilen_from_json();
              MainWindow->resize(800, 600);
      	centralwidget = new QWidget(MainWindow);
      	Tabulator = new QTabWidget(centralwidget);
      	Tabulator->setGeometry(QRect(4, 4, 700, 400));
      	TestLabel = new QLabel(tabwidget);
      	QList<QWidget *> tabs = get_tablist(zeilenliste.size());
              QList<QTableView *> tableviews;
      	QList<ZauberTableModel *> models;
      	QList<QHBoxLayout *> tabs_hLayout;
      	for (int i=0; i<zeilenliste.size(); ++i) {
      	    tabs.at(i)->setObjectName(QString("TabGrad%1").arg(i));
      	    QString s = "Grad " + QString::number(i);
      	    Tabulator->addTab(tabs.at(i), s);
                  tableviews.append(new QTableView(tabs.at(i)));
      	    int zeilen = zeilenliste.at(i);
      	    std::cout << "Zeilenzahl: " << zeilen << std::endl;
      	    models.append(new ZauberTableModel(i, j_zauberei, centralwidget));
      	    tabs_hLayout.append(new QHBoxLayout(tabs.at(i)));
      	}    
      	for (int i=0; i<zeilenliste.size(); ++i) {
      	    tableviews.at(i)->setModel(models.at(i));
      	    tableviews.at(i)->horizontalHeader()->setSectionsMovable(true);
                  tabs_hLayout.at(i)->addWidget(tableviews.at(i));
      	}
      
      	MainWindow->setCentralWidget(centralwidget);
          }	
      };
      
      #include "zaubertablemodel.h"
      #include "zauberdata.h"
      #include <nlohmann/json.hpp>
      #include <string>
        
      
          QVariant ZauberTableModel::data(const QModelIndex &index, int role) const {
              std::cout << "data Methode" << std::endl;
              if (role == Qt::DisplayRole) {
      	int zeile, 
      	spalte;
      	int iteration = -1;
      	zeile = index.row();
      	//std::cout << " zeilen " << zeile << std::endl;
      	zeile = ZauberTableModel::zeilenjustierung(zeile);
      	//std::cout << "OK zeilen " << zeile << std::endl;
      	spalte = index.column();
      	nlohmann::json row_obj = Zauberei->get_row_obj(zeile);
      	for (auto& el :row_obj.items()) {
      	    ++iteration;
      	    std::string tn = el.value().type_name();
      	    //std::cout << "Urtyp " << el.value().type() << std::endl;
      	    int bool_comp = tn.compare("boolean");
      	    int number_comp = tn.compare("number");
      	    if ((el.value().type()==nlohmann::json::value_t::string) && (iteration==spalte)) {
      	        //std::cout << "Type " << el.value().type_name() << " Key " << el.key() << std::endl;
      		std::cout << "Schlüssel "<< el.key() << " Spalte " << iteration << "Zeile "<< zeile << std::endl;
      	        QString s = QString::fromStdString(el.value());
      	        return s;
      	        break;
      	    } else if ((bool_comp == 0) && (iteration==spalte)) {
      		std::cout << "Schlüssel "<< el.key() << " Spalte " << iteration << "Zeile "<< zeile << std::endl;
      	        bool flag = el.value();
      	//	std::cout << "Flags auch?" << std::endl;
      	        //QString s = QString::number(flag);
      		return flag;
      		break;
      	    } else if ((el.value().type()==nlohmann::json::value_t::number_integer) && (iteration==spalte)) {
      	        int counter = el.value();
      		std::cout << "Zahlen auch? " << counter << std::endl;
      	        //QString s = QString::number(counter);
      		return counter;
      		break;
      	    } else if ((number_comp == 0)  && (iteration == spalte)) {
      	 //       std::cout << "Wie jetzt?" << std::endl;
      		std::cout << "Schlüssel "<< el.key() << " Spalte " << iteration << "Zeile "<< zeile << std::endl;
      		int counter = el.value();
      		//QString s = QString::number(counter);
      	        return counter;
      		break;
                  }		
      	} 
      	}
      	return QVariant();
          }	
      
          bool ZauberTableModel::setData(const QModelIndex &index, const QVariant &value, int role) 
           {
           std::cout << "Dies ist die setData Methode" << std::endl;
           if (role == Qt::EditRole) {
               int zeile,
               spalte;
               zeile = ZauberTableModel::zeilenjustierung(index.row());
               spalte = index.column();
               nlohmann::json row_obj = Zauberei->get_row_obj(zeile);
               QList<QString> headerliste = Zauberei->get_header();
               for (int i=0; i<headerliste.size(); ++i) {
      	     if (i == spalte) {
      		 QString qs = headerliste.at(i);
                       std::string spaltenname = qs.toStdString();
      	             QString s;
      		     QVariant qvtest = value;
      
      	         if ((value.canConvert<QString>()) && (qvtest.convert(QVariant::String)))
      	         {
      		     s = value.toString();
      	             std::string neuerstringwert = s.toStdString();
      	             row_obj[spaltenname] = neuerstringwert;
      	         }  
      	         else if ((value.canConvert<int>()) && (qvtest.convert(QVariant::Int))) {
      	            int neuerzahlwert = value.toInt();
      	            row_obj[spaltenname] = neuerzahlwert;
      	         }
      	         else if ((value.canConvert<bool>()) && (qvtest.convert(QVariant::Bool)))
      	         {
      		   std::cout << "Überhaupt angekommen?" << std::endl;
      	           bool neuerboolwert = value.toBool();
      		   row_obj[spaltenname] = neuerboolwert;
                       }
      	     }    
             }	 
         emit dataChanged(index, index, {role});    
         return true;
         } else {
         return false;
          }
        } 
          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;
          }	 
      
      

      The above shows the code of the subclassed QAbstractTableModel. So how could I debug this? Could it be that setData does not get called because I use a QList of models? Or should I learn to use the QJson class. (Btw. I know the code is still ugly, but I first wanted to get the basics to work before cleaning up.)

      Kind regards,

      Andreas

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

      @andi456

      • Please put your code block inside the Code tags offered by the forum posts. You can see how difficult what you have posted is to read, it has been broken into multiple segments, and the indentation is all over the place.

      • Please make an attempt to edit your code down to a minimal example of whatever your issue is. Most of yours seems irrelevant to the actual problem. Nobody wants to wade through hundreds of lines to find a problem.

      • Show your declarations of setData() & data() from your class .h file. Have you used C++ override keyword to make sure your signatures are correct, e.g. I am thinking of the defaulted role parameter.

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        did you overwrite the flags function to say its editable?
        https://doc.qt.io/qt-5/qabstractitemmodel.html#flags
        ItemIsEnabled and ItemIsSelectable is default so maybe thats the issue.
        Seems Qt::ItemIsEditable is not default on as far as i know.

        JonBJ 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          did you overwrite the flags function to say its editable?
          https://doc.qt.io/qt-5/qabstractitemmodel.html#flags
          ItemIsEnabled and ItemIsSelectable is default so maybe thats the issue.
          Seems Qt::ItemIsEditable is not default on as far as i know.

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

          @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 1 Reply Last reply
          0
          • 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

                                          • Login

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