Results 1 to 7 of 7

Thread: Use QThread with Qbytearray parameter and return...+ QTCPSocket

  1. #1
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Use QThread with Qbytearray parameter and return...+ QTCPSocket

    Hi all!

    I am kind of new with qt and I am doing a more or less complex program (at least for me).

    I have a client server application that send and receives QByteArray packages and it works but it is not optimize. My boss told me that it should use the tcp members in a separated QThread because if the query takes too long the Gui part of the client kind of block until the QTcpSocket.waitforreadyread() is finished.

    I saw all the basic examples but I haven't seen any example with a QByteArray as parameter and that returns a QByteArray. I need to:

    QByteArray qba; //this qbytearray is compose with stuff like username,password from the GUI

    qba = MyThread(qba).start();

    I know that the syntax is totally wrong and this doesn't work but how can I send and get a qba that is processed in a QThread...and is this the best solution?

    Thank you all!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Use QThread with Qbytearray parameter and return...+ QTCPSocket

    Please don't use threads for this. It will make your coding more difficult (as you already see).

    Do use the readyRead() signal.

    Example:
    http://www.qtcentre.org/wiki/index.p...ithout_threads

  3. #3
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Use QThread with Qbytearray parameter and return...+ QTCPSocket

    Quote Originally Posted by tbscope View Post
    Please don't use threads for this. It will make your coding more difficult (as you already see).

    Do use the readyRead() signal.

    Example:
    http://www.qtcentre.org/wiki/index.p...ithout_threads
    Thanks for your reply!

    I saw, built and tested the example but it doesn't help me very much. Using the readyRead Signal was my first option but I need something that allows me to use the tcpsocket->write(qbytearray) and receive the processed QBA right after. We will have more then 100 functions and they all must use the same TCPclass.

    I will keep looking but just for the record, is it possible to call a QThread with parameters and expect to return a qbytearray?

    Thanks for your time!

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Use QThread with Qbytearray parameter and return...+ QTCPSocket

    Quote Originally Posted by ruben.rodrigues View Post
    Using the readyRead Signal was my first option but I need something that allows me to use the tcpsocket->write(qbytearray) and receive the processed QBA right after.
    Huh? Why is this not possible with the readyRead signal?
    I've done this hundreds of times.

    We will have more then 100 functions and they all must use the same TCPclass.
    Well, how is this a problem? If you only use one client, create a command queue. Else create a client for each command. Either way, there's absolutely no need to use threads.

    But if you insist on using threads, by my guest, I will not stop you.

    If your boss tells you that you NEED to use threads, nothing else, you can tell him I said he's an idiot.

  5. #5
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Use QThread with Qbytearray parameter and return...+ QTCPSocket

    Quote Originally Posted by tbscope View Post
    Huh? Why is this not possible with the readyRead signal?
    I've done this hundreds of times.


    Well, how is this a problem? If you only use one client, create a command queue. Else create a client for each command. Either way, there's absolutely no need to use threads.

    But if you insist on using threads, by my guest, I will not stop you.

    If your boss tells you that you NEED to use threads, nothing else, you can tell him I said he's an idiot.
    Calm down friend!!

    First of all, please notice that I have posted this in the newbie section of the forum. Second my boss is totally not an idiot as he is actually one of the most intelligent person I know and I don't want to insist in using the QThread I just asked if it was possible to send parameters and have returns in a QThread Class.

    I don't understand how I can return to the right function, the package that the class activated by the readyRead signal receives, because differently from that simple client/server application that you've recommended, the server will send packages that must be processed by 100 different functions. I'd appreciate some help to understand that.


    My code looks like this (logically):

    1-the client pushes the login button and the function request_load is started.
    2-that function will collect the lineEdit members (like user and password) and send them to the server.
    3-then the server returns all the data from the entered user and then the function is ready to process the received package.

    the way the code looks on the step 3 is like this:

    tcpClass->send(qb); //That qb (QByteArray) contains user information
    qb = tcpClass->receive(); //gets the user information to the requested user
    .... Use of the qb to load modules.

    As I mention there are a lot more function and they all need the tcpClass send and receive. I don't see how I can do this with the Signal readyRead but once again I am a beginner.
    Other thing is that I know how to short that 2 function in 1, and that was my goal with the QThread. Other advantage that I would see with the QThread is that if the time from the step 2 to the 3 is too long the user may get confused and crash the application. The software is intended to be used by people that undestand nothing about computers.

    I am sorry if I am not explaining my self well enough.

    Once again thanks for your time.

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Use QThread with Qbytearray parameter and return...+ QTCPSocket

    Quote Originally Posted by ruben.rodrigues View Post
    Calm down friend!!
    I'm perfectly calm.

    First of all, please notice that I have posted this in the newbie section of the forum. Second my boss is totally not an idiot as he is actually one of the most intelligent person I know and I don't want to insist in using the QThread I just asked if it was possible to send parameters and have returns in a QThread Class.
    Ok.

    I didn't give an answer to your question because I gave you a way for you not having to ask the question in the first place.

    I don't understand how I can return to the right function, the package that the class activated by the readyRead signal receives, because differently from that simple client/server application that you've recommended, the server will send packages that must be processed by 100 different functions. I'd appreciate some help to understand that.
    First, you need to explain yourself a lot better. These 100 different functions, what do they do? Does each function send a different command to the server? Or does each function do something with the received response?

    As I mention there are a lot more function and they all need the tcpClass send and receive. I don't see how I can do this with the Signal readyRead but once again I am a beginner.
    Like I already said, create a command queu and/or multiple clients. If you can't do anything before logging in, than implement this the correct way.

    Example:

    Qt Code:
    1. Suppose you have a subclassed TCPSocket that has a state flag.
    2. enum MyClientState
    3. {
    4. ClientStateIdle = 0,
    5. ClientStateLogIn = 1,
    6. ClientStateFunction2 = 2,
    7. ...
    8. }
    9.  
    10. void myClient::setFlag(MyClientState state);
    11. MyClientState myClient::flag() const;
    12.  
    13.  
    14. Function1 : logging in
    15. Use an available TCPSocket
    16. Set the flag of the socket to ClientStateLogIn
    17. Write a command with the correct username and password and then forget about it.
    18.  
    19. Function2: something else
    20. Use an available TCPSocket
    21. Set the flag of the socket to ClientStateFunction2
    22. write a command ... and forget about it.
    23.  
    24. slot readyRead
    25. Get to know the sender, as in: MyClientSocket *client = static_cast<MyClientSocket *>(sender())
    26. Or, alternatively, use a QSignalMapper
    27. Get the state of the socket
    28. If ClientStateLogIn
    29. Process the incoming data
    30. If multiple lines, check if you get everything (you need to provide a way to know when a response is complete like sending the size of the response too (like in http) or a control character.
    31. if ClientStatFunction2
    32. Process the incoming data
    To copy to clipboard, switch view to plain text mode 

    How hard can this be?

    And once again, feel free to use threads, and I think someone will answer your question about the threads.
    I still think you don't need them and I give you a solution that doesn't need them.

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Use QThread with Qbytearray parameter and return...+ QTCPSocket

    Here's a real life example
    It's work in progress, so it may contain some errors in handling the data.
    Attached Files Attached Files

Similar Threads

  1. Replies: 4
    Last Post: 2nd April 2010, 10:04
  2. Replies: 4
    Last Post: 1st February 2010, 14:21
  3. QTcpSocket QDataStream QByteArray
    By Grimlock in forum Newbie
    Replies: 1
    Last Post: 14th December 2009, 22:47
  4. Problem in printing the data return /read from QTcpsocket
    By skumar434 in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 19:36
  5. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2008, 17:43

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.