How to get a variable from a thread? - c++

I have a class that creates a server with multithreading. With the help of multithreading, I can connect several users at the same time. In one stream, the QByteArray Data is written, but I need this variable in the lane or in the lane heder, so that I can output this line (i.e., QByteArray Data). How I can tranfer var Data in my main function or header main?
I want write Data in QLable or somthing...
main.c
#include "groundstation.h"
#include <QApplication>
#include "server.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GroundStation w;
Server wServer;
wServer.StartServer();
//qDebug() << ServerThread::getString();
// QLabel *label = new QLabel();
//label->setText((QString));
w.show();
return a.exec();
}
server.cpp
#include "server.h"
Server::Server(QObject *parent) :
QTcpServer (parent)
{
}
void Server::StartServer(){
if(!this->listen(QHostAddress::Any, 1234)){
qDebug() << "Could not start server";
}
else {
qDebug() << "Listening...";
}
}
void Server::incomingConnection(int socketDescriptor){
qDebug() << socketDescriptor << "Connecting...";
ServerThread *thread = new ServerThread(socketDescriptor, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
sever.h
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
#include <QDebug>
#include "serverthread.h"
class Server : public QTcpServer
{
Q_OBJECT
public:
explicit Server(QObject *parent = 0);
void StartServer();
signals:
public slots:
protected:
void incomingConnection(int socketDescriptor);
};
#endif // SERVER_H
serverthread.cpp
#include "serverthread.h"
ServerThread::ServerThread(int id, QObject *parent) :
QThread (parent)
{
this->socketDescriptor = id;
}
void ServerThread::run(){
//thread starts here
qDebug() << socketDescriptor << " Starting thread";
socket = new QTcpSocket();
if(!socket->setSocketDescriptor(this->socketDescriptor)){
emit error(socket->error());
return;
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);
qDebug() << socketDescriptor << " Client connected";
//make this thread a loop
exec();
}
void ServerThread::readyRead(){
Data = socket->readAll();
qDebug() << socketDescriptor << " Data in: " << Data;
socket->write(Data);
}
void ServerThread::disconnected(){
qDebug() << socketDescriptor << " Disconnected";
socket->deleteLater();
exit(0);
}
serverthread.h
#ifndef SERVERTHREAD_H
#define SERVERTHREAD_H
#include <QThread>
#include <QTcpSocket>
#include <QDebug>
class ServerThread : public QThread
{
Q_OBJECT
public:
QByteArray Data;
explicit ServerThread(int id, QObject *parent = 0);
void run();
signals:
void error(QTcpSocket::SocketError socketerror);
public slots:
void readyRead();
void disconnected();
public slots:
private:
QTcpSocket *socket;
int socketDescriptor;
};
#endif // SERVERTHREAD_H

Related

I have created a TCPSocket in QT, but how do i implement received data from QTCPSocket?

As the title says, i have created a client which receives data from my server. So far i can print it in my console, but i would like to work with it. (The data that the client recevies is from my Smartphones accelerometer sensor)
My Socket.h class:
#ifndef SOCKETTEST_H
#define SOCKETTEST_H
#include <QObject>
#include <QDebug>
#include <QTcpSocket>
#include <QAbstractSocket>
class SocketTest : public QObject
{
Q_OBJECT
public:
explicit SocketTest(QObject *parent = nullptr);
void Test();
signals:
public slots:
void connected();
void disconnected();
void bytesWritten( qint64 bytes);
void readyRead();
private:
QTcpSocket *socket;
};
#endif // SOCKETTEST_H
and socket.cpp :
#include "sockettest.h"
SocketTest::SocketTest(QObject *parent)
: QObject{parent}
{
}
void SocketTest::Test()
{
socket = new QTcpSocket(this);
connect (socket, SIGNAL(connected()),this, SLOT(connected()));
connect (socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
connect (socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
connect (socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
qDebug() <<"Connecting...";
socket->connectToHost("MY IP", PORT);
if(!socket->waitForConnected(2000))
{
qDebug() <<"Error" << socket->errorString();
}
}
void SocketTest::connected()
{
qDebug() <<"Connected!";
socket->write("\r\n\r\n\r\n\r\n");
}
void SocketTest::disconnected()
{
qDebug() <<"Disconnected!";
}
void SocketTest::bytesWritten(qint64 bytes)
{
qDebug() <<"We wrote: " <<bytes;
}
void SocketTest::readyRead()
{
qDebug() << socket->readAll();
}
typically what i want to create, in another class, is an if statment which moves an Object based on the sent data :
if( received_data == 5 ){
this->setX(this->x()+1);
}

Pass signals and data from a class to another on Qt

I will post the code first as it will be easier to understand after that.
Basically, I have a default class, MainWindow:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include "tcpsocket.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
//This is my class
tcpsocket *socket;
public slots:
void updateUI(QString);
private slots:
void on_connectButton_clicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updateUI(QString msg)
{
qDebug() << "GOOD, SLOT ACCESSED FROM ANOTHER CLASS";
ui->responseBox->setText(msg);
}
void MainWindow::on_connectButton_clicked()
{
socket = new tcpsocket();
socket->doConnect();
}
Behind is a simple form with a pushButton and a textBox.
And the second class:
tcpsocket.h
#ifndef TCPSOCKET_H
#define TCPSOCKET_H
#include <QMainWindow>
#include <QObject>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QBitArray>
#include <QDebug>
#include "mainwindow.h"
class tcpsocket : public QMainWindow
{
Q_OBJECT
public:
explicit tcpsocket(QWidget *parent = 0);
void doConnect();
signals:
public slots:
void connected();
void disconnected();
void bytesWritten(qint64 bytes);
void readyRead();
private:
QTcpSocket *socket;
};
#endif // TCPSOCKET_H
and tcpsocket.cpp
#include "tcpsocket.h"
tcpsocket::tcpsocket(QWidget *parent) : QMainWindow(parent)
{
}
void tcpsocket::doConnect()
{
socket = new QTcpSocket(this);
connect(socket, SIGNAL(connected()),this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
connect(socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
//And here I want to add a signal to a slor from main window.
//Something like this:
// connect(socket, SIGNAL(readyRead()), MainWindow, SLOT(updateUI());
qDebug() << "connecting...";
// this is not blocking call
socket->connectToHost("google.com", 80);
// we need to wait...
if(!socket->waitForConnected(5000))
{
qDebug() << "Error: " << socket->errorString();
}
}
void tcpsocket::connected()
{
qDebug() << "connected...";
// Hey server, tell me about you.
socket->write("HEAD / HTTP/1.0\r\n\r\n\r\n\r\n");
}
void tcpsocket::disconnected()
{
qDebug() << "disconnected...";
}
void tcpsocket::bytesWritten(qint64 bytes)
{
qDebug() << bytes << " bytes written...";
}
void tcpsocket::readyRead()
{
qDebug() << "reading response...";
QString data = socket->readAll();
qDebug() << data;
//Here, I want to pass somehow this content to function updateUI, from MainWindow
// MainWindow::updateUI( data );
}
Bsically, I want to pass some data from class tcpsocket to class MainWindow
main.cpp is default code. I think it is not relevant.
To be more specific, I want that a signal from tcpsocket class to trigger a slot from MainWindow class:
void tcpsocket::doConnect()
{
socket = new QTcpSocket(this);
//local slots
connect(socket, SIGNAL(connected()),this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
connect(socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
//And here I want to add a signal to a slot from main window.
//Something like this:
// connect(socket, SIGNAL(readyRead()), MainWindow, SLOT(updateUI());
....
And the second question is how to pass some data from tcpsocket class to MainWindow class:
void tcpsocket::readyRead()
{
qDebug() << "reading response...";
QString data = socket->readAll();
qDebug() << data;
//Here, I want to pass somehow this content to function updateUI, from MainWindow
// MainWindow::updateUI( data );
}
The application run perfectly and content is fetched. But I can view the fetched content only using qDebug(); so I want to pass response to GUI, into a textBox.

how to send an audio stream via tcp in a thread in Qt?

I'm trying to send an audio stream from a microphone via TCP in a thread other than the main thread. Therefore I've set up a server an I'm listening for incoming connections. Once a client connects, I want to send it the audio stream. I've read the Qt Documentation about QTcpServer, Multithreading, and QtMultimedia. I've also looked at this website for example code about multithreading and TCP servers in Qt. The problem is this message that I get during runtime:
QObject: Cannot create children for a parent that is in a different thread. (Parent is QNativeSocketEngine(0xae802a78), parent's thread is MyThread1(0x1856d28), current thread is QThread(0x1838aa0)
this is my code:
in file myserver.h
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QtNetwork>
#include <QTcpServer>
#include <mythread.h>
#include <QtMultimediaKit/QAudioFormat>
#include <QtMultimediaKit/QAudioInput>
#include <QtMultimediaKit/QAudioDeviceInfo>
class MyServer : public QTcpServer
{
Q_OBJECT
public:
explicit MyServer(QObject *parent = 0);
void startServer();
signals:
public slots:
protected:
void incomingConnection(int socketDescriptor);
private:
QAudioInput *audioInput;
};
#endif // MYSERVER_H
in file myserver.cpp
#include "myserver.h"
MyServer::MyServer(QObject *parent) :
QTcpServer(parent)
{
}
void MyServer::startServer()
{
int port = 12345;
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
for (int i = 0; i < ipAddressesList.size(); ++i)
{
if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address())
{
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
if(!this->listen( QHostAddress(ipAddress),port ) )
{
qDebug() << "Could not start server";
}
else
{
qDebug() << "The server is running on\n\nIP: "<< ipAddress;
qDebug() << "\nport: " << this->serverPort() << "\n\n";
}
}
void MyServer::incomingConnection(int socketDescriptor)
{
qDebug() << socketDescriptor << " Connecting...";
QAudioFormat format;
format.setSampleRate(44100);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format))
{
qWarning()<<"default format not supported try to use nearest";
format = info.nearestFormat(format);
}
audioInput = new QAudioInput(format, this);
MyThread1 *thread = new MyThread1(socketDescriptor,audioInput, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
in file mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QTcpSocket>
#include <QDebug>
#include <QtMultimediaKit/QAudioInput>
class MyThread1 : public QThread
{
Q_OBJECT
public:
explicit MyThread1( int ID, QAudioInput *spk, QObject *parent = 0);
void run();
signals:
void error(QTcpSocket::SocketError socketerror);
public slots:
void sendData();
void disconnected();
private:
QTcpSocket *socket;
int socketDescriptor;
QAudioInput *speaker;
};
#endif // MYTHREAD_H
in file mythread.cpp
#include "mythread.h"
MyThread1::MyThread1( int ID, QAudioInput *spk, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = ID;
this->speaker = spk;
}
void MyThread1::run()
{
qDebug() << " Thread started";
socket = new QTcpSocket();
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
emit error(socket->error());
return;
}
connect( speaker->start(), SIGNAL( readyRead() ), this, SLOT( sendData() ) , Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
qDebug() << socketDescriptor << " Microphone connected";
exec();
}
void MyThread1::sendData()
{
QByteArray Data = speaker->start()->readAll();
QDataStream l_vStream(&Data, QIODevice::WriteOnly);
l_vStream.setByteOrder(QDataStream::LittleEndian);
socket->write(Data, Data.length());
socket->waitForBytesWritten();
}
void MyThread1::disconnected( )
{
qDebug() << socketDescriptor << "Mic Disconnected";
speaker->stop();
delete speaker;
socket->deleteLater();
exit(0);
}
in file main.cpp
#include <QCoreApplication>
#include <myserver.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyServer server;
server.startServer();
return a.exec();
}
Any help would really be appreciated. Thanks.

Qt - Simple example using threads controlled by push buttons

I have been trying to get this simple example using threads activated by pushbuttons to work. It is based off of the solution in the question below:
How to implement frequent start/stop of a thread (QThread)
The main differences between the example solution above and my code below are:
I used a QWidget instead of MainWindow
I changed the name of signals for clarity
My code contains debugging information
I experimented with eliminating the signals created by worker as the didn't appear to do anything
It appears that the start/stop signals are not triggering their corresponding slots, but I am not experienced enough to troubleshoot why.
Additionally, I am unsure of the purpose of the signal:
SignalToObj_mainThreadGUI()
Is that just something that could be used and is not?
I have been trying to get this code to work for some time, so any help would be greatly appreciated.
main.cpp
#include "threadtest.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ThreadTest w;
w.show();
return a.exec();
}
threadtest.h
#include <QWidget>
#include <QThread>
#include "worker.h"
namespace Ui
{
class ThreadTest;
}
class ThreadTest : public QWidget
{
Q_OBJECT
public:
explicit ThreadTest(QWidget *parent = 0);
~ThreadTest();
signals:
void startWorkSignal();
void stopWorkSignal();
private slots:
void on_startButton_clicked();
void on_stopButton_clicked();
private:
Ui::ThreadTest *ui;
worker *myWorker;
QThread *WorkerThread;
};
threadtest.cpp
#include "threadtest.h"
#include "ui_threadtest.h"
ThreadTest::ThreadTest(QWidget *parent) :
QWidget(parent),
ui(new Ui::ThreadTest)
{
ui->setupUi(this);
myWorker = new worker;
WorkerThread = new QThread;
myWorker->moveToThread(WorkerThread);
connect(this,
SIGNAL(startWorkSignal()),
myWorker,
SLOT(StartWork())
);
connect(this,
SIGNAL(stopWorkSignal()),
myWorker,
SLOT(StopWork())
);
//Debug
this->dumpObjectInfo();
myWorker->dumpObjectInfo();
}
ThreadTest::~ThreadTest()
{
delete ui;
}
void ThreadTest::on_startButton_clicked()
{
qDebug() << "startwork signal emmitted";
emit startWorkSignal();
}
void ThreadTest::on_stopButton_clicked()
{
qDebug() << "stopwork signal emmitted";
emit stopWorkSignal();
}
worker.h
#include <QObject>
#include <QDebug>
class worker : public QObject {
Q_OBJECT
public:
explicit worker(QObject *parent = 0);
~worker();
signals:
void SignalToObj_mainThreadGUI();
//void running();
//void stopped();
public slots:
void StopWork();
void StartWork();
private slots:
void do_Work();
private:
volatile bool running, stopped;
};
worker.cpp
#include "worker.h"
worker::worker(QObject *parent) : QObject(parent), stopped(false),
running(false)
{
qDebug() << "running: " << running;
qDebug() << "stopped: " << stopped;
}
worker::~worker() {}
void worker::do_Work()
{
qDebug() << "inside do Work";
emit SignalToObj_mainThreadGUI();
if (!running || stopped) return;
// actual work here
/*
for (int i = 0; i < 100; i++)
{
qDebug() << "count: " + i;
}
*/
QMetaObject::invokeMethod(this, "do_Work", Qt::QueuedConnection);
}
void worker::StopWork()
{
qDebug() << "inside StopWork";
stopped = true;
running = false;
//emit stopped();
}
void worker::StartWork()
{
qDebug() << "inside StartWork";
stopped = false;
running = true;
//emit running();
do_Work();
}
You should write
WorkerThread->start();
Or you can use the thread of the ThreadTest object instead the WorkerThread (in this case the WorkerThread is needless):
myWorker->moveToThread(thread()); // this->thread
The slots are not triggered, because you have moved myWork to the thread WorkerThread, but didnot run an event loop in that thread. In threadtest.cpp, add
WorkerThread .start();
after
myWorker = new worker;
WorkerThread = new QThread;
myWorker->moveToThread(WorkerThread);

QThread - Call a method of another thread

I'm trying to call a method of another thread (not the Main), using invokeMethod. Unfortunately that makes the application crash as soon as it tries to execute the invokeMethod!
Am I mistaking something?
// main.cpp
#include <QtCore>
#include "entrypointclass.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << a.thread()->currentThreadId() << " - Application started.";
EntryPointClass entryPoint;
entryPoint.runInNewThread();
return a.exec();
}
// Entrypoint.h
#ifndef ENTRYPOINTCLASS_H
#define ENTRYPOINTCLASS_H
#include "worker.h"
#include <QtCore>
class EntryPointClass : public QObject
{
Q_OBJECT
public:
EntryPointClass();
~EntryPointClass();
void runInNewThread();
public slots:
void timeoutExpired();
private:
Worker* m_Worker;
QThread* m_Thread;
};
#endif // ENTRYPOINTCLASS_H
// Entrypoint.cpp
#include <QTCore>
#include "entrypointclass.h"
#include "Worker.h"
EntryPointClass::EntryPointClass()
{
qDebug() << "EntryPointClass created";
}
EntryPointClass::~EntryPointClass()
{
qDebug() << "EntryPointClass destroyed";
}
void EntryPointClass::runInNewThread()
{
QThread* m_Thread = new QThread;
Worker* m_Worker = new Worker();
connect(m_Thread, SIGNAL(started()), m_Worker, SLOT(doSomething()));
connect(m_Worker, SIGNAL(finished()), m_Thread, SLOT(quit()));
connect(m_Thread, SIGNAL(finished()), m_Thread, SLOT(deleteLater()));
connect(m_Thread, SIGNAL(finished()), m_Worker, SLOT(deleteLater()));
QTimer* timer = new QTimer;
timer->setSingleShot(true);
//bool bOK = connect(timer, SIGNAL(timeout()), m_Worker, SLOT(closeWorker()), Qt::BlockingQueuedConnection);
connect(timer, SIGNAL(timeout()), this, SLOT(timeoutExpired()));
m_Worker->moveToThread(m_Thread);
m_Thread->start();
timer->start(5000);
}
void EntryPointClass::timeoutExpired()
{
qDebug() << "timeout expired";
if (m_Worker != NULL)
{
QMetaObject::invokeMethod(m_Worker, "closeWorker", Qt::QueuedConnection);
}
}
// Worker.h
#ifndef WORKER_H
#define WORKER_H
#include <QtCore>
class Worker : public QObject
{
Q_OBJECT
public:
Worker();
~Worker();
public slots:
void doSomething();
void closeWorker();
private:
bool m_bAbort;
QMutex m_mutex;
signals:
void finished();
};
#endif // WORKER_H
// Worker.cpp
#include "worker.h"
#include <unistd.h>
#include "QTcore"
Worker::Worker()
: m_mutex()
{
qDebug() << this->thread()->currentThreadId() << "Worker created";
m_bAbort = false;
//qDebug() << QString("Thread %1 - Worker created").arg("");//this->thread()->currentThreadId());
}
Worker::~Worker()
{
qDebug() << this->thread()->currentThreadId() << "Worker destroyed";
}
void Worker::doSomething()
{
while(!m_bAbort)
{
sleep(2);
qDebug() << this->thread()->currentThreadId() << "Do Something!";
}
emit finished();
}
void Worker::closeWorker()
{
qDebug() << this->thread()->currentThreadId() << "closeWorker triggered!";
QMutexLocker mutexLocker(&m_mutex);
m_bAbort = true;
}
Instead of invoking a method directly, try posting a custom event. This will require that you implement an event filter at the target that handles your custom event. Event posting always works as long as an event loop is active in the target thread.
Take a look at this answer here too. Also here.
Hope this helps.