Ok but moving the socket was not my point, I try that with your code :
main.c
#include <QtCore/QCoreApplication>
#include "clientsocket.h"
#include <QThread>
#include <QtDebug>
int main(int argc, char *argv[])
{
ClientSocket m_ClientSocket;
thread->start();
// now we got another thread
m_ClientSocket.moveToThread(thread);
qDebug() << "Main thread is " <<QThread::currentThreadId();
bool bConnected = m_ClientSocket.connectToHost("10.33.17.37", 8000);
qDebug() << "Connexion done";
if (bConnected)
{
qDebug() << "Ask for Id";
m_ClientSocket.write("*rem\n*idn?\n", 10);
}
// For testing don't do a exec() just wait for msec
// Without the processEvent() qDebug not displaying anything but
// the threadid prove the reception is done in the other thread
thread->wait(500); // After 500ms I should have the response or I give up.
a.processEvents();
thread->quit();
thread->wait(100);
return 0;
}
#include <QtCore/QCoreApplication>
#include "clientsocket.h"
#include <QThread>
#include <QtDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ClientSocket m_ClientSocket;
QThread *thread = new QThread(&a);
thread->start();
// now we got another thread
m_ClientSocket.moveToThread(thread);
qDebug() << "Main thread is " <<QThread::currentThreadId();
bool bConnected = m_ClientSocket.connectToHost("10.33.17.37", 8000);
qDebug() << "Connexion done";
if (bConnected)
{
qDebug() << "Ask for Id";
m_ClientSocket.write("*rem\n*idn?\n", 10);
}
// For testing don't do a exec() just wait for msec
// Without the processEvent() qDebug not displaying anything but
// the threadid prove the reception is done in the other thread
thread->wait(500); // After 500ms I should have the response or I give up.
a.processEvents();
thread->quit();
thread->wait(100);
return 0;
}
To copy to clipboard, switch view to plain text mode
clientsocket.h
#ifndef CLIENTSOCKET_H
#define CLIENTSOCKET_H
#include <QTcpSocket>
{
Q_OBJECT
public:
ClientSocket();
bool connectToHost
(QString host,
int port
);
void write(const char* Data,unsigned int nBytes);
//void run();
public slots:
void test();
private:
};
#endif // CLIENTSOCKET_H
#ifndef CLIENTSOCKET_H
#define CLIENTSOCKET_H
#include <QTcpSocket>
class ClientSocket: public QObject
{
Q_OBJECT
public:
ClientSocket();
bool connectToHost(QString host, int port);
void write(const char* Data,unsigned int nBytes);
//void run();
public slots:
void test();
private:
QTcpSocket* m_socket;
};
#endif // CLIENTSOCKET_H
To copy to clipboard, switch view to plain text mode
clientsocket.c
#include "clientsocket.h"
#include <QDebug>
#include <QThread>
ClientSocket
::ClientSocket() : QObject(){
connect(m_socket, SIGNAL(readyRead()), this, SLOT(test()), Qt::QueuedConnection);
}
bool ClientSocket
::connectToHost(QString host,
int port
) {
if (port == 0) return false;
m_socket->connectToHost(host, port);
return m_socket->waitForConnected(1000);
}
void ClientSocket::write(const char* Data,unsigned int nBytes)
{
{
m_socket->write(Data);
m_socket->flush();
}
}
void ClientSocket::test()
{
qDebug
() <<
"I got something here!!!" <<
QThread::currentThreadId() ;
}
#include "clientsocket.h"
#include <QDebug>
#include <QThread>
ClientSocket::ClientSocket() : QObject()
,m_socket(new QTcpSocket())
{
connect(m_socket, SIGNAL(readyRead()), this, SLOT(test()), Qt::QueuedConnection);
}
bool ClientSocket::connectToHost(QString host, int port)
{
if (port == 0) return false;
m_socket->connectToHost(host, port);
return m_socket->waitForConnected(1000);
}
void ClientSocket::write(const char* Data,unsigned int nBytes)
{
if (m_socket && (m_socket->state() == QAbstractSocket::ConnectedState))
{
m_socket->write(Data);
m_socket->flush();
}
}
void ClientSocket::test()
{
qDebug() << "I got something here!!!" << QThread::currentThreadId() ;
}
To copy to clipboard, switch view to plain text mode
This is the output:
Starting /disk2/local/qt/TestDebug/TestDebug...
Main thread is 3070105296
Connexion done
Ask for Id
I got something here!!! 3068005232
/disk2/local/qt/TestDebug/TestDebug exited with code 0
There is something strange with the qDebug in the second thread, it will display nothing until we call a processEvent() but the thread Id displayed in the readyRead connected slot proves that it's the second thread .
I don't know if you can use it like this in your app but It seems to work fine for me except that I can explain the strange behavior with the qDebug in the second thread.
(You have to take care of the cross thread call in a second time)
Hope it can help
Bookmarks