Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inaccessible conversion in QObject

Tags:

Here is my sample code:

class hoho : public QObject {     Q_OBJECT public:     hoho()      {         httpFetch = new HttpFetch(QUrl("http://www.google.com/"));         connect(httpFetch, SIGNAL(Fetched()), this, SLOT(PrintData(QByteArray)));     }      void PrintData(QByteArray http)     {         qDebug()<<http;     }      HttpFetch *httpFetch; }; 

When I try to compile this, following error pops up

1>main.cpp(15): error C2243: 'type cast' : conversion from 'HttpFetch *' to 'const QObject *' exists, but is inaccessible 

This error comes as the class is derived from QObject (which is necessary for signal and slot mechanism).

Can anyone tell me how to fix this?

like image 352
asitdhal Avatar asked Jun 07 '13 08:06

asitdhal


2 Answers

You probably did not derive HttpFetch publicly, but privately from QObject. So just change

class HttpFetch : QObject { // ... 

to

class HttpFetch : public QObject { // ... 

and it should work.

like image 68
Ralph Tandetzky Avatar answered Sep 20 '22 09:09

Ralph Tandetzky


If your design requires to make the inheritance non-public (I had this requirement because I inherited from a QWidget for a multithreading purpose and didn't want to expose all functions to the user), you can do this:

class FilesQueueQList : protected QWidget {     Q_OBJECT  public:     using QWidget::QObject; //This is the solution! //... } 

Now the members of QWidget are private/protected, but QObject is accessible as public.

like image 35
The Quantum Physicist Avatar answered Sep 20 '22 09:09

The Quantum Physicist