Results 1 to 8 of 8

Thread: QSignalMapper One single Object

  1. #1
    Join Date
    May 2009
    Posts
    56
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default QSignalMapper One single Object

    Hi there,

    Is it possible to connect different signals form the same object to a signalmapper, and still know wich signal is triggered? or there is a way to connect signal to a single slot wit a define input.

    first case(did not work):
    Qt Code:
    1. //signal1 and signal2 are cons char *
    2. //firs case:
    3. connect(object,signal1, sMapper,SLOT(map()));
    4. sMapper->setMapping(object, signal1);
    5. connect(object,signal2, sMapper,SLOT(map()));
    6. sMapper->setMapping(object, signal2);
    7.  
    8. connect(sMapper, SIGNAL(mapped(QString)), this, SLOT(triggered(QString)));
    9.  
    10. //second case(What i would like to do):
    11. //assign a predefined value to the signal.
    12.  
    13. connect(object,signal1, this,SLOT(triggered(=signal1)));
    14. connect(object,signal2, this,SLOT(triggered(=signal2));
    To copy to clipboard, switch view to plain text mode 

    thanks for the answer
    Last edited by wysota; 12th March 2011 at 19:05.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSignalMapper One single Object

    It's possible to have multiple signals coming to the same slot if they have the same signatures, but I don't think it's possible to detect which signal caused your slot to be called, only which object caused your signal be called.

    Your second case would always fail at the connect, as the SIGNAL and SLOT signatures must match exactly.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSignalMapper One single Object

    I imagine that you want two (or more) signals to trigger an action (do almost the same thing) and each of them can do some more stuff, that can be achieved with the help of an "helper" function - a function can do the things that are common for all the slots and you code a slot for each signal (with proper connection) and in the slot body do the signal-specific things and then call the helper function (that does the stuff common to all the slots).

    //if i didn't guess the "design" than please say more about what you are trying to do, and than we can share opinions about that.

  4. #4
    Join Date
    May 2009
    Posts
    56
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSignalMapper One single Object

    Hi There,

    What I want is to implement is signal waiter, i have done this already but i don't think is the best way though, but so far is working. I have just create and "helper object", connect the signal that i want to track to the helper object slot, and then trigger a signal with helper object as parameter, then i can track which signal was triggered, but i think for many signal the overhead is to big, is a waste of memory, since i have to store the object that i want to track and the object that track the signal, if anybody have a better idea just let me know, thank for the help.

    Quote Originally Posted by squidge View Post
    It's possible to have multiple signals coming to the same slot if they have the same signatures, but I don't think it's possible to detect which signal caused your slot to be called, only which object caused your signal be called.

    Your second case would always fail at the connect, as the SIGNAL and SLOT signatures must match exactly.
    thanks, i am aware of this situation.

    Quote Originally Posted by Zlatomir View Post
    I imagine that you want two (or more) signals to trigger an action (do almost the same thing) and each of them can do some more stuff, that can be achieved with the help of an "helper" function - a function can do the things that are common for all the slots and you code a slot for each signal (with proper connection) and in the slot body do the signal-specific things and then call the helper function (that does the stuff common to all the slots).

    //if i didn't guess the "design" than please say more about what you are trying to do, and than we can share opinions about that.
    i just want to track the signals

  5. #5
    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: QSignalMapper One single Object

    What exactly do you mean by "track signals"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    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: QSignalMapper One single Object

    Anyway, try this:
    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4. class SignalTracker : public QObject {
    5. int qt_metacall(QMetaObject::Call call, int id, void **args) {
    6. id = QObject::qt_metacall(call, id, args);
    7. if(id==-1 || call != QMetaObject::InvokeMetaMethod)
    8. return id;
    9. if(!m_slots.contains(id)) return -1;
    10. Signal sig = m_slots.value(id);
    11. qDebug() << "Object:" << sig.source->objectName() << "signal:" << sig.signal;
    12. return -1;
    13. }
    14. public:
    15. SignalTracker(QObject *parent = 0) : QObject(parent) {
    16. m_slotId = 0;
    17. }
    18. bool connectSignal(QObject *obj, const char *signal) {
    19. QByteArray theSignal = QMetaObject::normalizedSignature(signal);
    20. int signalId = obj->metaObject()->indexOfSignal(theSignal);
    21. if(signalId == -1) {
    22. return false;
    23. }
    24. m_slots[++m_slotId] = Signal(obj, theSignal);
    25. QMetaObject::connect(obj, signalId, this, QObject::metaObject()->methodCount()+m_slotId);
    26. return true;
    27. }
    28. private:
    29. int m_slotId;
    30. struct Signal {
    31. QObject *source;
    32. QByteArray signal;
    33. Signal(QObject *src, const QByteArray &ba) : source(src), signal(ba){}
    34. Signal() : source(0){}
    35. };
    36. QHash<int,Signal> m_slots;
    37. };
    38.  
    39. int main(int argc, char **argv){
    40. QApplication app(argc, argv);
    41. SignalTracker tracker;
    42. QPushButton button("Click me");
    43. button.setObjectName("some button");
    44. tracker.connectSignal(&button, "clicked()");
    45. button.show();
    46. le.setObjectName("some line edit");
    47. tracker.connectSignal(&le, "textChanged(const QString &)");
    48. le.show();
    49. return app.exec();
    50. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2009
    Posts
    56
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSignalMapper One single Object

    Quote Originally Posted by wysota View Post
    What exactly do you mean by "track signals"?
    Hi wysota,
    what i meanby that is: know when different signal from different or the same object have been triggered to count then or to wait for all of then before doing something, i have no tried yet the example. i let you know when i have.

    Thanks for the help.

  8. #8
    Join Date
    Jan 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QSignalMapper One single Object

    I've just made a class doing this. It tracks every signals from the object you want to monitor. You can just test the parameter of the emitted (or emittedNameOnly) signal to check only the ones you want.

Similar Threads

  1. QSignalMapper
    By axisdj in forum Newbie
    Replies: 6
    Last Post: 16th September 2010, 01:52
  2. QSignalMapper and argument problems
    By harmodrew in forum Newbie
    Replies: 14
    Last Post: 7th August 2010, 19:20
  3. help with QSignalMapper and actions
    By andreime in forum Newbie
    Replies: 1
    Last Post: 9th August 2009, 18:24
  4. ? about QSignalMapper
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2008, 21:21
  5. QSignalMapper question: SIGNAL 2 int's
    By vonCZ in forum Newbie
    Replies: 5
    Last Post: 20th July 2007, 10:02

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.