PDA

View Full Version : Cant create an instance of custom class extending Qobject.



tonnot
10th February 2011, 13:08
This is a piece of the header:

class Qt_utiles : public QObject
{
Q_OBJECT
public:
explicit Qt_utiles(QObject *parent = 0);
.....

and the constructor into cpp :

Qt_utiles::Qt_utiles(QObject *parent) :
QObject(parent)
{
}

Ok. My questions ....

Must I create the class in this way ?:
Qt_utiles *qt_utiles : at h.file
Qt_utiles qt_utiles(0) :at cpp.file

There is another way ?
Every class that extends Qt must be created using * approach ?
(Yes, I know that these are basic C++ questions ...)
Thanks

franz
10th February 2011, 17:16
Must I create the class in this way ?
Qt_utiles *qt_utiles : at h.file
Qt_utiles qt_utiles(0) :at cpp.file

These are basic C++ questions indeed, and I'm not sure then what you mean by "Can't create".

If you have a reference declared in your class header in the way you described, you should instantiate it with

qt_utiles = new Qt_utiles(this); in your source file. This will ensure that the object stays alive for as long as the creating class (this) exists and be destroyed when the creating class is destroyed.

If you only need an instance of the class while in a certain scope, you can create it on the stack:

Qt_utiles qt_utiles; Bear in mind that when leaving the particular scope, the object will be destroyed automatically.

To be able to give more specific answers, we will need more specific questions. Also you might want to work through some C/C++ documentation before diving into Qt development head first. (You'll come out with a nose bleed. Qt is easy and convenient if you already know C++.)