我有以下课程:

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。

这是怎么了

最佳答案

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

};

关于c++ - movetothread可以访问,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28655596/

10-11 15:53