0

我将首先发布代码,因为之后它会更容易理解。

基本上,我有一个默认类 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

主窗口.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();
}

后面是一个带有pushButtontextBox的简单表单。

第二类:

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

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,我想将一些数据从类 tcpsocket 传递 MainWindow

main.cpp是默认代码。我认为这无关紧要。

更具体地说,我希望来自tcpsocket 类的信号触发来自MainWindow 类的插槽:

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());

....

第二个问题是如何将一些数据从tcpsocket 类传递给MainWindow 类

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 );

}

应用程序运行完美,内容被获取。但我只能使用 qDebug(); 查看获取的内容;所以我想将对 GUI 的响应传递给一个文本框。

4

0 回答 0