Results 1 to 2 of 2

Thread: Signals and Slots between 2 classes

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Signals and Slots between 2 classes

    Class A has a QPushButton. Class B has a Slot called conectToServer().

    Class A has to send an string called name, when the button is pressed to the Slot in class B.

    Class A:
    Qt Code:
    1. string name = "hi";
    2. connect(btnLogin, SIGNAL(clicked()), classB, SLOT(connectToServer(name)));
    To copy to clipboard, switch view to plain text mode 

    Class B:
    Qt Code:
    1. public slots:
    2. void connectToServer(string);
    To copy to clipboard, switch view to plain text mode 

    When I use the same code, but I do not send any arguments in the call to connectToServer(), then everything is fine. Sending the string is not possible.

    I read the docs and it says that a SLOT is like an ordinary C++ function, so it should be able to accept arguments.

    Am I doing something wrong ?

  2. The following user says thank you to probine for this useful post:


  3. #2
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanked 42 Times in 37 Posts

    Default Re: Signals and Slots between 2 classes

    Quote Originally Posted by probine
    Class A has a QPushButton. Class B has a Slot called conectToServer().

    Class A has to send an string called name, when the button is pressed to the Slot in class B.

    Class A:
    Qt Code:
    1. string name = "hi";
    2. connect(btnLogin, SIGNAL(clicked()), classB, SLOT(connectToServer(name)));
    To copy to clipboard, switch view to plain text mode 

    Class B:
    Qt Code:
    1. public slots:
    2. void connectToServer(string);
    To copy to clipboard, switch view to plain text mode 

    When I use the same code, but I do not send any arguments in the call to connectToServer(), then everything is fine. Sending the string is not possible.

    I read the docs and it says that a SLOT is like an ordinary C++ function, so it should be able to accept arguments.

    Am I doing something wrong ?

    Read the docs again, you cannot do this:

    Qt Code:
    1. connect(btnLogin, SIGNAL(clicked()), classB, SLOT(connectToServer(name)))
    To copy to clipboard, switch view to plain text mode 

    Since a connect statment cannot contain parameters for the signals or slots. What you need to do is use an intermediate slot that will call your connectToServer() slot with the string parameter, or use a QSignalMapper, like this:

    Qt Code:
    1. #include <QSignalMapper>
    2. ...
    3.  
    4. QSignalmapper* mapper = new QSignalMapper(this);
    5. mapper->setMapping(btnLogin, QString("hi!"));
    6. connect(btnLogin, SIGNAL(clicked()), mapper, SLOT(map()))
    7. connect(mapper, SIGNAL(mapped(const QString&)), classB, SLOT(connectToServer(const QString&)))
    To copy to clipboard, switch view to plain text mode 
    Save yourself some pain. Learn C++ before learning Qt.

  4. The following user says thank you to Chicken Blood Machine for this useful post:


Similar Threads

  1. Signals and Slots Problem
    By GenericProdigy in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2009, 09:06
  2. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  3. Problem with SpinBox signals and slots
    By ramstormrage in forum Newbie
    Replies: 4
    Last Post: 2nd May 2008, 01:45
  4. signals and slots in plugins
    By anderl in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 13:57
  5. Signals and Slots in dll
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2006, 08:12

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
  •  
Qt is a trademark of The Qt Company.