2011-06-13 116 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

相關問題