Results 1 to 7 of 7

Thread: signal slot conection using a string, not a SLOT

  1. #1
    Join Date
    Jan 2006
    Posts
    30
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default signal slot conection using a string, not a SLOT

    Ok here is my problem. I have to connect a QAction signal to a QTextEdit slot. Here are my parameters

    connect
    object *
    SIGNAL (triggered ())
    object *
    QString (containing slot name) -- Eample: signal is void copy (), string is "copy"

    How can I do this. Currently all of my attenpts have failed. I always get a false returned from the connect function.

    On thing I know is this should be possible because the slots can be found via QMetaObject::invokeMethod using a string.

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

    Default Re: signal slot conection using a string, not a SLOT

    Hmm... doesn't SLOT(somestring) work? If it doesn't (and it probably doesn't), then take a look at Qt source code and find how the SLOT macro expands and modify the string to be the same.

  3. #3
    Join Date
    Jan 2006
    Posts
    30
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal slot conection using a string, not a SLOT

    Where can I find this macro definition. I have looked all over the source code and can't find this definition.

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

    Default Re: signal slot conection using a string, not a SLOT

    src/corelib/kernel/qobjectdefs.h:

    Qt Code:
    1. #define SLOT(a) "1"#a
    2. #define SIGNAL(a) "2"#a
    To copy to clipboard, switch view to plain text mode 

    So in your case, just prepend the slot signature with "1".

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

    rianquinn (21st March 2006)

  6. #5
    Join Date
    Jan 2006
    Posts
    30
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal slot conection using a string, not a SLOT

    WOW... it works great.

    I had to adjust my string to be "1copy ()" instead of "copy", and it works. I wonder why they add the 1 to the end of this? Seems this makes things a bit more difficult. I'm there is a reason.

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

    Default Re: signal slot conection using a string, not a SLOT

    It's an easy way to distinguish between methods ("0"), slots ("1"), signals ("2") and garbage (??).

  8. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: signal slot conection using a string, not a SLOT

    They prepend a digit because in C++ no identifiers like method or variable names may start with a digit. That way you can distinguish between the types like wysota mentioned.

  9. The following user says thank you to Codepoet for this useful post:

    rianquinn (21st March 2006)

Similar Threads

  1. Replies: 0
    Last Post: 18th February 2009, 13:31
  2. pthread instead QThread
    By brevleq in forum Qt Programming
    Replies: 8
    Last Post: 23rd December 2008, 07:16
  3. Connection of custon signals/slots
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 07:04
  4. Replies: 12
    Last Post: 18th September 2008, 15:04
  5. Signal & Slot editor
    By Ishark in forum Qt Tools
    Replies: 4
    Last Post: 28th May 2008, 15:20

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.