Results 1 to 9 of 9

Thread: Thread shall emit signals with "complex" argument

  1. #1
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Thread shall emit signals with "complex" argument

    Hello,

    I want to pass some data from a sub thread to the main thread (GUI).
    So I decided to emit a signal from the thread. The signal shall have an object containing the data as argument.
    The main thread has a slot with the identical signature (means: expects an object as argument).

    Qt Code:
    1. // The signal declarationin of the sub thread class:
    2. signals:
    3. void newLineConstructed( MyTableWidgetRow );
    4.  
    5. // The slot definition in the main thread class:
    6. public slots:
    7. void AddNewElementsLine( MyTableWidgetRow newRow );
    8.  
    9. // The "connect" statement used:
    10. connect( m_updateElementsThread, SIGNAL(newLineConstructed(MyTableWidgetRow)),
    11. this, SLOT(AddNewElementsLine(MyTableWidgetRow)) );
    To copy to clipboard, switch view to plain text mode 
    "MyTableWidgetRow" is a class.
    m_updateElementsThread is an instance of a class derived from QThread that declares the above described signal.
    "this" is the main thread. The connecting code is therefore in the constructor of the main thread.

    The slot is never called when the signal is emited (and yes, it is emited in my code).
    When I change the aguments type from MyTableWidgetRow to int, the slot is called, but not with my class as argument.

    Any ideas what could be wrong?
    What more information do you need th "analyze" what could be wrong?

  2. #2
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Thread shall emit signals with "complex" argument

    You don't really want to be using SIGNALs and SLOTs between threads. What you need to do is within your thread create an event and post it to the GUI thread using,

    MyCustomEvent *someevent = new MyCustomEvent(); // This should be a subclass of QCustomEvent and could contain your data.
    QApplication:ostEvent(targetWidget, someevent);

    In the GUI thread, you need to implement,

    void customEvent(QCustomEvent *event);

    to recieve your event.

    Have a read up on these.
    Hope it helps,
    Chris.

  3. #3
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Thread shall emit signals with "complex" argument

    Have a read of "C++ GUI Programming with Qt 3" which can be downloaded from http://www.informit.com/store/produc...sbn=0131240722 in pdf for free. Chaper 17 has a section on multithreading in Qt.

    Chris

  4. #4
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread shall emit signals with "complex" argument

    Why shouldn't I want signals and slots between thread?

    You are talking of Qt 3, aren't you?
    My Qt 4.6 docs say that signals and slots between threads work very well.

    The QCustomEvent is part of the Qt 3 support library, according to the documentation.
    I will give QEvent as base class for my event containing data a try.
    But I still believe signals and slots should work.

  5. #5
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Thread shall emit signals with "complex" argument

    I was just suggesting using the custom event mechanism because I know for certain it works properly. I've posted messages between threads this way in Qt4 applications.

    I don't know whether signalling between threads is valid or otherwise.

  6. #6
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Thread shall emit signals with "complex" argument


  7. #7
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thread shall emit signals with "complex" argument

    Docs say:
    To use the type T in queued signal and slot connections, qRegisterMetaType<T>() must be called before the first connection is established.
    Note that connections across threads are queued by default.

  8. The following user says thank you to numbat for this useful post:

    Boron (17th June 2010)

  9. #8
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread shall emit signals with "complex" argument

    Thanks a lot. numbats' post helped.

    Declaring a new metatype right after the class definition in the header file:
    Qt Code:
    1. Q_DECLARE_METATYPE(MyTableWidgetRow)
    To copy to clipboard, switch view to plain text mode 
    and a
    Qt Code:
    1. qRegisterMetaType<MyTableWidgetRow>();
    To copy to clipboard, switch view to plain text mode 
    right before the first usage in a connection did it.

  10. #9
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread shall emit signals with "complex" argument

    For the future: look in "output" window of your debugger, when you run your application. In your case it had a string:
    "use qRegisterMetaType to send "MyTableWidgetRow" in signals/slots"
    or something like this

Similar Threads

  1. Replies: 7
    Last Post: 8th November 2012, 11:26
  2. Does "emit" in the calling thread mean execution?
    By nomadoro in forum Qt Programming
    Replies: 3
    Last Post: 23rd September 2009, 11:16
  3. Error "QString::arg: Argument missing"
    By Lawand in forum Qt Programming
    Replies: 3
    Last Post: 18th February 2009, 20:26
  4. "emit" keyword optional when calling signals?
    By will49 in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2008, 01:13
  5. Signal defined in "a.h" can not emit in "b.cpp"
    By Shawn in forum Qt Programming
    Replies: 9
    Last Post: 21st May 2007, 16:55

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.