// mythread.cpp #include "mythread.h" #include "QProcess" #include "QtGlobal" #ifdef Q_OS_LINUX // linux #define CMD_LEFT "xdotool key Left" #define CMD_RIGHT "xdotool key Right" #define CMD_ESC "xdotool key Escape" #define CMD_F5 "xdotool key F5" #endif #ifdef Q_OS_WIN32 // win #define CMD_LEFT "KeyUtil Left" #define CMD_RIGHT "KeyUtil Right" #define CMD_ESC "KeyUtil Esc" #define CMD_F5 "KeyUtil F5" #endif MyThread::MyThread(long ID, QObject *parent) : QThread(parent) { this->socketDescriptor = ID; } void MyThread::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 MyThread::readyRead() { QByteArray Data = socket->readAll(); QString d =QString::fromUtf8(Data); qDebug() << socketDescriptor << " Data in: " << d; QProcess p(0); if(!d.trimmed().compare("Left")){ //receive left qDebug()<write(Data); } void MyThread::disconnected() { qDebug() << socketDescriptor << " Disconnected"; socket->deleteLater(); exit(0); }