Nulik
Nulik

Reputation: 7350

Creating QAbstractItemModel object from constructor

I am getting this error:

/home/niko/QT_snippets/oop2/bus.cpp:3: error: no matching function for call to ‘Person::Person(Bus*, int)’
 Bus::Bus(QObject *parent) : QObject(parent) , person1(this,100), person2(this,200)
                                                                                  ^
/home/niko/QT_snippets/oop2/bus.h:5: In file included from ../oop2/bus.h:5:0,
/home/niko/QT_snippets/oop2/bus.cpp:1: from ../oop2/bus.cpp:1:
/home/niko/QT_snippets/oop2/person.h:11: candidate: Person::Person(QAbstractItemModel*, int)
     explicit Person(QAbstractItemModel *parent = 0, int shoe_color=0);
              ^
/home/niko/QT_snippets/oop2/person.h:11: note:   no known conversion for argument 1 from ‘Bus*’ to ‘QAbstractItemModel*’

And my question is, how do I initialize an object where the members are of QAbstractModelItem class ? (The parent of person1 and person2 must be Bus because when I delete it, I want the children to be deleted to.) I tried to cast to (QObject*) but I got another compilation error.

This is the full source code of the example:

//File: bus.h
#ifndef BUS_H
#define BUS_H

#include <QObject>
#include "person.h"

class Bus : public QObject
{
    Q_OBJECT
public:
    explicit Bus(QObject *parent = 0);

private:
    Person      person1;
    Person      person2;
};

#endif // BUS_H


//File: bus.cpp
#include "bus.h"

Bus::Bus(QObject *parent) : QObject(parent) , person1(this,100), person2(this,200)
{

}



//File: person.h
#ifndef PERSON_H
#define PERSON_H

#include <QObject>
#include <QAbstractItemModel>

class Person : public QAbstractItemModel
{
    Q_OBJECT
public:
    explicit Person(QAbstractItemModel *parent = 0, int shoe_color=0);
    QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const;
    QModelIndex parent(const QModelIndex &child) const;
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
private:
    int m_shoe_color;
};

#endif // PERSON_H


//File: person.cpp
#include "person.h"

Person::Person(QAbstractItemModel *parent,int shoe_color) : QAbstractItemModel(parent)
{
    m_shoe_color=shoe_color;
}
QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const {

}
QModelIndex parent(const QModelIndex &child) const {

}
int rowCount(const QModelIndex &parent = QModelIndex()) const {

}
int columnCount(const QModelIndex &parent = QModelIndex()) const {

}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const {

}



//File: main.cpp
#include <QCoreApplication>
#include "person.h"
#include "bus.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Bus b;
    return a.exec();
}

Upvotes: 0

Views: 408

Answers (1)

lena
lena

Reputation: 1191

In the line person1(this, 100) you pass this to the Person constructor, and this in the current context is Bus. But the constructor expects QAbstractItemModel as parent.

Because Bus is not a subclass of QAbstractItemModel, you are getting compiler errors.

To make it work either make Bus a derivative of QAbstractItemModel or change the Person constructor to explicit Person(QObject *parent = 0, int shoe_color=0);

Upvotes: 1

Related Questions