Results 1 to 4 of 4

Thread: readyRead slot - socket->readAll() data needed in separate class

  1. #1
    Join Date
    Dec 2012
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default readyRead slot - socket->readAll() data needed in separate class

    I've made a client which can connect to a server, and it sends information to the server when a key is pressed (it's not a keylogger ), and the data that gets sent back to the client with the readyRead slot is needed in a different class than the dialog class. Is there any simple way that I can move the data from one class to another without passing the ui as a parameter through lots of functions (and possibly causing lag)?

    I have 2 classes: Dialog and clientSocket. The dialog class has a private pointer to a clientSocket object. In the dialog class, I have overridden the keyPressEvent function to send the key presses to the server, which sends back a number in the clientSocket class. This is what I need in the dialog class.

    keyPressEvent function in dialog.cpp:

    Qt Code:
    1. void Dialog::keyPressEvent(QKeyEvent *event){
    2. if(socketTest->connectedToClient()){ //if connected, let the server handle input.
    3. char a[1];
    4. sprintf(a, "%c", event->key());
    5. socketTest->socket->write(a); //socketTest is a pointer to a clientSocket object, socket is a QTcpSocket in the clientSocket class
    6. }
    7. //stuff
    To copy to clipboard, switch view to plain text mode 

    Server sends back "0", which is picked up in the readyRead function

    Qt Code:
    1. void clientSocket::readyRead(){
    2. QByteArray data = socket->readAll();
    3. //what I need to do here is call a function from the dialog class
    4. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: readyRead slot - socket->readAll() data needed in separate class

    Qt Code:
    1. Dialog::Dialog()
    2. {
    3. socketTest = ...;
    4. connect(socketTest, signalX(QByteArray), this, slotABC(QByteArray));
    5. }
    6.  
    7. void clientSocket::readyRead(){
    8. QByteArray data = socket->readAll();
    9. //what I need to do here is call a function from the dialog class
    10.  
    11. emit signalX(data);
    12. }
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    ben1996123 (9th December 2012)

  4. #3
    Join Date
    Dec 2012
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: readyRead slot - socket->readAll() data needed in separate class

    Quote Originally Posted by amleto View Post
    code
    Thanks, I completely forgot about signals and slots because I'm pretty new to Qt

  5. #4
    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: readyRead slot - socket->readAll() data needed in separate class

    It might be an idea to do the socket stuff outside the dialog, maybe in the class that actually handles the response.

    The dialog could just emit a signal with the relevant input and be totally independent of what your program does with it.

    Makes it easier to test since you can use a different class for generating the input, e.g. something that reads input from a test file, etc.

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 1st August 2011, 13:06
  2. separate class using an interface
    By qt_gotcha in forum Newbie
    Replies: 7
    Last Post: 3rd March 2010, 21:54
  3. loss of data recieved using http->readAll()
    By arunredi in forum Qt Programming
    Replies: 5
    Last Post: 26th June 2008, 17:12
  4. separate Qt class diagram
    By juanrb in forum Qt Programming
    Replies: 2
    Last Post: 14th April 2008, 19:55
  5. Replies: 1
    Last Post: 1st November 2007, 14:09

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.