
Originally Posted by
wysota
I see some synchronous things in your code.
Do you mean connect/write signals sequence? It's just to make example smaller. Normally write should be called in slot for connected() signal, but in example it doesn't matter because local connections are quite fast.

Originally Posted by
wysota
Besides, if you want to post an example
As I mentioned, the problem is that in example it works and in real project the same code doesn't call test() slot. Here is the complete example:
test.h
#include <ClientSocket.h>
{
Q_OBJECT
signals:
void connectSignal(const QString& host, int port);
void writeData(const char* data,unsigned int length);
public:
Test();
private:
ClientSocket m_ClientSocket;
};
#include <ClientSocket.h>
class Test : public QObject
{
Q_OBJECT
signals:
void connectSignal(const QString& host, int port);
void writeData(const char* data,unsigned int length);
public:
Test();
private:
ClientSocket m_ClientSocket;
};
To copy to clipboard, switch view to plain text mode
test.cpp
#include "test.h"
Test
::Test() : QObject(), m_ClientSocket
() { connect(this, SIGNAL(connectSignal(const QString&, int)), &m_ClientSocket, SLOT(connectSlot(const QString&, int)));
connect(this, SIGNAL(writeData(const char*,unsigned int)), &m_ClientSocket, SLOT(writeData(const char*,unsigned int)));
emit(connectSignal("localhost", 1000));
emit(writeData("rtst", 4));
}
#include "test.h"
Test::Test() : QObject(), m_ClientSocket() {
connect(this, SIGNAL(connectSignal(const QString&, int)), &m_ClientSocket, SLOT(connectSlot(const QString&, int)));
connect(this, SIGNAL(writeData(const char*,unsigned int)), &m_ClientSocket, SLOT(writeData(const char*,unsigned int)));
emit(connectSignal("localhost", 1000));
emit(writeData("rtst", 4));
}
To copy to clipboard, switch view to plain text mode
ClientSocket.h
#include <QTcpSocket>
class ClientSocket
: public QObject{
Q_OBJECT
public:
connect(m_socket, SIGNAL(readyRead()), this, SLOT(test()));
}
bool connectToHost
(QString host,
int port
) { m_socket->connectToHost(host, port);
return m_socket->waitForConnected(1000);
}
void write(const char* Data,unsigned int nBytes) {
{
m_socket->write(Data);
m_socket->flush();
}
}
public slots:
void writeData(const char* Data,unsigned int nBytes) { write(Data, nBytes); }
void connectSlot(const QString& host, int port) { connectToHost(host, port); }
void test() { qDebug()<<"ok"; }
private:
};
#include <QTcpSocket>
class ClientSocket : public QObject
{
Q_OBJECT
public:
ClientSocket() : QObject()
, m_socket(new QTcpSocket(this)) {
connect(m_socket, SIGNAL(readyRead()), this, SLOT(test()));
}
bool connectToHost(QString host, int port) {
m_socket->connectToHost(host, port);
return m_socket->waitForConnected(1000);
}
void write(const char* Data,unsigned int nBytes) {
if (m_socket && (m_socket->state() == QAbstractSocket::ConnectedState))
{
m_socket->write(Data);
m_socket->flush();
}
}
public slots:
void writeData(const char* Data,unsigned int nBytes) { write(Data, nBytes); }
void connectSlot(const QString& host, int port) { connectToHost(host, port); }
void test() { qDebug()<<"ok"; }
private:
QTcpSocket* m_socket;
};
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QtCore/QCoreApplication>
#include "test.h"
int main(int argc, char *argv[])
{
Test t;
return a.exec();
}
#include <QtCore/QCoreApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Test t;
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks