//I create my thread in CommandClient constructor and emit a signal sendCommandSignal when qml calls CommandClient function
mst = new MySocketThread();
mst->ipAddress = "192.168.0.8";
mst->port = 7755;
mst->moveToThread(qt);
connect(qt, SIGNAL(started()), mst, SLOT(doWork()));
connect(qt, SIGNAL(finished()), mst, SLOT(deleteLater()));
connect(this, SIGNAL(sendCommandSignal()), mst, SLOT(sendCommandSlot()));
qt->start();
void MySocketThread::doWork()
{
}
void MySocketThread::sendCommandSlot()
{
qDebug() << "socket is not null "<<toSend;
txPacket= "";
txPacket.append(toSend);
txPacket.append(" ");
txDatagram = txPacket.toUtf8(); //convert string to datagram
socket
->writeDatagram
(txDatagram.
data(), txDatagram.
size(),
QHostAddress("192.168.0.8"),
1212);
socket->flush();// tried with and without flush and waitForBytesWritten()
qDebug() << "bytes written-"<<socket->waitForBytesWritten(30000);
toSend = "";
}
//I create my thread in CommandClient constructor and emit a signal sendCommandSignal when qml calls CommandClient function
QThread* qt = new QThread();
mst = new MySocketThread();
mst->ipAddress = "192.168.0.8";
mst->port = 7755;
mst->moveToThread(qt);
connect(qt, SIGNAL(started()), mst, SLOT(doWork()));
connect(qt, SIGNAL(finished()), mst, SLOT(deleteLater()));
connect(this, SIGNAL(sendCommandSignal()), mst, SLOT(sendCommandSlot()));
qt->start();
void MySocketThread::doWork()
{
socket = new QUdpSocket(this);
socket->setSocketOption(QAbstractSocket::LowDelayOption,1);
}
void MySocketThread::sendCommandSlot()
{
qDebug() << "socket is not null "<<toSend;
QString txPacket;//, txDatagram;
txPacket= "";
QByteArray txDatagram;// = "";
txPacket.append(toSend);
txPacket.append(" ");
txDatagram = txPacket.toUtf8(); //convert string to datagram
socket->writeDatagram(txDatagram.data(), txDatagram.size(), QHostAddress("192.168.0.8"), 1212);
socket->flush();// tried with and without flush and waitForBytesWritten()
qDebug() << "bytes written-"<<socket->waitForBytesWritten(30000);
toSend = "";
}
To copy to clipboard, switch view to plain text mode
Bookmarks