Results 1 to 4 of 4

Thread: Signal / Slot with specific argument and threads

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Signal / Slot with specific argument and threads

    Have created a slot like shown below, in order to do something when a worker thread is done, but it must know which one finished:
    Qt Code:
    1. void ProgramMain::serverDisconnectDone(int thread) {
    2.  
    3. }
    To copy to clipboard, switch view to plain text mode 

    Since I know what signal comes from what thread, I wanted to connect them in this way:
    Qt Code:
    1. connect(&thread1, SIGNAL(logoffDone()), this, SLOT(serverDisconnectDone(1)));
    2. connect(&thread2, SIGNAL(logoffDone()), this, SLOT(serverDisconnectDone(2)));
    3. connect(&thread3, SIGNAL(logoffDone()), this, SLOT(serverDisconnectDone(3)));
    4. connect(&thread4, SIGNAL(logoffDone()), this, SLOT(serverDisconnectDone(4)));
    To copy to clipboard, switch view to plain text mode 

    When I execute my program I get the error:
    Object::connect: No such slot ProgramMain::serverDisconnectDone(1)
    Object::connect: (receiver name: 'ProgramMain')
    Object::connect: No such slot ProgramMain::serverDisconnectDone(2)
    Object::connect: (receiver name: 'ProgramMain')
    Object::connect: No such slot ProgramMain::serverDisconnectDone(3)
    Object::connect: (receiver name: 'ProgramMain')
    Object::connect: No such slot ProgramMain::serverDisconnectDone(4)
    Object::connect: (receiver name: 'ProgramMain')
    I know that there are more complicated ways that I could make this work, but the easiest way is to just force the argument in the signal slot connection. Is there any way that I can do this?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signal / Slot with specific argument and threads

    Take a look at QSignalMapper.

  3. The following user says thank you to wysota for this useful post:

    tpf80 (14th September 2007)

  4. #3
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal / Slot with specific argument and threads

    Nice! at least by reading the description for it, this looks like its perfect for what I need. I will see if I can impliment it right away.

  5. #4
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal / Slot with specific argument and threads

    Yes this worked perfectly! Here is the solution I used in case anyone later wants to know about it:
    Qt Code:
    1. QSignalMapper* mapper = new QSignalMapper(this);
    2.  
    3. mapper->setMapping(&thread1, 1);
    4. mapper->setMapping(&thread2, 2);
    5. mapper->setMapping(&thread3, 3);
    6. mapper->setMapping(&thread4, 4);
    7.  
    8. connect(&thread1, SIGNAL(logoffDone()), mapper, SLOT(map()));
    9. connect(&thread2, SIGNAL(logoffDone()), mapper, SLOT(map()));
    10. connect(&thread3, SIGNAL(logoffDone()), mapper, SLOT(map()));
    11. connect(&thread4, SIGNAL(logoffDone()), mapper, SLOT(map()));
    12.  
    13. connect(mapper, SIGNAL(mapped(int)), this, SLOT(serverDisconnectDone(int)));
    To copy to clipboard, switch view to plain text mode 

    Thanks again for pointing me in the right direction!

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.