Results 1 to 3 of 3

Thread: Qt connect without SLOT macro

  1. #1

    Default Qt connect without SLOT macro

    et's say I have a string containing "function()", where function() is a slot in a class, and I would like to connect that slot with any signal, but using that string. Neither

    Qt Code:
    1. QString f="function()";
    2. connect (randomobject, SIGNAL(randomsignal()), this, SLOT(f));
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. QString f="SLOT(function())";
    2. //conversion to const char*
    3. connect (randomobject, SIGNAL(randomsignal()), this, f);
    To copy to clipboard, switch view to plain text mode 

    work.

    Is there a way to do something similar? It's important that it's a string and not a function pointer.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt connect without SLOT macro

    I don't think you can use this string in "connect" statement directly, but you can connect the signal to a regular slot, in which you call QMetaObject::invokeMethod
    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3.  
    4. int main(int argc, char *argv[]) {
    5. QApplication a(argc, argv);
    6. const QString slotName = "show"; // no parentheses
    7. const QByteArray ba = slotName.toLocal8Bit();
    8. const char * c_str = ba.constData();
    9. QMetaObject::invokeMethod(&w, c_str);
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt connect without SLOT macro

    Quote Originally Posted by webstar View Post
    Qt Code:
    1. QString f="SLOT(function())";
    2. //conversion to const char*
    3. connect (randomobject, SIGNAL(randomsignal()), this, f);
    To copy to clipboard, switch view to plain text mode 
    That is almost there, you just need to get a slightly different string
    Qt Code:
    1. QString f=SLOT(function());
    2. connect (randomobject, SIGNAL(randomsignal()), this, f.toAscii().constData());
    To copy to clipboard, switch view to plain text mode 

    I.e. the SLOT() macro creates a string, with the function name, any argument types and some slot specific prefix.

    Cheers,
    _

Similar Threads

  1. Object::connect: No such slot
    By kango in forum Newbie
    Replies: 8
    Last Post: 20th February 2013, 10:00
  2. Replies: 4
    Last Post: 14th August 2011, 15:37
  3. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 20:42
  4. QObject::connect: No such slot !?!
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 18:31
  5. Qt Designer & Qt4, connect to my own slot.
    By geitosten in forum Newbie
    Replies: 2
    Last Post: 17th February 2007, 19:22

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.