movetothread是可访问的

时间:2015-02-22 07:35:55

标签: c++ qt

我有以下课程:

class commMonitor : QObject
{
    Q_OBJECT
public:
    commMonitor();
    ~commMonitor();
private:
    QMap<unsigned short int, QTimer*> Monitors;
    QTimer* currTimer;
public slots:
    void createMonitor(unsigned short int);
    void deleteMonitor(unsigned short int);
    void startMonitor(unsigned short int);
    void stopMonitor(unsigned short int);

};

当我尝试执行以下操作时:

commMonitor* commOverWatch = new commMonitor();
QThread* monitorThread = new QThread();
commOverWatch->moveToThread(monitorThread);

visual studio将无法编译,错误是QObject :: moveToThread无法访问。

这里出了什么问题?

1 个答案:

答案 0 :(得分:4)

在C ++中class commMonitor : QObject表示私有继承(默认情况下),因此在您的情况下不能使用公共成员。我希望你熟悉继承规则。如果不是,see this question.正如您所看到的,在您的情况下,moveToThread()是一种私有方法,当然您无法在课堂外访问它。

解决方案:明确指定公共继承:

class commMonitor : public QObject
{
    Q_OBJECT
public:
    commMonitor();
    ~commMonitor();
private:
    QMap<unsigned short int, QTimer*> Monitors;
    QTimer* currTimer;
public slots:
    void createMonitor(unsigned short int);
    void deleteMonitor(unsigned short int);
    void startMonitor(unsigned short int);
    void stopMonitor(unsigned short int);

};