Results 1 to 5 of 5

Thread: connect in constructor?

  1. #1
    Join Date
    Oct 2006
    Location
    Massachusetts, USA
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question connect in constructor?

    Hello --

    I have a class derived from QObject which has a QTimer member called m_MyTimer. I have a private slot in that object with signature void MyTimeout(). The following call in the class constructor causes a crash.

    Qt Code:
    1. connect(&m_MyTimer,SIGNAL(timeout()),this,SLOT(MyTimeout()));
    To copy to clipboard, switch view to plain text mode 

    The failure is at the 4th line of the body of the following Qt library function (at the for loop).

    Qt Code:
    1. int QMetaObject::indexOfSlot(const char *slot) const
    2. {
    3. int i = -1;
    4. const QMetaObject *m = this;
    5. while (m && i < 0) {
    6. for (i = priv(m->d.data)->methodCount-1; i >= 0; --i)
    7. if ((m->d.data[priv(m->d.data)->methodData + 5*i + 4] & MethodTypeMask) == MethodSlot
    8. && strcmp(slot, m->d.stringdata
    9. + m->d.data[priv(m->d.data)->methodData + 5*i]) == 0) {
    10. i += m->methodOffset();
    11. break;
    12. }
    13. m = m->d.superdata;
    14. }
    15. return i;
    16. }
    To copy to clipboard, switch view to plain text mode 

    In particular, m->d.data is 0.

    A workaround is to put the call to connect() in an initialization function that the client of the object is then obliged to call once, but that's annoying. Is there a way to get the connection at construction time?

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: connect in constructor?

    Do you have any static or global variables in your application?

  3. #3
    Join Date
    Oct 2006
    Location
    Massachusetts, USA
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect in constructor?

    Yes, the object itself is global ... I sense a newbie moment approaching ... why, does that affect order of construction of the object?

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: connect in constructor?

    All QObjects have to be createad after a QAppliaction object has been instantiated.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: connect in constructor?

    Quote Originally Posted by McKee View Post
    why, does that affect order of construction of the object?
    I think you are experiencing a static initialization order fiasco. It's easy to stumble upon it in Qt, because every class with Q_OBJECT macro has a static member variable with meta-data and if you initialize your object before that meta-data object was created, you hit a null pointer.

    Also, as Marcel has already said, you should create all Qt objects after QApplication instance was created, since only then Qt is fully initialized. This especially applies to fonts, codecs and everything that has something to do with plugins.

  6. The following user says thank you to jacek for this useful post:

    McKee (14th October 2007)

Similar Threads

  1. QDialog: show() and exec() together in constructor?
    By Teuniz in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2007, 11:43
  2. Killing a Window in its constructor
    By hardgeus in forum Qt Programming
    Replies: 6
    Last Post: 15th December 2006, 18:31
  3. Replies: 5
    Last Post: 28th August 2006, 14:36
  4. use libs under qt4
    By raphaelf in forum Qt Programming
    Replies: 6
    Last Post: 27th February 2006, 17:59

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.