Results 1 to 3 of 3

Thread: How to declare SLOT as a parameter to member function?

  1. #1
    Join Date
    Jul 2007
    Posts
    121
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How to declare SLOT as a parameter to member function?

    How can I pass slot as a parameter to class member function and then call "connect"?
    Below is the sketch of what I am trying to do. What is the proper way to pass the slot to a member function to be later passed as a parameter to "connect"?

    class A
    {
    private slot:
    void DoSmth() {}; // slot declaration
    public:
    A()
    {
    Connector(&A::DoSmth); //I am trying to pass the slot as function pointer
    }
    void Connector(void (A::*p) ()) // what is the right way to declare the function with slot as a parameter?
    {
    connect (....SLOT(p)); // compiles, but does not work. Apparently I am doing smth. wrong.
    }
    }

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to declare SLOT as a parameter to member function?

    In Qt, signals and slots are const char*. You can do it like this, for example:

    Qt Code:
    1. void connectSlot(const char* slot) {
    2. connect(this, SIGNAL(something(int)), this, slot);
    3. }
    4.  
    5. connectSlot(SLOT(somethingElse(int)));
    To copy to clipboard, switch view to plain text mode 

    Function pointers don't work, because SLOT is a macro that uses the text you place in it.
    Last edited by Michiel; 4th August 2007 at 09:30.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

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

    QPlace (4th August 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to declare SLOT as a parameter to member function?

    You can also do this using the new Qt connect system by passing function pointers

    Qt Code:
    Qt Code:
    1. void Class:connectSlot( void(Class::* slotName )() )
    2. {
    3. connect( this, &Class::noParamSignal, this, slotName );
    4. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Qtopia core 4.2.2 cross compile make error
    By smiyai18 in forum Installation and Deployment
    Replies: 2
    Last Post: 28th August 2007, 17:04
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  4. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 22:55
  5. Qt 4.1 and KDE 3.5.1 on OSX 10.2.8
    By Ptero-4 in forum Installation and Deployment
    Replies: 6
    Last Post: 6th February 2006, 02:44

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.