Results 1 to 2 of 2

Thread: Cant create an instance of custom class extending Qobject.

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Cant create an instance of custom class extending Qobject.

    This is a piece of the header:
    Qt Code:
    1. class Qt_utiles : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit Qt_utiles(QObject *parent = 0);
    6. .....
    To copy to clipboard, switch view to plain text mode 

    and the constructor into cpp :
    Qt Code:
    1. Qt_utiles::Qt_utiles(QObject *parent) :
    2. QObject(parent)
    3. {
    4. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cant create an instance of custom class extending Qobject.

    Quote Originally Posted by tonnot View Post
    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 Code:
    1. qt_utiles = new Qt_utiles(this);
    To copy to clipboard, switch view to plain text mode 
    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 Code:
    1. Qt_utiles qt_utiles;
    To copy to clipboard, switch view to plain text mode 
    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++.)
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. Replies: 1
    Last Post: 17th December 2010, 15:43
  2. Replies: 1
    Last Post: 6th May 2010, 11:05
  3. Replies: 3
    Last Post: 26th March 2010, 07:26
  4. Replies: 0
    Last Post: 3rd January 2009, 05:26
  5. create a Class inherits from two QObject subclasses
    By sabeesh in forum Qt Programming
    Replies: 17
    Last Post: 31st December 2007, 12:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.