...it is a signal just like QSocketNotifier::activated(), so it is connected to a slot...
I'm not following. What is a "signal"? And what is a "slot"? They are preprocessor macros for "qFlagLocation("1"#a QLOCATION)" and "qFlagLocation("2"#a QLOCATION)", respectively.
This is because the example uses blocking I/O, so it calls waitForReadyRead instead of connecting to readRead()
What do you mean, "connecting to"? How would you "connect" to a preprocessor macro? What does that even mean?
Added after 42 minutes:
Also, I've been looking at the example you linked to, trying to tear it apart and get it working. I'm running into an error in my "connect(...)" call again, in which it cannot find the function specified in the fourth argument (the one with SLOT(...) surrounding it). This may be a compiler-specific constructor order error.
This is my basic code:
#include <QtCore\QObject>
#include <QtNetwork\QTcpSocket>
#include <QtCore\QDebug>
class my_TCP_socket
: public QObject{
public:
explicit my_TCP_socket
(QObject *parent
= 0);
~my_TCP_socket();
private:
void read_it();
};
my_TCP_socket
::my_TCP_socket(QObject *parent
) :{
m_socket_ptr->connectToHost("10.10.10.126", 5);
if (!(m_socket_ptr->waitForConnected(5000)))
{
qDebug() << "Not connected!";
m_socket_ptr = NULL;
}
else
{
qDebug() << "Connected!";
}
this->connect(m_socket_ptr, SIGNAL(readyRead()), this, SLOT(this->read_it(void)));
}
void my_TCP_socket::read_it()
{
qint64 bytes_available = m_socket_ptr->bytesAvailable();
if (bytes_available > 0)
{
in >> in_data;
qDebug() << in_data;
}
else
{
qDebug() << "no data";
}
}
#include <QtCore\QObject>
#include <QtNetwork\QTcpSocket>
#include <QtCore\QDebug>
class my_TCP_socket : public QObject
{
public:
explicit my_TCP_socket(QObject *parent = 0);
~my_TCP_socket();
private:
QTcpSocket *m_socket_ptr;
void read_it();
};
my_TCP_socket::my_TCP_socket(QObject *parent) :
QObject(parent)
{
m_socket_ptr = new QTcpSocket(this);
m_socket_ptr->connectToHost("10.10.10.126", 5);
if (!(m_socket_ptr->waitForConnected(5000)))
{
qDebug() << "Not connected!";
m_socket_ptr = NULL;
}
else
{
qDebug() << "Connected!";
}
this->connect(m_socket_ptr, SIGNAL(readyRead()), this, SLOT(this->read_it(void)));
}
void my_TCP_socket::read_it()
{
QDataStream in(m_socket_ptr);
qint64 bytes_available = m_socket_ptr->bytesAvailable();
if (bytes_available > 0)
{
QString in_data;
in >> in_data;
qDebug() << in_data;
}
else
{
qDebug() << "no data";
}
}
To copy to clipboard, switch view to plain text mode
Those functions may not exist yet when "connect(...)" is called in the constructor, although I may just be doing it wrong because this
int method_index = this->metaObject()->indexOfMethod("init()");
int method_index = this->metaObject()->indexOfMethod("init()");
To copy to clipboard, switch view to plain text mode
just returns -1, and that's after initialization.
Again, any feedback would be helpful.
Added after 20 minutes:
Followup: Should have noted that the attempt connect the "read_it()" function resulted in this error at runtime:
Object
::connect: No such
slot QObject::this->read_it
(void) in
*path redacted
*
Object::connect: No such slot QObject::this->read_it(void) in *path redacted*
To copy to clipboard, switch view to plain text mode
I have tentatively discovered that the Q_OBJECT macro needs to be declared at the top of the "private" section of the class, but that is producing a linker error. I put that into a new thread. The question seemed different enough from this thread.
Please advise still. The examples are not working outside QtCreator.
Bookmarks