[kind of solved] subclassed QAbstractTableModel::setData does not get called
-
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. -
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.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....
-
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....
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 -
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@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 -
@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@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...
-
@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...
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 ? -
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 ?@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 functionZauberTableModel::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 tovtable 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.
-
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?
-
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?
@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.
QAbstractTableModeldoesn't know anything about JSON. What you do withnlohmann::jsonparameters is up to you. -
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.
-
What build system do you use? Is the moc file for zaubertablemodel.h generated, compiled and linked?
-
Hi,
Might be a silly question but do you build the class code along with your test ?
-
@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?
-
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
-
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 .
-
@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.
-
@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.
@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-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.
@andi456 but even there I see no headerData() implementation...
-
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; } } } -
@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.