gpt4 book ai didi

qt - 构造函数是私有(private)的?

转载 作者:行者123 更新时间:2023-12-02 03:42:16 25 4
gpt4 key购买 nike

C:/Qt/.../mymodel.h:-1:在成员函数“void MainWindow::createModel()”中:

错误:“myModel::myModel(QObject*)”是私有(private)的

错误:在此上下文中

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

}

ma​​inwindow.h

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow();

private slots:
...

signals:
...

private:
...
myModel *model;
};

ma​​inwindow.cpp:

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

谢谢。

最佳答案

我将在开头说,我刚刚浏览了其他 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.

我猜你应该将其移到 mymodel.h 中的 public: 之前

这是我用来找到这个的SO帖子:

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

关于qt - 构造函数是私有(private)的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6326650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com