1

I have the following 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);

};

when I tried to do the following:

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

the visual studio won't compile, and the error is the QObject::moveToThread is inaccessible.

What's going wrong here?

jww
  • 97,681
  • 90
  • 411
  • 885
Nyaruko
  • 4,329
  • 9
  • 54
  • 105

1 Answers1

5

In C++ class commMonitor : QObject means private inheritance (by default), so you can't use public members in your case. I hope you are familiar with inheritance rules. If no, see this question. As you can see, in your case, moveToThread() is a private method and of course you can't access it outside the class.

Solution: specify public inheritance explicitly:

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

};
Community
  • 1
  • 1
Jablonski
  • 18,083
  • 2
  • 46
  • 47