All I want to do is create a websocket client connection using Qt on a thread. Create a processing thread,so that when data comes to the websocket handler the data is passed(through slots and signals) to the processor and then the processor processes the data and passes it to the main-thread(again via signals and slots) to render the data.All I am doing this because I want to keep the main thread ublocked because processing and websocket data would take lot of time. First of all,is this the right approach?
Second:when I try to run QWebsockets on a different thread I am getting the error .
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QWebSocket(0x1a493f30), parent's thread is QThread(0x187fc0e8), current thread is QThread(0xe3fe08)
Not sure where to include or put this
(Make sure 'QAbstractSocket::SocketState' is registered using qRegisterMetaType().)
in my example. The code which I am using are.
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
QUrl url
("ws://localhost:8080");
websocketObj=new websocketclientimpl(url);
websocketObj->moveToThread(&workerThread_websocket);
connect(&workerThread_websocket, &QThread::started, websocketObj, &websocketclientimpl::startConnection);
workerThread_websocket.start();
}
MainWindow::~MainWindow()
{
delete ui;
workerThread_websocket.quit();
workerThread_websocket.wait();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QUrl url("ws://localhost:8080");
websocketObj=new websocketclientimpl(url);
websocketObj->moveToThread(&workerThread_websocket);
connect(&workerThread_websocket, &QThread::started, websocketObj, &websocketclientimpl::startConnection);
workerThread_websocket.start();
QMainWindow::showMaximized();
}
MainWindow::~MainWindow()
{
delete ui;
workerThread_websocket.quit();
workerThread_websocket.wait();
}
To copy to clipboard, switch view to plain text mode
websocketclientimpl
::websocketclientimpl(const QUrl &url
){this->m_url=url;
}
void websocketclientimpl::startConnection()
{
qDebug() << "WebSocket client:" << m_url;
connect(&m_webSocket, &QWebSocket::connected, this, &websocketclientimpl::onConnected);
connect(&m_webSocket, &QWebSocket::disconnected, this, &websocketclientimpl::onClosed);
QNetworkRequest request
=QNetworkRequest
(QUrl(m_url
));
m_webSocket.open(request);
}
void websocketclientimpl::stopConnection(){
m_webSocket.disconnect();
qDebug() << "WebSocket stopped";
}
void websocketclientimpl::onConnected()
{
qDebug() << "WebSocket connected..";
}
void websocketclientimpl::onClosed()
{
}
void websocketclientimpl
::onTextMessageReceived(QString message
) {
qDebug() << "message received in websocket class"<<message;
emit processIncomingMessage(message);
}
websocketclientimpl::websocketclientimpl(const QUrl &url){
this->m_url=url;
}
void websocketclientimpl::startConnection()
{
qDebug() << "WebSocket client:" << m_url;
connect(&m_webSocket, &QWebSocket::connected, this, &websocketclientimpl::onConnected);
connect(&m_webSocket, &QWebSocket::disconnected, this, &websocketclientimpl::onClosed);
QNetworkRequest request=QNetworkRequest(QUrl(m_url));
m_webSocket.open(request);
}
void websocketclientimpl::stopConnection(){
m_webSocket.disconnect();
qDebug() << "WebSocket stopped";
}
void websocketclientimpl::onConnected()
{
qDebug() << "WebSocket connected..";
}
void websocketclientimpl::onClosed()
{
}
void websocketclientimpl::onTextMessageReceived(QString message)
{
qDebug() << "message received in websocket class"<<message;
emit processIncomingMessage(message);
}
To copy to clipboard, switch view to plain text mode
using namespace std;
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
websocketclientimpl *websocketObj;
calculation *calculationObj;
};
using namespace std;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
websocketclientimpl *websocketObj;
calculation *calculationObj;
QThread workerThread_websocket;
QThread workerThread_calculator;
};
To copy to clipboard, switch view to plain text mode
class websocketclientimpl
: public QObject{
Q_OBJECT
public:
QWebSocket m_webSocket;
websocketclientimpl
(const QUrl &url
);
void startConnection();
void stopConnection();
private Q_SLOTS:
void onConnected();
void onClosed();
void onTextMessageReceived
(QString message
);
Q_SIGNALS:
void processIncomingMessage
(QString message
);
private:
};
class websocketclientimpl : public QObject
{
Q_OBJECT
public:
QWebSocket m_webSocket;
websocketclientimpl(const QUrl &url);
void startConnection();
void stopConnection();
private Q_SLOTS:
void onConnected();
void onClosed();
void onTextMessageReceived(QString message);
Q_SIGNALS:
void processIncomingMessage(QString message);
private:
QUrl m_url;
};
To copy to clipboard, switch view to plain text mode
Can anyone please help me out here? Stuck very badly!
Bookmarks