Results 1 to 6 of 6

Thread: slots with arguments

  1. #1
    Join Date
    Feb 2011
    Posts
    31
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default slots with arguments

    is this legal? I can compile but i wonder if this is bad practice or, even worse, suited for run time bugs
    Qt Code:
    1. connect(mTcpSocket, SIGNAL(connected()), this, SLOT(sendMsg(mTcpSocket)));
    2.  
    3. connect(pTcpSocket, SIGNAL(connected()), this, SLOT(sendMsg(pTcpSocket)));
    4. ...
    5. void ecmqMfgClient::sendMsg(QTcpSocket *tcpSocket)
    6. {
    7. QByteArray block;
    8. QDataStream out(&block, QIODevice::WriteOnly);
    9. out.setVersion(QDataStream::Qt_4_1);
    10. out << this->Cli2SvrMsg.dutId;
    11. out << this->Cli2SvrMsg.opCode;
    12. out.device()->seek(0);
    13. tcpSocket->write(block);
    14. tcpSocket->waitForBytesWritten();
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 
    i.e. can i associate to the same signal of two different QTcpSocket to the same slot but with different parameters (the target socket but also others)?
    QTcpSocket is event driven so i do not see any danger for concurrency, right?

    not sure this is really a newbie question, hope to get less severe reply from somebody ;-)

    thanks much
    Last edited by marco.stanzani; 23rd March 2011 at 11:29.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: slots with arguments

    is this legal?
    No.
    See http://doc.qt.nokia.com/latest/signalsandslots.html for details.
    can i associate to the same signal of two different QTcpSocket to the same slot?
    Yes you can.
    QTcpSocket is event driven so i do not see any danger for concurrency, right?
    Huh?
    Explain what you mean.

    not sure this is really a newbie question,
    Yes it is.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    marco.stanzani (23rd March 2011)

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

    Default Re: slots with arguments

    Quote Originally Posted by marco.stanzani View Post
    QTcpSocket is event driven so i do not see any danger for concurrency, right?
    This has nothing to do with event-driven architecture. Your slot has to be reentrant if you wish to call it from different threads. If you wish to call it from the same thread, unless you enter the event loop inside the slot, it doesn't even have to be reentrant.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    marco.stanzani (24th March 2011)

  6. #4
    Join Date
    Feb 2011
    Posts
    31
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slots with arguments

    Quote Originally Posted by high_flyer View Post
    but i see in the posted URL there are slots with arguments (i cannot see the connect in this example, though)
    Qt Code:
    1. public slots:
    2. void display(int num);
    3. void display(double num);
    4. void display(const QString &str);
    To copy to clipboard, switch view to plain text mode 
    so what's wrong in my code exactly (suppsoed that sendMsg is re-entrant?)
    thanks

  7. #5
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slots with arguments

    slots can have arguments, of course. But you cannot set these arguments by yourself in the connect-statement. the arguments must match the signature of the connected signal. you can do this:

    Qt Code:
    1. connect(objectA,SIGNAL(somethingHappended(const QString&)), objectB, SLOT(doSomething(const QString&)));
    To copy to clipboard, switch view to plain text mode 

    in this case, the signal "somethingHappended" has an argument which is automatically passed to "doSomething".

  8. The following user says thank you to FelixB for this useful post:

    marco.stanzani (24th March 2011)

  9. #6
    Join Date
    Feb 2011
    Posts
    31
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slots with arguments

    this is crystal-clear! thanks much!
    indeed it ws not clear to me from the URL what 'signature' means.

    it looks like i can achieve the goal by using the QObject::sender method to discern which emitter caused the slot invocation
    Qt Code:
    1. connect(mTcpSocket, SIGNAL(connected()), this, SLOT(sendMsg());
    2. connect(pTcpSocket, SIGNAL(connected()), this, SLOT(sendMsg());
    3. ...
    4. void myClass::sendMsg()
    5. {
    6. QByteArray block;
    7. QDataStream out(&block, QIODevice::WriteOnly);
    8. out.setVersion(QDataStream::Qt_4_1);
    9. out << this->Cli2SvrMsg.dutId;
    10.  
    11. if(sender== pTcpSocket)
    12. out<<this->Cli2SvrMsg.opCode=P_OPCODE:
    13. if(sender== mTcpSocket)
    14. out<<this->Cli2SvrMsg.opCode=M_OPCODE:
    15.  
    16. sender()->write(block);
    17. sender()->waitForBytesWritten();
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    this will infringe 'the object-oriented principle of modularity. However, getting access to the sender might be useful when many signals are connected to a single slot." according to http://doc.qt.nokia.com/latest/qobject.html#sender, which i don care since i am on the top level of my application
    i also this Warning "... the return value of this function is not valid when the slot is called via a Qt:irectConnection from a thread different from this object's thread. Do not use this function in this type of scenario." but i think this si not the case (it is autoconnection)
    ciao
    Last edited by marco.stanzani; 24th March 2011 at 15:57.

Similar Threads

  1. QApplication Arguments
    By ToddAtWSU in forum Qt Programming
    Replies: 0
    Last Post: 27th January 2011, 16:34
  2. how to use arguments in Qt?
    By mr_kazoodle in forum Newbie
    Replies: 5
    Last Post: 26th January 2011, 20:04
  3. QProcess Arguments
    By doggrant in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2010, 16:45
  4. QVector arguments
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 4th December 2007, 10:47
  5. Replies: 1
    Last Post: 6th April 2006, 12:24

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.