Reputation: 173
I have a project to do using Qt and C++. For that first, I need to create a sample Client-Server program. Using Qt Creator, I have designed the Ui and written some code for connecting and reading messages, etc.
But, I'm unable to update the UI for program from the server class.
What should I do to make it right ?
#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
#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();
}
#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;
}
#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;
}
The debug lines need to updated in the GUI where I have a text box (plaintextedit).
Upvotes: 1
Views: 3789
Reputation: 29976
You will need to make member functions in your window class, which will b responsible for outputting your text. For example,
class MainWindow : public QMainWindow
{
<...>
public slots:
<...>
void displayMessage( const QString & message );
}
Now, in this function, you can access the UI, for example:
void MainWindow::displayMessage( const QString & message )
{
ui->plainTextEdit->appendPlainText( message );
}
Now, all that you have to do from the server is to call this function and pass it the message. Since the function belongs to the MainWindow
class, it will have no problems displaying the message. You can either call it explicitly, or connect a signal to it.
Upvotes: 3