Funny thing about that: I tried both the QApplication (that one took awhile to figure out the .pro file) and QCoreApplication, and they both give the same error. The error comes from the QTcpSocket constructor, not from the application. The bizarre part is that it seems that the QTcpSocket was created anyway, and while QCoreApplication ignores it, Qapplication goes ahead anyway and is happy with my QObject::connect(...) stuff and it works, so I don't know what it's complaining about.
Bottom line: My code works, but something, somewhere, is still not ok.
Note: This is a QConsole project. Here is my .pro file:
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = testing_1
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS +=
INCLUDEPATH += tmp/moc/release_shared
unix|win32: LIBS += -L$$PWD/../../../../../Qt/4.8.5/lib/ -lQtCore4
INCLUDEPATH += $$PWD/../../../../../Qt/4.8.5/include
DEPENDPATH += $$PWD/../../../../../Qt/4.8.5/include
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = testing_1
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS +=
INCLUDEPATH += tmp/moc/release_shared
unix|win32: LIBS += -L$$PWD/../../../../../Qt/4.8.5/lib/ -lQtCore4
INCLUDEPATH += $$PWD/../../../../../Qt/4.8.5/include
DEPENDPATH += $$PWD/../../../../../Qt/4.8.5/include
To copy to clipboard, switch view to plain text mode
Here is my latest experimental code if you're interested:
#include <QApplication>
#include <QtCore/QCoreApplication>
#include <iostream>
using std::cout;
using std::endl;
#include <QtDebug>
#include <QObject>
#include <QtNetwork/QTcpSocket>
class my_TCP_Q_socket_with_readyRead
: public QObject{
public:
explicit my_TCP_Q_socket_with_readyRead
(QObject *parent
= 0) : {
// ??why are you not ok taking "this" as an argument ?!
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";
}
m_socket_ptr,
SIGNAL(readyRead()),
this,
SLOT(read_it()));
}
// http://qt-project.org/wiki/Qt_for_beginners_Signals_and_slots_2
// "Even if the signal is declared as a method, there is no
// need to implement it. The meta-object compiler is used to
// do this."
Q_SIGNAL void done_reading(int num);
private:
Q_OBJECT
Q_SLOT void read_it()
{
static int read_count = 0;
qint64 bytes_available = m_socket_ptr->bytesAvailable();
if (bytes_available > 0)
{
read_count += 1;
qDebug() << "Reading '" << bytes_available << "' bytes";
m_arr = m_socket_ptr->readAll();
qDebug() << m_arr;
}
emit done_reading(read_count);
}
};
{
public:
explicit my_class
(QObject *parent
= 0) : {
}
Q_SLOT void say_something(int num)
{
cout << "hey there! number is '" << num << "'" << endl;
}
private:
Q_OBJECT
};
#include "main.moc"
int main(int argc, char *argv[])
{
int app_ret_val = 0;
//QApplication app(argc, argv);
my_TCP_Q_socket_with_readyRead s;
my_class mc;
QObject::connect(&s,
SIGNAL(done_reading
(int)),
&mc,
SLOT(say_something
(int)));
app_ret_val = app.exec();
return app_ret_val;
}
#include <QApplication>
#include <QtCore/QCoreApplication>
#include <iostream>
using std::cout;
using std::endl;
#include <QtDebug>
#include <QObject>
#include <QtNetwork/QTcpSocket>
class my_TCP_Q_socket_with_readyRead : public QObject
{
public:
explicit my_TCP_Q_socket_with_readyRead(QObject *parent = 0) :
QObject(parent)
{
// ??why are you not ok taking "this" as an argument ?!
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";
}
QObject::connect(
m_socket_ptr,
SIGNAL(readyRead()),
this,
SLOT(read_it()));
}
// http://qt-project.org/wiki/Qt_for_beginners_Signals_and_slots_2
// "Even if the signal is declared as a method, there is no
// need to implement it. The meta-object compiler is used to
// do this."
Q_SIGNAL void done_reading(int num);
private:
Q_OBJECT
QTcpSocket *m_socket_ptr;
QByteArray m_arr;
Q_SLOT void read_it()
{
static int read_count = 0;
qint64 bytes_available = m_socket_ptr->bytesAvailable();
if (bytes_available > 0)
{
read_count += 1;
qDebug() << "Reading '" << bytes_available << "' bytes";
m_arr = m_socket_ptr->readAll();
qDebug() << m_arr;
}
emit done_reading(read_count);
}
};
class my_class : public QObject
{
public:
explicit my_class(QObject *parent = 0) :
QObject(parent)
{
}
Q_SLOT void say_something(int num)
{
cout << "hey there! number is '" << num << "'" << endl;
}
private:
Q_OBJECT
};
#include "main.moc"
int main(int argc, char *argv[])
{
int app_ret_val = 0;
//QApplication app(argc, argv);
QCoreApplication app(argc, argv);
my_TCP_Q_socket_with_readyRead s;
my_class mc;
QObject::connect(&s, SIGNAL(done_reading(int)), &mc, SLOT(say_something(int)));
app_ret_val = app.exec();
return app_ret_val;
}
To copy to clipboard, switch view to plain text mode
I've been trying to hunt down how the basic QTcpSocket constructor is supposed work with the "this" argument. It is supposed to "just work" from the demos that I have seen, so I don't know where to to do when it doesn't work. Any more ideas?
Bookmarks