Solved Help creating a tree that traverses the map of a graph
-
You forgot a start before
rootNode
in the declaration. -
-
Thanks for that catch, but it wasn't the error.
invalid use of incomplete type ‘class QAbstractItemModel’
If I uncomment the #define<QModelIndex> in the MainWindow class, the error originates from there (though the location is in the qabstractitemmodel.h file), and when I comment it, it comes from TreeMapModel.h
-
Sorry but I'm going to be blunt: start by cleaning up your code.
It's full of errors:
- Wrong signatures for function you want to override
- You are trying to override non-virtual methods
- Wrong type used
- You have default values in methods implementation signatures
- You want to use a QVector on a type that doesn't match the requirements to be stored in a QVector or your QVector variable declaration is wrong.
- Mismatch of function signature between declaration and implementation
- You are using undeclared variables.
-
I mostly left them in there for the sake of thoroughness. I know those other errors have nothing to do with it. I created a new widget project. It has the very basic default implementation. The only difference is the include for QModelIndex (I tried QAbstractItemModel and their combinations to no avail). So here are the only 2 files worth posting:
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = ShowError TEMPLATE = app DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui
MainWindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QModelIndex> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
And the result always yields:
In file included from /home/chris/Qt5.9.2/5.9.2/gcc_64/include/QtCore/QAbstractItemModel:1:0,
from ../ShowError/mainwindow.h:5,
from ../ShowError/main.cpp:1:
/home/chris/Qt5.9.2/5.9.2/gcc_64/include/QtCore/qabstractitemmodel.h:174:41: error: expected ‘)’ before ‘’ token
explicit QAbstractItemModel(QObject parent = Q_NULLPTR);
^ -
Did you modify that header at some point ?
-
Nope. (posts need at least 8 characters)
-
And your header also looks like this at line 174 ?
class Q_CORE_EXPORT QAbstractItemModel : public QObject { Q_OBJECT friend class QPersistentModelIndexData; friend class QAbstractItemViewPrivate; friend class QIdentityProxyModel; public: explicit QAbstractItemModel(QObject *parent = Q_NULLPTR); virtual ~QAbstractItemModel(); Q_INVOKABLE bool hasIndex(int row, int column, const QModelIndex &parent = QModelIndex()) const; Q_INVOKABLE virtual QModelIndex index(int row, int column,
-
Correct
(more than 8 chars)
-
@Trayon said in Help creating a tree that traverses the map of a graph:
#include <QModelIndex>
And you get error by simply including this in a default GUI project ?
-
Yes, that is correct.
-
@Trayon
Your Qt installation must be broken then as in a default project
there cant be any circular dependency.invalid use of incomplete type ‘class QAbstractItemModel’
That error went away ? and now its just
"explicit QAbstractItemModel(QObject parent = Q_NULLPTR);"
error: expected ‘)’ before that is left?In a clean project, only thing that can be wrong is the Qt files then.
Did you use Refactor and replace or anything that might have altered the Qt files ?And just to be 100% clear,
you made a new default project with File->New and
adding #include <QModelIndex>
to that , gives error ?
If yes, then reinstall Qt. -
Reinstalled QT. Thanks for that tip. It got rid of those errors, but now I'm getting something a bit odd.
The error I got was:
no matching function for call to ‘QVector<TreeMap>::indexOf(const TreeMap)’*Here:
int TreeMap::nodeIndex() const { if (parent) return parent->nodes.indexOf(this); return 0; }
Here's TreeMap.h for reference:
#ifndef TREEMAP_H #define TREEMAP_H #include <QString> #include <QVector> class TreeMap { public: TreeMap(QString name, TreeMap *parentNode = 0); ~TreeMap(); TreeMap nodeAt(int position) const; int nodeCount() const; QString data() const; bool insertNode(int position); TreeMap* getParent(); bool removeNode(int position); int nodeIndex() const; bool setData(const QString &value); private: QString nodeName; QVector<TreeMap> nodes; TreeMap *parent; }; #endif // TREEMAP_H
-
Hi,
You have a QVector of TreeMap object,
this
is a pointer to the current object. -
Hi
nodes.indexOf(this);
"This" is a const TreeMap *
but it wants a &
int indexOf(const T &t, int from = 0) const;so you can do
int TreeMap::nodeIndex() const {
nodes.indexOf(*this);
}However, QVector wants an assignable data type so you will need some extra functions
http://doc.qt.io/qt-5/containers.html#assignable-data-typesomething like (stripped down)
class TreeMap { public: TreeMap(QString name, TreeMap* parentNode = 0) {} int nodeIndex() const; TreeMap(const TreeMap& other) {/* IMPLEMENT*/} TreeMap& operator=(const TreeMap& other) {/* IMPLEMENT*/} bool operator==(const TreeMap& other) {/* IMPLEMENT*/} // most likely wants this too private: QVector<TreeMap> nodes; };
-
Thanks mrjj.
A few more things to add. You most likely know I'm following the example here:
http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.htmlWhy is it that the example doesn't implement these functions? QList has the same shortcoming as QVector when I tried changing it. Also, should I start a new thread with the new errors that pop up in my program, or should I continue posting here as they come?
-
That example is using a QList of pointer to TreeItem you are using a QVector of TreeMap object.
-
@Trayon
Hi
The main difference with the sample is that it uses pointers.
QList<TreeItem*>
So it can just compare pointers. ( they are assignable-data-type by nature)You are use a class directly so it need you to tell it how to compare etc.
since it cannot know what members inside that should be used.like if we have
class Car {
QString Model;
}if we have
Car *A = new Car;
and
Car *B = new Car;
we can say if ( A == B ) and it compiler can just check is the memory address is the same.
But if we do
Car A;
Car B;
and say if ( A == B )
then what should it compare.
we can then "explain it" to the compiler with
if ( A.model == B.model )
and that is what we do with
operator==(..)@SGaist (hehe ninjaed)
-
Yes, but as I said, I tried changing it to QList, and it had the same shortcomings. Down the error line, I even saw the "no match for '=='" error for QList as well.
-
@Trayon
Yes, as explained it dont know how to compare your class when its NOT pointers.