0

I will post the code first as it will be easier to understand after that.

Basically, I have a default class, MainWindow:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>

#include "tcpsocket.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    //This is my class
    tcpsocket *socket;

public slots:
    void updateUI(QString);

private slots:
    void on_connectButton_clicked();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::updateUI(QString msg)
{
    qDebug() << "GOOD, SLOT ACCESSED FROM ANOTHER CLASS";
    ui->responseBox->setText(msg);
}

void MainWindow::on_connectButton_clicked()
{
    socket = new tcpsocket();
    socket->doConnect();
}

Behind is a simple form with a pushButton and a textBox.

And the second class:

tcpsocket.h

#ifndef TCPSOCKET_H
#define TCPSOCKET_H

#include <QMainWindow>
#include <QObject>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QBitArray>
#include <QDebug>

#include "mainwindow.h"

class tcpsocket : public QMainWindow
{
    Q_OBJECT
public:

    explicit tcpsocket(QWidget *parent = 0);
    void doConnect();

signals:

public slots:
    void connected();
    void disconnected();
    void bytesWritten(qint64 bytes);
    void readyRead();

private:
    QTcpSocket *socket;
};

#endif // TCPSOCKET_H

and tcpsocket.cpp

#include "tcpsocket.h"

tcpsocket::tcpsocket(QWidget *parent) : QMainWindow(parent)
{

}

void tcpsocket::doConnect()
{
    socket = new QTcpSocket(this);

    connect(socket, SIGNAL(connected()),this, SLOT(connected()));
    connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
    connect(socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
    connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));

    //And here I want to add a signal to a slor from main window.
    //Something like this:
    // connect(socket, SIGNAL(readyRead()), MainWindow, SLOT(updateUI());

    qDebug() << "connecting...";

    // this is not blocking call
    socket->connectToHost("google.com", 80);

    // we need to wait...
    if(!socket->waitForConnected(5000))
    {
        qDebug() << "Error: " << socket->errorString();
    }
}

void tcpsocket::connected()
{
    qDebug() << "connected...";

    // Hey server, tell me about you.
    socket->write("HEAD / HTTP/1.0\r\n\r\n\r\n\r\n");
}

void tcpsocket::disconnected()
{
    qDebug() << "disconnected...";
}

void tcpsocket::bytesWritten(qint64 bytes)
{
    qDebug() << bytes << " bytes written...";
}

void tcpsocket::readyRead()
{
    qDebug() << "reading response...";
    QString data = socket->readAll();
    qDebug() << data;

    //Here, I want to pass somehow this content to function updateUI, from MainWindow
    // MainWindow::updateUI( data );

}

Bsically, I want to pass some data from class tcpsocket to class MainWindow

main.cpp is default code. I think it is not relevant.

To be more specific, I want that a signal from tcpsocket class to trigger a slot from MainWindow class:

void tcpsocket::doConnect()
{
    socket = new QTcpSocket(this);

    //local slots
    connect(socket, SIGNAL(connected()),this, SLOT(connected()));
    connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
    connect(socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
    connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));

    //And here I want to add a signal to a slot from main window.
    //Something like this:
    // connect(socket, SIGNAL(readyRead()), MainWindow, SLOT(updateUI());

....

And the second question is how to pass some data from tcpsocket class to MainWindow class:

void tcpsocket::readyRead()
{
    qDebug() << "reading response...";
    QString data = socket->readAll();
    qDebug() << data;

    //Here, I want to pass somehow this content to function updateUI, from MainWindow
    // MainWindow::updateUI( data );

}

The application run perfectly and content is fetched. But I can view the fetched content only using qDebug(); so I want to pass response to GUI, into a textBox.

caffeine
  • 435
  • 7
  • 26
  • 3
    what's your problem in connecting the socket to your main window? seems like you create the socket in your main window and even have the pointer to your main window in the socket? so you can just connect it in both places (I'd prefer to do the connection in the parent class). with your second question it's essentially the same, you should use a signal or just call the method directely – xander Mar 20 '17 at 12:38
  • 2
    by the way I just noticed your socket class is derived from `QMainWindow`? why is that: `class tcpsocket : public QMainWindow`, should it not be `class tcpsocket : public QTcpSocket`? or just `QObject` if you want to keep the `QTcpSocket` as a member variable. – xander Mar 20 '17 at 12:44
  • 2
    Are you sure that you want your TcpSocket inherit from QMainWindow class? `class tcpsocket : public QMainWindow` – Dmitriy Mar 20 '17 at 12:48
  • How to access pointer to MainWindow from tcpsocket class? – caffeine Mar 20 '17 at 14:10
  • 1
    well if you create your socket like `socket = new tcpsocket(this);` you can just use the `parent()` pointer anywhere in your class to access your `MainWindow`. Of course you can also save a pointer in a custom member variable, but setting the main windows as a parent makes sense I guess. – xander Mar 20 '17 at 14:38
  • And this is the problem. If i use `parent()->updateUI("something");` then i get: `‘class QObject’ has no member named ‘updateUI’` – caffeine Mar 20 '17 at 16:58
  • After some dig, the solution was the following: `((MainWindow*)(parent()))->updateUI("");` – caffeine Mar 20 '17 at 22:55

0 Answers0