how to operate tcpsocket in thread again
I establish a multi-thread server,but encouter some problems,after declare tcpsocket in thread::run,I want to operate the tcpsocket in slot ,such as write or read,but i couldnot.
how to resolve this problem,forgive my poor english,3x
the thread.h as follows
Code:
class FortuneThread
: public QThread {
Q_OBJECT
public:
FortuneThread
(int socketDescriptor,
QObject *parent
);
void run();
signals:
private:
int socketDescriptor;
bool stopped;
private slots:
void recfserver
(const QString peeradd
);
};
the thread.cpp as follows
Code:
FortuneThread
::FortuneThread(int socketDescriptor,
const QString &fortune,
: QThread(parent
), socketDescriptor
(socketDescriptor
) {
};
void FortuneThread::run()
{
if (!((*tcpSocket).setSocketDescriptor(socketDescriptor))) {
emit error((*tcpSocket).error());
return;
}
(*tcpSocket).waitForDisconnected();
}
void FortuneThread
::recfserver(const QString peeradd
) {
QString text
=tr
("transfer success!");
out << (quint16)0;
out << text;
(*tcpSocket).write(block);
};
when i debug,the error as follows
QObject cannot create children for a parent that is in a different thread.
(parent is QNativeSocketEngine,parent's thread is FortuneThread,current thread
is QThread)
Re: how to operate tcpsocket in thread again
i) use [code] tags
ii) with Qt signals & slots and you do not really need to put that code into a separate thread
iii) if using QThread: put your code into a separate class (not the QThread subclass!) and create that in your thread's run() function. You should not add slots/signals to the QThread subclass itself. There are some postings that elaborate on that. Search for them, please.
(The QThread belongs to the thread that created it, not to the thread it is executing itself.)
HTH
Re: how to operate tcpsocket in thread again
Quote:
Originally Posted by
caduel
i) use [code] tags
ii) with Qt signals & slots and you do not really need to put that code into a separate thread
iii) if using QThread: put your code into a separate class (not the QThread subclass!) and create that in your thread's run() function. You should not add slots/signals to the QThread subclass itself. There are some postings that elaborate on that. Search for them, please.
(The QThread belongs to the thread that created it, not to the thread it is executing itself.)
HTH
thanks for your help,but i think i don't explain my desigh explicility
firstly,I want to create a thread,and creat a socket in the thread for some client's connection.
secondly,I want the gui-mainthread to notify the created thread to send some message to the client..So I use signal/slot for it,just like i clicked a button,then emit a signal which send by gui to the created thread,it is a blocked operation.
now the slot recfserveris useful for accept the signal,then i want to operate tcpsocket in the slot.can i into the run for the operation?
could you tell me what other methods which can realize the function
thank you very much!
Re: how to operate tcpsocket in thread again
Quote:
Originally Posted by
succulent_lily
thanks for your help,but i think i don't explain my desigh explicility
firstly,I want to create a thread,and creat a socket in the thread for some client's connection.
secondly,I want the gui-mainthread to notify the created thread to send some message to the client..So I use signal/slot for it,just like i clicked a button,then emit a signal which send by gui to the created thread,it is a blocked operation.
now the slot recfserveris useful for accept the signal,then i want to operate tcpsocket in the slot.can i into the run for the operation?
could you tell me what other methods which can realize the function
thank you very much!
i subclass Qtcpsocket,and transfer the signal to the thread ,then to the socket,the problem is solved ,thanks for your help