Results 1 to 4 of 4

Thread: passing arguments in SLOT

  1. #1
    Join Date
    Nov 2006
    Posts
    96

    Default passing arguments in SLOT

    Hi, I'm having problem passing arguments in SLOT:

    Qt Code:
    1. private slot:
    2. void connectToHost(QString, quint32);
    To copy to clipboard, switch view to plain text mode 

    and I'm calling it like this:

    Qt Code:
    1. connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToHost(host, port)));
    To copy to clipboard, switch view to plain text mode 

    while the connectToHost is:

    Qt Code:
    1. void MainWindow::connectToHost(QString host, quint32 port) {
    2. /* create and connect to the socket */
    3. socket = new QTcpSocket(this);
    4. socket->connectToHost(host, port, QIODevice::ReadOnly);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Problem is:

    Object::connect: No such slot MainWindow::connectToHost(host,port)

  2. #2
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: passing arguments in SLOT

    Hi,

    isn't it "private slots: ?

  3. #3
    Join Date
    Nov 2006
    Posts
    96

    Default Re: passing arguments in SLOT

    Yes it is...I make a mistake typing....

    I have slots in my code.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: passing arguments in SLOT

    You don't call a slot with connect.

    You create a connection to between a signal and a slot with connect.
    Every time that signal is emitted (with its arguments) the slots is called with those.

    If you want to call a 'slot', just do it. It is a regular function.

    When connecting you must not use names of some variables, but types:
    Qt Code:
    1. // does not quite work
    2. connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToHost(QString, quint32)));
    To copy to clipboard, switch view to plain text mode 

    This does not work either, as the slot's arguments must be those (or less) of the signal.
    clicked() has no arguments, so neither can the slot you connect to.

    Qt Code:
    1. // perhaps in the constructor:
    2. connect(connectButton, SIGNAL(clicked()), this, SLOT(doConnect()));
    3.  
    4. MainWindow::doConnect()
    5. {
    6. connectToHost(host, port);
    7. }
    To copy to clipboard, switch view to plain text mode 
    where host and port would be members of MainWindow, and doConnect a slot in MainWindow.
    connectToHost can be a slot, but does not have to, since you're only connecting to doConnect() here.

    HTH

    PS: Do read the excellent Qt docs and tutorials on the subject!

Similar Threads

  1. How to declare SLOT as a parameter to member function?
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2018, 00:41
  2. Problem When Creating my own Slot
    By Fatla in forum Qt Programming
    Replies: 12
    Last Post: 6th June 2008, 14:44
  3. Calling a slot with arguments
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 1st May 2008, 21:22
  4. Replies: 6
    Last Post: 21st September 2007, 13:51

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.