Results 1 to 6 of 6

Thread: Can't create an object : initialisation problem ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    158
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    25

    Default Re: Can't create an object : initialisation problem ?

    Small UP, I don't find the solution...
    So perhaps someone may find it

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Can't create an object : initialisation problem ?

    A good practice is to allocate objects derived from QObject on the heap (with operator "new"). QObjects allocated on the stack in together with a parent-child relationship tend to cause problems (*). I suggest diving into docs; QObject, Object Trees and Object Ownership..

    I'm not sure if this is the problem of yours because I can't see any parent being passed for any object in the example piece of code.. But QObject parent-child relationship sure is a concept a developer must understand while developing with Qt.

    (*) A QObject deletes all it's children. A child gets destructed twice in case it was allocated on the stack, 1) by the parent 2) when going out of scope => crash.
    J-P Nurmi

  3. #3
    Join Date
    Feb 2007
    Posts
    158
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    25

    Default Re: Can't create an object : initialisation problem ?

    Hello,

    Someone esle gave me the solution on another forum.
    My error was to use the QTcpSocket in the constructor of MailChecker while it wasn't initialised :

    Qt Code:
    1. MailChecker::MailChecker() : QObject()
    2. {
    3. connect(socket_r, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(afficher_erreur_socket(QAbstractSocket::SocketError)));
    4. }
    To copy to clipboard, switch view to plain text mode 

    The good code is :

    Qt Code:
    1. MailChecker::MailChecker() : QObject()
    2. {
    3. socket_r = new QTcpSocket();
    4.  
    5. connect(socket_r, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(afficher_erreur_socket(QAbstractSocket::SocketError)));
    6. }
    To copy to clipboard, switch view to plain text mode 

    Is was initialising my Socket directly in a function that :
    - initialise the socket
    - connect to host
    - submit a request
    - listen the answer
    - close the socket
    - destroy the socket

    Thanks Dudule from developpez.net C++ forums



    PS : Thanks jpn, your answer is usefull for me, because I didn't know that

Similar Threads

  1. QT4 scope problem
    By the_bis in forum Newbie
    Replies: 5
    Last Post: 29th January 2007, 23:01
  2. Problem combining QWorkspace & QGLWidget
    By Nb2Qt in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2006, 21:45
  3. Replies: 16
    Last Post: 7th March 2006, 15:57
  4. passing a QPainter object to widgets
    By vratojr in forum Qt Programming
    Replies: 9
    Last Post: 11th January 2006, 15:27

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
  •  
Qt is a trademark of The Qt Company.