gpt4 book ai didi

c++ - Qt/C++ -- 从另一个类更新 UI

转载 作者:行者123 更新时间:2023-11-30 02:57:22 25 4
gpt4 key购买 nike

我有一个项目要使用 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

main.cpp

#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 中更新。

最佳答案

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

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

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

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

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

关于c++ - Qt/C++ -- 从另一个类更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14684228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com