gpt4 book ai didi

c++ - movetothread 可访问

转载 作者:行者123 更新时间:2023-11-27 23:00:04 25 4
gpt4 key购买 nike

我有以下类(class):

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 表示私有(private)继承(默认情况下),因此您不能在您的案例中使用公共(public)成员。我希望你熟悉继承规则。如果没有,see this question.如您所见,在您的例子中,moveToThread() 是一个私有(private)方法,当然您不能在类外访问它。

解决方案:显式指定公共(public)继承:

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/

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