在Qt上将信号和数据从一个类传递到另一个类

时间:2017-03-20 12:17:13

标签: c++ qt qt-signals qt-slot

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

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

后面是一个简单的表单,其中包含 pushButton textBox

第二节课:

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

}

实际上,我想将类tcpsocket 中的一些数据传递给 MainWindow

main.cpp 是默认代码。我认为这不相关。

更具体地说,我希望来自 tcpsocket class 的信号从 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的响应传递给textBox。

0 个答案:

没有答案