0

Hi Everyone i have a foo class

Code in foo.h

namespace GUI
{
class Foo : public QObject
{
Q_OBJECT

public:
explicit Foo(QObject *parent = 0);
virtual ~Foo();
....


};
}

Now this is working and compiling fine but i want to save this custom c++ class using QSettings and one of the step is registering your class with Q_DECLARE_METATYPE

and therefore as soon as i put this line Q_DECLARE_METATYPE(Foo) at the end of my foo.h file i get these compiler error C2248:'QObject::Qobject':cannot access private member declared in class 'QObject' and which when clicked only takes me to last line of my connection.h header file and gives no other information as to what might be wrong i,e the error point me to

  Class Foo
  {

  };<---- point me here
  Q_DECLARE_METATYPE(Foo)

I know that QObject cannot be copied and this is related to it but i have no idea what maybe wrong here and how can i rectify it. Thanks in advance

bourne
  • 1,083
  • 4
  • 14
  • 27

3 Answers3

2

I know that QObject cannot be copied and this is related to it but i have no idea what maybe wrong here and how can i rectify it.

It is related. Q_DECLARE_METATYPE requires your type to be copiable, but your type inherits from QObject, so you can't do that. Sure, you could instead Q_DECLARE_METATYPE(Foo*), but I think you should instead move the settings into a separate value class, which then you can save using QSettings.

peppe
  • 21,934
  • 4
  • 55
  • 70
  • thanks a lot that did worked ,btw do you suggest that i should make a new struct of all the member variables that i need to save using Qsettings and than have a member variable of that struct in my connection class.And that struct would not be inherited from QObject – bourne Nov 18 '14 at 23:40
  • Yes, exactly. Then you can create some functionality to save/restore an instance of that struct from settings. – peppe Nov 18 '14 at 23:49
0

Make sure your Q_DECLARE_METATYPE statement is outside of your namespace and that you fully qualify your class name. See the Q_DECLARE_METATYPE doc for more detail.

namespace GUI
{

class Connection : public QObject
{
    ...
};

}

Q_DECLARE_METATYPE(GUI::Connection)
kersson
  • 23
  • 3
0

I know that QObject cannot be copied and this is related to it but i have no idea what maybe wrong here and how can i rectify it.

So declare a copy constructor, then it will be ok.

...
Foo(const Foo &_other);
...
rca
  • 196
  • 1
  • 9