QSerialPort in QThread - qt

I was trying to read the serial output from a thread. The Serial port is opened in main program and passed the QSerialPort variable to thread.
Then I called serial.waitForReadyRead() function in thread, But its not getting any value from serial port. I confirmed that serial port is sending data by reading the same from main program.
Can anyone knows why this behavior is happening?
Adding the code below:
mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QSerialPort *serial;
private slots:
void onProgressChanged();
private:
Ui::MainWindow *ui;
};
class WorkerThread : public QThread {
Q_OBJECT
public:
QSerialPort *thread_serial;
void run() {
int ret = thread_serial->waitForReadyRead(60000); //thread is not getting any serial data.
//so it will not emit progressChanged.
if (ret)
{
qDebug()<<"Data Rate Read done";
QByteArray recvData = thread_serial->readAll();
qDebug()<<recvData;
emit progressChanged();
}
}
// Define signal:
signals:
void progressChanged();
};
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort(this);
openSerialPort( );
}
void MainWindow::openSerialPort( )
{
serial->setPortName("COM 4");
serial->setBaudRate(QSerialPort::Baud115200);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
if (serial->open(QIODevice::ReadWrite)) {
qDebug()<<"Connected";
WorkerThread *workerThread = new WorkerThread();
workerThread->thread_serial = serial;
connect(workerThread, SIGNAL(progressChanged()),
SLOT(onProgressChanged()));
workerThread->start();
}
}
void MainWindow::onProgressChanged()
{
qDebug()<<"inside onProgressChanged";
}

Related

How to deal with signal and slot in different classes in Qt

My UI looks like following diagram:
Inside MainWindow, there is a big red QStackedWidget area where i will use to put different custom stackedwidgets, current StackedWidget1 have three CardWidgets, (StackedWidget and CardWidget are both cpp class), inside each CardWidget, there is a pushbutton, what i want to do is when i click the pushbotton of CardWidget, the CardWidget send signal to MainWindow, the MainWindow then switch from current stackedwidget to another custom stackedwidget, meanwhile, the signal will carry some variables in cardwidget to the new custom stackedwidget.
Now the problem is after i click one pushbutton, the cardwidget1 emit out signal,the slot function in mainwindow didn't react.
I searched out one question similiar to my situation,link, maybe just as Eeli K and dheerendra said, here the cardwidget-sender instance used in connect(...) in mainwindow.cpp is different from the cardwidget-sender instance in StackedWidget1.cpp, so mainwindow actually didn't receive the signal.
Another thing, i construct the cardwidget-sender instance without any argument in mainwindow.cpp, but i construct the cardwidget-sender instance with 5 arguments in StackedWidget1.cpp.
I write a minimal,reproducible example Qt project - download link.
For those who would like to see the code directly, i extract some of its code:
# mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
stackedwidget1 = new StackedWidget1(ui->stackedWidget);
ui->stackedWidget->addWidget(stackedwidget1);
CardWidget* card = new CardWidget();
bool c_res = connect (card, &CardWidget::open_panel_signal, this, &MainWindow::open_panel);
qDebug()<<"Connect open_panels signal and slot ? :"<<c_res;
//If i emit signal from here instead of from cardwidget, it can trigger the slot function, uncomment to see it
// int dummy_int = 0;
// QString dummy_string = "abcd";
// emit card->open_panel_signal(dummy_int, dummy_string);
}
void MainWindow::open_panel(int index, QString text)
{
qDebug()<<"test text from cardwidget"<<index<<"is"<<text;
stackedwidget2 = new StackedWidget2(ui->stackedWidget);
ui->stackedWidget->addWidget(stackedwidget2);
ui->stackedWidget->setCurrentWidget(stackedwidget2);
}
#mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void open_panel(int index, QString text);
private:
Ui::MainWindow *ui;
StackedWidget1* stackedwidget1;
StackedWidget2* stackedwidget2;
};
# stackedwidget1.cpp
StackedWidget1::StackedWidget1(QWidget *parent) :
QWidget(parent),
ui(new Ui::StackedWidget1)
{
ui->setupUi(this);
for(int col = 0; col< 3; col++){
CardWidget* card = new CardWidget(this,col);
ui->tableWidget->setCellWidget(0, col, card);
ui->tableWidget->update();
}
}
# cardwidget.cpp
CardWidget::CardWidget(QWidget *parent, int index) :
QWidget(parent),
ui(new Ui::CardWidget),
index_(index)
{
ui->setupUi(this);
}
void CardWidget::on_pushButton_clicked()
{
QString text = "text from cardwidget";
emit open_panel_signal(index_, text);
qDebug()<<"emit signal from cardwidget"<<index_;
}
# cardwidget.h
class CardWidget : public QWidget
{
Q_OBJECT
public:
explicit CardWidget(QWidget *parent = nullptr, int index = 0);
~CardWidget();
signals:
void open_panel_signal(int, QString);
private slots:
void on_pushButton_clicked();
private:
Ui::CardWidget *ui;
int index_;
};
# stackedwidget2.cpp
StackedWidget2::StackedWidget2(QWidget *parent) :
QWidget(parent),
ui(new Ui::StackedWidget2)
{
ui->setupUi(this);
}
Is there any good way to solve this problem? Thanks in advance!
The Basic idea should be :
Signal (card Widget) -> Signal (stackedwidget1) -> SLOT(Mainwindow)
You can follow the general approach:
mainwindow.h
public slots:
void open_panel(int, QString);
cardwidget.h
signals:
void open_panel_signal(int, QString);
cardwidget.cpp
void CardWidget::on_pushButton_clicked()
{
QString text = "text from cardwidget";
emit open_panel_signal(index_, text);
qDebug()<<"emit signal from cardwidget"<<index_;
}
stackedwidget1.h
signals:
void open_panel_signal(int, QString);
stackedwidget1.cpp
StackedWidget1::StackedWidget1(QWidget *parent) :
QWidget(parent),
ui(new Ui::StackedWidget1)
{
ui->setupUi(this);
for(int col = 0; col< 3; col++){
CardWidget* card = new CardWidget(this,col);
ui->tableWidget->setCellWidget(0, col, card);
ui->tableWidget->update();
connect(card,SIGNAL(open_panel_signal(int, QString)),this,SIGNAL(open_panel_signal(int, QString)));
}
}
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
stackedwidget1 = new StackedWidget1(ui->stackedWidget);
ui->stackedWidget->addWidget(stackedwidget1);
connect(stackedwidget1,SIGNAL(open_panel_signal(int, QString)),this,SLOT(open_panel(int, QString)));
}

QTcpSocket handling in another QThread

I need to handle incoming tcp connections on individual QThread's.
After successful client authentication, the according socket should be stored in an QList object.
[simplified main/server-side application]
class Server : public QObject
{
Q_OBJECT
public:
Server();
private:
QList<QTcpSocket*> m_connections;
QTcpServer m_server;
void handleIncomingConnection();
void handleWaiterThread();
private slots:
void treatFinishedWaiterThread();
}
[according function definitions]
handleIncomingConnection() slot is connected with the server object's (m_server) newConnection() signal.
void Server::handleIncomingConnection()
{
QThread *waiter = new QThread();
connect(waiter, SIGNAL(started()), this, SLOT(handleWaiterThread()));
connect(waiter, SIGNAL(finished()), this, SLOT(treatFinishedWaiterThread()));
moveToThread(waiter);
waiter->start();
}
void Server::handleWaiterThread()
{
// fetch requesting socket
QTcpSocket *socket = m_server->nextPendingConnection();
// HANDLE PASSWORD AUTHENTICATION HERE ...
// IF SUCCESSFUL, CONTINUE
connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
// add to list
m_connections.append(socket);
}
void Server::treatFinishedWaiterThread()
{
QThread *caller = qobject_cast<QThread*>(sender());
caller->deleteLater();
}
If I try to run this, the threads get created but no SIGNAL is emitted when they're done, so I can't delete threads afterwards. Additionally I get this message:
QObject::moveToThread: Widgets cannot be moved to a new thread
How to fix this?
[01.06.2016]
According to QTcpServer::nextPendingConnection() it says:
The returned QTcpSocket object cannot be used from another thread. If you want to use an incoming connection from another thread, you need to override incomingConnection().
So in the end I have to create another class that inherits from QTcpServer.
[01.07.2016 #1]
I revised my code and added a custom server and waiter thread class.
[custom server class]
class CustomServer : public QTcpServer
{
Q_OBJECT
public:
WServer(QObject* = nullptr) : QTcpServer(parent) {}
signals:
void connectionRequest(qintptr);
protected:
void incomingConnection(qintptr socketDescriptor)
{
emit connectionRequest(socketDescriptor);
}
};
[custom thread class]
class Waiter : public QThread
{
Q_OBJECT
public:
Waiter(qintptr socketDescriptor, QObject *parent = nullptr)
: QThread(parent)
{
// Create socket
m_socket = new QTcpSocket(this);
m_socket->setSocketDescriptor(socketDescriptor);
}
signals:
void newSocket(QTcpSocket*);
protected:
void run()
{
// DO STUFF HERE
msleep(2500);
emit newSocket(m_socket);
}
private:
QTcpSocket *m_socket;
};
[new main class]
class ServerGUI : public QWidget
{
Q_OBJECT
public:
Server(QObject*);
private:
QList<QTcpSocket*> m_connections;
CustomServer m_server;
private slots:
void handleConnectionRequest(qintptr);
void handleNewSocket(QTcpSocket*);
}
void CustomServer::handleConnectionRequest(qintptr socketDescriptor)
{
Waiter *nextWaiter = new Waiter(socketDescriptor, this);
connect(nextWaiter, SIGNAL(newSocket(QTcpSocket*)), this, SLOT(handleNewSocket(QTcpSocket*)));
connect(nextWaiter, SIGNAL(finished()), this, SLOT(deleteLater()));
nextWaiter->start();
}
void CustomServer::handleNewSocket(QTcpSocket *socket)
{
// DO STUFF HERE ...
connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
// FINALLY ADD TO ACTIVE-CLIENT LIST ...
}
Signal & Slot specific settings:
Since CustomServer is defined as a class member (m_server) within my main widget class (that handles GUI; called ServerGUI),
connectionRequest(qintptr) signal of m_server gets connected with handleConnectionRequest(qintptr) slot of ServerGUI instance.
But now my application is crashing immediately after startup, showing following message in debug window:
HEAP[qtapp.exe]: Invalid address specified to RtlValidateHeap( 000002204F430000, 0000006E0090F4C0 )
What may be the cause of this?
[01.10.2016 #2]
I adapted my code according to user2014561's answer.
for CustomServer class
class CustomServer : public QTcpServer
{
Q_OBJECT
public:
WServer(QHostAddress, quint16, quint16, QObject* = nullptr);
~WServer();
void kickAll();
void kickClient(qintptr);
QHostAddress localAddress() const;
quint16 serverPort() const;
bool isReady() const;
bool alreadyConnected(qintptr) const;
bool clientLimitExhausted() const;
signals:
void clientConnected(qintptr);
void clientDisconnected(qintptr);
private slots:
void destroyedfunc(QObject*);
// JUST FOR TESTING PURPOSES
void waiterFinished();
private:
QList<ServerPeer*> m_connections;
quint16 m_maxAllowedClients;
bool m_readyState;
void incomingConnection(qintptr);
};
for kickAll():
void WServer::kickAll()
{
while (!m_connections.isEmpty())
{
ServerPeer *peer = m_connections.first();
QEventLoop loop;
connect(peer->thread(), SIGNAL(destroyed()), &loop, SLOT(quit())); // ### PROBLEM ENCOUNTERED HERE
QMetaObject::invokeMethod(peer, "deleteLater", Qt::QueuedConnection);
loop.exec();
}
}
for kickClient(qintptr):
void WServer::kickClient(qintptr client_id)
{
foreach (ServerPeer *peer, m_connections)
{
bool peerState;
QMetaObject::invokeMethod(peer, "hasSocket", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(bool, peerState), Q_ARG(qintptr, client_id));
if (peerState)
{
QEventLoop loop;
connect(peer->thread(), SIGNAL(destroyed()), &loop, SLOT(quit()));
QMetaObject::invokeMethod(peer, "deleteLater", Qt::QueuedConnection);
loop.exec();
break;
}
}
}
for destroyedfunc(QObject*):
void CustomServer::destroyedfunc(QObject *obj)
{
ServerPeer *peer = static_cast<ServerPeer*>(obj);
m_connections.removeAll(peer);
}
for incomingConnection(qintptr):
void WServer::incomingConnection(qintptr handle)
{
ServerPeer *peer = new ServerPeer();
QThread *waiter = new QThread();
m_connections.append(peer); // add to list
peer->moveToThread(waiter);
// notify about client connect
connect(peer, SIGNAL(connected(qintptr)), this, SIGNAL(clientConnected(qintptr)));
// stop waiter thread by indirectly raising finished() signal
connect(peer, SIGNAL(finished()), waiter, SLOT(quit()));
// notify about client disconnect
connect(peer, SIGNAL(disconnected(qintptr)), this, SIGNAL(clientDisconnected(qintptr)));
// remove client from list
connect(peer, SIGNAL(destroyed(QObject*)), this, SLOT(destroyedfunc(QObject*)));
// notify about finished waiter thread; only for debug purposes
connect(waiter, SIGNAL(finished()), this, SLOT(waiterFinished()));
// remove waiter thread when finished
connect(waiter, SIGNAL(finished()), waiter, SLOT(deleteLater()));
QMetaObject::invokeMethod(peer, "start", Qt::QueuedConnection,
Q_ARG(qintptr, handle));
waiter->start();
}
for ServerPeer class
class ServerPeer : public QObject
{
Q_OBJECT
public:
ServerPeer(QObject* = nullptr);
~ServerPeer();
bool hasSocket(qintptr) const;
signals:
void connected(qintptr);
void disconnected(qintptr);
void finished();
public slots:
void start(qintptr);
void disconnect();
private slots :
void notifyConnect();
void notifyDisconnect();
private:
QTcpSocket *m_peer;
qintptr m_id;
};
for ServerPeer(QObject*):
ServerPeer::ServerPeer(QObject *parent) : QObject(parent), m_peer(nullptr)
{
}
for ~ServerPeer():
ServerPeer::~ServerPeer()
{
disconnect();
}
for start(qintptr):
void ServerPeer::start(qintptr handle)
{
qDebug() << "New waiter thread has been started.";
m_peer = new QTcpSocket(this);
if (!m_peer->setSocketDescriptor(handle))
{
this->deleteLater();
return;
}
if (true /*verification here*/)
{
connect(m_peer, SIGNAL(disconnected()), this, SLOT(notifyDisconnect()));
connect(m_peer, SIGNAL(disconnected()), this, SLOT(deleteLater()));
// manually do connected notification
QTimer::singleShot(0, this, SLOT(notifyConnect()));
}
else
{
this->deleteLater();
}
emit finished();
}
for disconnect():
void ServerPeer::disconnect()
{
if (m_peer != nullptr)
{
if (m_peer->state() != QAbstractSocket::SocketState::ClosingState
&& m_peer->state() != QAbstractSocket::SocketState::UnconnectedState)
m_peer->abort();
delete m_peer;
m_peer = nullptr;
}
}
for notifyConnect():
void ServerPeer::notifyConnect()
{
emit connected(m_peer);
}
for notifyDisconnect():
void ServerPeer::notifyDisconnect()
{
emit disconnected(m_peer);
}
for ServerGUI class
class ServerGUI : public QWidget
{
Q_OBJECT
public:
ServerGUI(QWidget* = nullptr);
private:
Ui::ServerWindow ui;
CustomServer *m_server;
private slots:
// For further handling, e.g. updating client view
void handleNewClient(qintptr);
void handleRemovedClient(qintptr);
}
for ServerGUI(QWidget*):
ServerGUI::ServerGUI(QWidget *parent) : QWidget(parent)
{
// initialize gui elements;
// GENERATED WITH ACCORDING *.ui FILE
ui.setupUi(this);
m_server = new WServer(QHostAddress::LocalHost, 1234, 2, this);
if (!m_server->isReady())
{
qDebug() << "Server could not start!";
delete m_server;
m_server = nullptr;
return;
}
connect(m_server, SIGNAL(clientConnected(qintptr)), this, SLOT(handleNewClient(qintptr)));
connect(m_server, SIGNAL(clientDisconnected(qintptr)), this, SLOT(handleRemovedClient(qintptr)));
}
And here my main function:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ServerGUI w;
w.show();
return a.exec();
}
With given code following message pops up if I try to kick a (selected) client:
QMetaObject::invokeMethod: No such method ServerPeer::hasSocket(qintptr)
QObject::connect: Cannot connect (null)::destroyed() to QEventLoop::quit()
How to fix this?
If I understood correctly, you want to run each peer of your server on a separate thread, if so, then the following may help you:
create a subclass of QTcpServer
reimplement incomingConnection() method
create an instance (without parent) of QThread and of ServerPeer and start the thread
do the SIGNAL - SLOT connections to remove the peer from the list and delete the thread and the peers instances
add ServerPeer to your QList
once time started, do the credentials verification; if you reject them, abort the connection
Edit, considerations:
You haven't got the connected SIGNAL because when you set a socketDescriptor to the socket was already connected, so you can simple assume after setSocketDescriptor that the socket is connected and do what you want.
About the error when closing, it's happens because you not releasing properly the threads, see my edit how can you solve that.
Finally QTcpSocket must not be accessed by diferent threads, if you need to call ServerPeer from another thread use QMetaObject::invokeMethod with QueuedConnection or BlockingQueuedConnection and SIGNAL SLOT mechanism.
Edit 2:
Now the server and it's peers will be deleted on MainWindow::closeEvent and that way you can see the disconnected function being called. I guess the problem happens depending of the order that the classes will be deleted.
You can interact with the socket including send data through it, but I believe that will be painless use the Qt methods for cross-thread calls already mentioned. In my example you can easily write to a specific peer or to all peers.
customserver.h:
//Step 1
#include <QtCore>
#include <QtNetwork>
#include "serverpeer.h"
class CustomServer : public QTcpServer
{
Q_OBJECT
public:
explicit CustomServer(const QHostAddress &host, quint16 port, quint16 maxconnections, QObject *parent = nullptr);
~CustomServer();
void kickAll();
void kickClient(qintptr id);
void writeData(const QByteArray &data, qintptr id);
void writeData(const QByteArray &data);
QHostAddress localAddress();
quint16 serverPort();
bool isReady();
signals:
void clientConnected(qintptr);
void clientDisconnected(qintptr);
private slots:
void destroyedfunc(QObject *obj);
private:
void incomingConnection(qintptr handle);
QList<ServerPeer*> m_connections;
int m_maxAllowedClients;
};
customserver.cpp:
#include "customserver.h"
CustomServer::CustomServer(const QHostAddress &host, quint16 port, quint16 maxconnections, QObject *parent) :
m_maxAllowedClients(maxconnections), QTcpServer(parent)
{
listen(host, port);
}
CustomServer::~CustomServer()
{
kickAll();
}
//Step 2
void CustomServer::incomingConnection(qintptr handle)
{
// handle client limit
if (m_connections.size() >= m_maxAllowedClients)
{
qDebug() << "Can't allow new connection: client limit reached!";
QTcpSocket *socket = new QTcpSocket();
socket->setSocketDescriptor(handle);
socket->abort();
delete socket;
return;
}
//Step 3
ServerPeer *peer = new ServerPeer();
QThread *waiter = new QThread();
peer->moveToThread(waiter);
//Step 4
connect(peer, SIGNAL(connected(qintptr)), this, SIGNAL(clientConnected(qintptr)));
connect(peer, SIGNAL(disconnected(qintptr)), this, SIGNAL(clientDisconnected(qintptr)));
connect(peer, SIGNAL(destroyed()), waiter, SLOT(quit()));
connect(peer, SIGNAL(destroyed(QObject*)), this, SLOT(destroyedfunc(QObject*)));
connect(waiter, SIGNAL(finished()), waiter, SLOT(deleteLater()));
QMetaObject::invokeMethod(peer, "start", Qt::QueuedConnection, Q_ARG(qintptr, handle));
waiter->start();
//Step 5
m_connections.append(peer);
}
void CustomServer::kickAll()
{
while (!m_connections.isEmpty())
{
ServerPeer *peer = m_connections.first();
QEventLoop loop;
connect(peer->thread(), SIGNAL(destroyed()), &loop, SLOT(quit()));
QMetaObject::invokeMethod(peer, "deleteLater", Qt::QueuedConnection);
loop.exec();
}
}
void CustomServer::kickClient(qintptr id)
{
foreach (ServerPeer *peer, m_connections)
{
ServerPeer::State hassocket;
QMetaObject::invokeMethod(peer, "hasSocket", Qt::BlockingQueuedConnection, Q_RETURN_ARG(ServerPeer::State, hassocket), Q_ARG(qintptr, id));
if (hassocket == ServerPeer::MyTRUE)
{
QEventLoop loop;
connect(peer->thread(), SIGNAL(destroyed()), &loop, SLOT(quit()));
QMetaObject::invokeMethod(peer, "deleteLater", Qt::QueuedConnection);
loop.exec();
break;
}
}
}
void CustomServer::writeData(const QByteArray &data)
{
foreach (ServerPeer *peer, m_connections)
QMetaObject::invokeMethod(peer, "writeData", Qt::QueuedConnection, Q_ARG(QByteArray, data));
}
void CustomServer::writeData(const QByteArray &data, qintptr id)
{
foreach (ServerPeer *peer, m_connections)
{
ServerPeer::State hassocket;
QMetaObject::invokeMethod(peer, "hasSocket", Qt::BlockingQueuedConnection, Q_RETURN_ARG(ServerPeer::State, hassocket), Q_ARG(qintptr, id));
if (hassocket == ServerPeer::MyTRUE)
{
QMetaObject::invokeMethod(peer, "writeData", Qt::QueuedConnection, Q_ARG(QByteArray, data));
break;
}
}
}
QHostAddress CustomServer::localAddress()
{
return QTcpServer::serverAddress();
}
quint16 CustomServer::serverPort()
{
return QTcpServer::serverPort();
}
bool CustomServer::isReady()
{
return QTcpServer::isListening();
}
void CustomServer::destroyedfunc(QObject *obj)
{
ServerPeer *peer = static_cast<ServerPeer*>(obj);
m_connections.removeAll(peer);
}
serverpeer.h:
#include <QtCore>
#include <QtNetwork>
class ServerPeer : public QObject
{
Q_OBJECT
public:
explicit ServerPeer(QObject *parent = nullptr);
~ServerPeer();
enum State
{
MyTRUE,
MyFALSE
};
signals:
void connected(qintptr id);
void disconnected(qintptr id);
public slots:
ServerPeer::State hasSocket(qintptr id);
void start(qintptr handle);
void writeData(const QByteArray &data);
private slots:
void readyRead();
void notifyConnect();
void notifyDisconnect();
private:
QTcpSocket *m_peer;
qintptr m_id;
};
serverpeer.cpp:
#include "serverpeer.h"
ServerPeer::ServerPeer(QObject *parent) : QObject(parent), m_peer(nullptr)
{
}
ServerPeer::~ServerPeer()
{
if (m_peer)
m_peer->abort();
}
ServerPeer::State ServerPeer::hasSocket(qintptr id)
{
if (m_id == id)
return MyTRUE;
else
return MyFALSE;
}
void ServerPeer::start(qintptr handle)
{
m_peer = new QTcpSocket(this);
m_peer->setSocketDescriptor(handle);
//Step 6
if (true /*verification here*/)
{
m_id = handle;
QTimer::singleShot(0, this, SLOT(notifyConnect()));
connect(m_peer, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(m_peer, SIGNAL(disconnected()), this, SLOT(notifyDisconnect()));
connect(m_peer, SIGNAL(disconnected()), this, SLOT(deleteLater()));
}
else
{
m_peer->abort();
this->deleteLater();
}
}
void ServerPeer::readyRead()
{
qDebug() << m_peer->readAll() << QThread::currentThread();
}
void ServerPeer::writeData(const QByteArray &data)
{
m_peer->write(data);
m_peer->flush();
}
void ServerPeer::notifyConnect()
{
emit connected(m_id);
}
void ServerPeer::notifyDisconnect()
{
emit disconnected(m_id);
}
mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
qRegisterMetaType<qintptr>("qintptr");
m_server = new CustomServer(QHostAddress::LocalHost, 1024, 2, this);
if (!m_server->isReady())
{
qDebug() << "Server could not start!";
delete m_server;
m_server = nullptr;
return;
}
connect(m_server, SIGNAL(clientConnected(qintptr)), this, SLOT(handleNewClient(qintptr)));
connect(m_server, SIGNAL(clientDisconnected(qintptr)), this, SLOT(handleRemovedClient(qintptr)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeEvent(QCloseEvent *)
{
if (m_server)
{
delete m_server;
m_server = nullptr;
}
}
void MainWindow::handleNewClient(qintptr id)
{
qDebug() << __FUNCTION__ << id;
m_server->writeData(QString("Hello client id: %0\r\n").arg(id).toLatin1(), id);
m_server->writeData(QString("New client id: %0\r\n").arg(id).toLatin1());
}
void MainWindow::handleRemovedClient(qintptr id)
{
qDebug() << __FUNCTION__ << id;
}

declaring qtcpsocket class object as global

I am working on qt to develop a socket programming.i am posting the code here.
"mainwindow.h"
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
namespace Ui {
class MainWindow;
}
static void handler(int sig, siginfo_t *si, void *uc);
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QTcpSocket *socket;
void timer();
void config();
private slots:
void newconnection();
private:
Ui::MainWindow *ui;
QTcpServer *server;
};
#endif // MAINWINDOW_H
"mainwindow.cpp"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
static char i;
ui->setupUi(this);
sock=socket;
// if(!i)
// {
// connect(ui->start_button,SIGNAL(clicked()),this,SLOT(config()));
// }
// i=1;
qDebug()<<i;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow ::newconnection()
{
static int i;
socket=server->nextPendingConnection();
//connect(socket,SIGNAL(readyRead()),this,SLOT(timer_data_recieve()));
qDebug()<<"new connection";
qDebug()<<"server is reading..."<<socket->bytesAvailable();
qDebug()<<socket->readAll();
//time->start(1000); //QT timer started
if(!i)
timer();
i=1;
}
void MainWindow::timer()
{
timer_t t_id;
struct sigaction sa;
struct sigaction arg;
struct sigevent s_evnt;
struct itimerspec timer;
/* Establish handler for notification signal */
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
s_evnt.sigev_notify = SIGEV_SIGNAL;
s_evnt.sigev_signo = SIGRTMAX;
s_evnt.sigev_value.sival_ptr = &t_id;
if (sigaction(SIGRTMAX, &sa,NULL) == -1)
{
perror("sigaction");
}
//==============timer vlaues==============
timer.it_value.tv_sec=0;
timer.it_value.tv_nsec=20000000;
timer.it_interval.tv_sec=0;
timer.it_interval.tv_nsec=20000000;
if(timer_create(CLOCK_REALTIME,&s_evnt,&t_id)==-1);
perror("timer create");
if(timer_settime(t_id,0,&timer,NULL)==-1)
perror("timer_set_time");
}
void MainWindow::config()
{
//time=new QTimer(this);
server=new QTcpServer(this);
socket=new QTcpSocket(this);
connect(server,SIGNAL(newConnection()),this,SLOT(newconnection()));
// connect(time,SIGNAL(timeout()),this,SLOT(timer_data_sending()));
if(server->listen(QHostAddress::LocalHost,600))
{
qDebug()<<"server started";
}
else
qDebug()<<"server not started";
qDebug()<<MainWindow::server->errorString();
}
static void handler(int sig, siginfo_t *si, void *uc)
{
// MainWindow w;
qDebug("in handler");
// MainWindow *s = static_cast<MainWindow*>(&w);
static char first_call=1;
static unsigned sec;
static long nsec;
struct timespec start;
struct timespec curr;
if (first_call) {
first_call = 0;
if (clock_gettime(CLOCK_MONOTONIC, &start) == -1)
perror("clock_gettime");
}
if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1)
perror("clock_gettime");
sec = (curr.tv_sec - start.tv_sec);
nsec = (curr.tv_nsec - start.tv_nsec);
if (nsec < 0)
{
sec--;
nsec += 1000000000;
}
start = curr;
}
"main.cpp"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.config();
return a.exec();
}
here evrything is working fine about handler.
but if want to access the handler data to socket to send to client,one way is to declare socket is global.but how to decalre globally to access in mainwindow member functions and as well as in handler?
please suggest me.it is very important to me .please don't avoid it.
NOTE:qtimer is not neded here
You can use Singletone Design Pattern architecture for your TCP Socket class. You need to create class that extends QTcpSocket. So you will create only one object and access the same globally using get_instance() method, as it is public static in this design pattern. You can go through Singleton design pattern
The documentation contains a good example about how to implement a Singleton and export it from C++ to QML.

Signal-slot doesn't work using QThread

I am using QT framework. I have been using SIGNAL-SLOT for a while. I like it. :-)
But I cannot make it work when I use QThread. I always create new thread using “moveToThread(QThread …)” function.
Any suggestion? :-)
The error message is:
Object::connect: No such slot connection::acceptNewConnection(QString,int) in ..\MultiMITU600\mainwindow.cpp:14
Object::connect: (sender name: 'MainWindow')
I have read about similar problems but those were not connected to QThread.
Thanks, David
EDITED: you asked for source code
Here is one:
Here is the code:
The main class which contains the signal and the new thread:
mainwindow header:
class MainWindow : public QMainWindow
{
…
QThread cThread;
MyClass Connect;
...
signals:
void NewConnection(QString port,int current);
…
};
The constructor of the above class: .cpp
{
…
Connect.moveToThread(&cThread1);
cThread.start(); // start new thread
….
connect(this,SIGNAL(NewConnection(QString,int)),
&Connect,SLOT(acceptNewConnection(QString,int))); //start measuring
…
}
The class that contains the new thread and SLOT
Header:
class MyClass: public QObject
{
Q_OBJECT
….
public slots:
void acceptNewConnection(QString port, int current);
}
And the .cpp file of the above class:
void MyClass::acceptNewConnection(QString port, int current){
qDebug() << "This part is not be reached";
}
Finally I use emit in the class where the connection was made:
void MainWindow::on_pushButton_3_clicked()
{
…
emit NewConnection(port, 1);
}
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject *parent = 0);
public slots:
void acceptConnection(QString port, int current) {
qDebug() << "received data for port " << port;
}
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0) : QMainWindow(parent) {
myClass.moveToThread(&thread);
thread.start();
connect(this, SIGNAL(newConnection(QString,int)), &myClass, SLOT(acceptConnection(QString,int)));
emit newConnection("test", 1234);
}
signals:
void newConnection(QString, int);
private:
QThread thread;
MyClass myClass;
};
output:
received data for port "test"
Is your void MainWindow::on_pushButton_3_clicked() slot connected to a signal?
Also, for the sake of the clarity and readability of your code, keep the established naming convention and use lower case for object instances and member objects and methods.

QPointer for accessing object methods

have a strange problem using QPointer for accessing object method from a different, not related, object.
I've prepared a little example to explain it better.
I created two QWidget, Widget_A and Widget_B in a simple QDialog.
I need to access from Widget_B a public method of Widget_A: I passed widget_a pointer to a method of widget_b for QPointer assignment. Widget_A contains a QLineEdit that I want to clear from Widget_B.
The problem is when hitting pushButton_B nothing happens to lineEdit_A. Console doesn't show any problem, so the most obvious reason is Widget_B is working on a different Widget_A object, not the one I passed.
I also created a connection from QWidget_A to QWidget_B so when editing lineEdit_A a label_B text is changed accordingly: this works.
Before asking, I need a QPointer since in my real project Widget_A could be deleted. Can you explain where I'm wrong? Should I choose a different way? Thanks. Follow some snippets
widget_a.h
class Widget_A : public QWidget
{
Q_OBJECT
public:
explicit Widget_A(QWidget *parent = 0);
~Widget_A();
void clearLineEdit_A();
signals:
void lineEdit_A_changed(const QString &);
private slots:
void on_lineEdit_A_textChanged(const QString &arg1);
private:
Ui::Widget_A *ui;
};
widget_a.cpp
Widget_A::Widget_A(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget_A)
{
ui->setupUi(this);
ui->lineEdit_A->setText("write something here...");
ui->lineEdit_A->selectAll();
}
Widget_A::~Widget_A()
{
delete ui;
}
void Widget_A::clearLineEdit_A()
{
ui->lineEdit_A->clear();
}
void Widget_A::on_lineEdit_A_textChanged(const QString &arg1)
{
emit lineEdit_A_changed(arg1);
}
widget_b.h
class Widget_B : public QWidget
{
Q_OBJECT
public:
explicit Widget_B(QWidget *parent = 0);
~Widget_B();
void controlWidget(QWidget *w);
private slots:
void changeLabel_B(const QString &text);
void on_pushButton_B_clicked();
private:
Ui::Widget_B *ui;
QPointer<QWidget> qPtrWdgt;
};
widget_b.cpp
Widget_B::Widget_B(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget_B)
{
ui->setupUi(this);
}
Widget_B::~Widget_B()
{
delete ui;
}
void Widget_B::controlWidget(QWidget *w)
{
qPtrWdgt = w;
connect(qPtrWdgt, SIGNAL(lineEdit_A_changed(const QString &)),
this, SLOT(changeLabel_B(const QString &)));
}
void Widget_B::changeLabel_B(const QString &text)
{
ui->label_B->setText(text);
}
void Widget_B::on_pushButton_B_clicked()
{
Widget_A(qPtrWdgt).clearLineEdit_A();
}
dialog.h
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
Widget_A *widget_a;
Widget_B *widget_b;
};
dialog.cpp
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
widget_a = new Widget_A(this);
widget_b = new Widget_B(this);
widget_b->controlWidget(widget_a);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(widget_a);
mainLayout->addWidget(widget_b);
setLayout(mainLayout);
}
Widget_A(qPtrWdgt).clearLineEdit_A();
This creates a new Widget_A by using the Widget_A(QWidget *) constructor, then calls clearLineEdit_A() on it.
If you define qPtrWdgt as QPointer<Widget_A> instead, and since QPointer overloads operator->, then you can use:
qPtrWdgt->clearLineEdit_A();
You'll need to include Widget_A's header in Widget_B's header too.

Resources