4

C:/Qt/.../mymodel.h:-1: In member function 'void MainWindow::createModel()':

error: 'myModel::myModel(QObject*)' is private

error: within this context

mymodel.h:

#ifndef MYMODEL_H
#define MYMODEL_H

#include <QStandardItemModel>

class myModel : public QStandardItemModel
{
public:
    Q_OBJECT

    myModel(QObject *parent = 0);
};

#endif // MYMODEL_H

mymodel.cpp:

#include "mymodel.h"

myModel::myModel(QObject *parent) :
    QStandardItemModel(parent)
{

}

mainwindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow();

private slots:
    ...

signals:
    ...

private:
    ...
    myModel *model;
};

mainwindow.cpp:

void MainWindow::createModel()
{
    model = new myModel(this);

Thanks.

Trainee
  • 61
  • 5
  • In the docs here: http://doc.trolltech.com/4.5/moc.html#moc and in your mainwindow.h, I see the Q_OBJECT used before the `public:`. In mymodel.h you have it after the `public:`. Is it possible the macro re-introduces a `private:`? Try moving it before the `public:` to see if it fixes your problem. – ccoakley Jun 13 '11 at 04:28

1 Answers1

4

I'm going to preface this with saying that I just browsed SO for other Qt questions and then stumbled around the documentation site below to arrive at this guess.

From http://doc.qt.digia.com/4.5/qobject.html#Q_OBJECT

The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.

I'm guessing that you should move it before your public: in mymodel.h

This was the SO post I used to find this:

What does the Q_OBJECT macro do? Why do all Qt objects need this macro?

Community
  • 1
  • 1
ccoakley
  • 3,235
  • 15
  • 12
  • hehe. sure. you are right. If you take a look at Q_OBJECT macro, it begins from `private:`. So constructor is in private section too – Pie_Jesu Jun 13 '11 at 11:59
  • Sweet. That was my speculation in my comment. Given the error, it seemed like a logical guess. – ccoakley Jun 13 '11 at 13:33