1

我有一个使用 Qt 和 C++ 的项目要做。首先,我需要创建一个示例客户端-服务器程序。使用 Qt Creator,我设计了 Ui 并编写了一些用于连接和读取消息等的代码。

但是,我无法从服务器类更新程序的 UI。

我应该怎么做才能使它正确?

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>
#include "serverwin.h"
#include <QtNetwork>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

public slots:
    void connectClicked();
    void disconnectClicked();

private:
    Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H

服务器win.h

#ifndef SERVERWIN_H
#define SERVERWIN_H

#include <mainwindow.h>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>



namespace server{
class MainWindow;
}

class ServerWin : public QTcpServer
{
    Q_OBJECT

    public:
        ServerWin(QObject* parent);

    protected:
        void incomingConnection(int socketfd);

    private slots:
        void readyRead();
        void disconnected();

    private:
        QSet<QTcpSocket*> clients;
};

#endif // SERVERWIN_H

主文件

#include <QApplication>
#include "mainwindow.h"
#include "serverwin.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow *uiwindow = new MainWindow();

    uiwindow->show();

    return a.exec();
}

主窗口.cpp

#include "mainwindow.h"
#include "ui_server.h"
#include "serverwin.h"
#include "QtNetwork/QTcpSocket"


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

    ui->disconnectButton->setEnabled(false);
}

void MainWindow::connectClicked()
{
    ui->connectButton->setEnabled(false);

    ui->disconnectButton->setEnabled(true);

    bool success = serv->listen(QHostAddress::Any, 4200);

    if(!success)
    {
        ui->plainTextEdit->appendPlainText("Could not connect to port 4200, check your firewall settings.");
    }
    else
    {
        ui->plainTextEdit->appendPlainText("Connected");
    }
}

//close server request by user

void MainWindow::disconnectClicked()
{
    ui->disconnectButton->setEnabled(false);

    ui->connectButton->setEnabled(true);

    ui->plainTextEdit->appendPlainText("doesn't work, code to be added");
    //disconnect server
}

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

服务器win.cpp

#include "serverwin.h"

ServerWin::ServerWin(QObject *parent)
{
}

void ServerWin::incomingConnection(int socketfd)
{
    QTcpSocket *client = new QTcpSocket(this);

    client->setSocketDescriptor(socketfd);

    clients.insert(client);

    qDebug() << "Client " + client->peerAddress().toString() + " is connected.";

    connect(client, SIGNAL(readyRead()), this, SLOT(readyRead()));

    connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
}

void ServerWin::readyRead()
{
    QTcpSocket *client = (QTcpSocket*)sender();

    while (client->canReadLine())
    {
        QString msg = "Client " + client->peerAddress().toString() + " says: "  + QString::fromUtf8(client->readLine()).trimmed();

        foreach (QTcpSocket *otherClient, clients)
            otherClient->write(QString(msg + "\n").toUtf8());

        qDebug() << msg;
    }
}

// when a client is disconnected

void ServerWin::disconnected()
{
    QTcpSocket *client = (QTcpSocket*)sender();

    clients.remove(client);

    QString notification = "Client " + client->peerAddress().toString() + " has Left.";

    foreach (QTcpSocket *otherClient, clients)
        otherClient->write(QString(notification + "\n").toUtf8());

    qDebug() << notification;
}

调试行需要在我有一个文本框 (plaintextedit) 的 GUI 中更新。

4

1 回答 1

3

您需要在窗口类中创建成员函数,该函数将负责输出您的文本。例如,

class MainWindow : public QMainWindow
{
<...>
public slots:
    <...>
    void displayMessage( const QString & message );
}

现在,在这个函数中,您可以访问 UI,例如:

void MainWindow::displayMessage( const QString & message )
{
    ui->plainTextEdit->appendPlainText( message );
}

现在,您只需从服务器调用此函数并将消息传递给它即可。由于该函数属于MainWindow该类,因此显示消息不会有任何问题。您可以显式调用它,也可以将信号连接到它。

于 2013-02-04T10:35:17.867 回答