PDA

View Full Version : Use QThread with Qbytearray parameter and return...+ QTCPSocket



ruben.rodrigues
23rd June 2010, 08:39
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!

tbscope
23rd June 2010, 08:52
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.php?title=Server_with_multiple_clients_witho ut_threads

ruben.rodrigues
23rd June 2010, 10:01
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.php?title=Server_with_multiple_clients_witho ut_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!

tbscope
23rd June 2010, 10:08
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.

ruben.rodrigues
23rd June 2010, 10:38
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.

tbscope
23rd June 2010, 11:29
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:




Suppose you have a subclassed TCPSocket that has a state flag.
enum MyClientState
{
ClientStateIdle = 0,
ClientStateLogIn = 1,
ClientStateFunction2 = 2,
...
}

void myClient::setFlag(MyClientState state);
MyClientState myClient::flag() const;


Function1 : logging in
Use an available TCPSocket
Set the flag of the socket to ClientStateLogIn
Write a command with the correct username and password and then forget about it.

Function2: something else
Use an available TCPSocket
Set the flag of the socket to ClientStateFunction2
write a command ... and forget about it.

slot readyRead
Get to know the sender, as in: MyClientSocket *client = static_cast<MyClientSocket *>(sender())
Or, alternatively, use a QSignalMapper
Get the state of the socket
If ClientStateLogIn
Process the incoming data
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.
if ClientStatFunction2
Process the incoming data


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.

tbscope
23rd June 2010, 14:48
Here's a real life example
It's work in progress, so it may contain some errors in handling the data.