2011-06-13 115 views
4

C:/ QT /.../ mymodel.h:-1: 在成员函数 '无效的MainWindow :: createModel()':构造函数是私有的?

错误:基于myModel ::基于myModel(QObject的*)'是私人

错误:此背景下

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); 

感谢。

+0

在这里的文档:http://doc.trolltech.com/4.5/moc.html#moc和在你的mainwindow.h中,我看到在'public:'之前使用的Q_OBJECT 。在mymodel.h中,你在'public:'之后。宏是否可能重新引入'private:'?尝试在“public:”之前移动它,看它是否能解决你的问题。 – ccoakley 2011-06-13 04:28:31

回答

4

我将在此前言说我刚刚浏览过其他Qt问题,然后偶然发现下面的文档站点以获得此猜测。

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.

我猜你应该是你的public:前mymodel.h

移动这是SO后我曾经找到这个:

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

+0

**非常感谢** – Trainee 2011-06-13 04:37:51

+0

我很好奇:它有用吗? – ccoakley 2011-06-13 04:38:36

+0

是的,它的工作原理。非常感谢 – Trainee 2011-06-13 04:39:39