Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9237141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:28:04+00:00 2026-06-18T07:28:04+00:00

I have a project to do using Qt and C++. For that first, I

  • 0

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 ?

mainwindow.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

serverwin.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();
}

mainwindow.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;
}

serverwin.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;
}

The debug lines need to updated in the GUI where I have a text box (plaintextedit).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:28:05+00:00Added an answer on June 18, 2026 at 7:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that using some third-party libraries. My questions is how to
I have an ASP.Net project using AJAX that I am putting on a server
I'm using Core Plot 0.9 in my first iPad project. I need to create
I have a project where I need to create a desktop app that acts
I have a lein project (using cascalog--but that's not particularly important). I'm trying to
I have recently just created Java project using Eclipse that requires 2 JAR files
We have a project that’s using many C++11 facilities, and we thought about this
I have a project that's using git. When I received the repo, it had
I have a WPF project using CM. I have a progress bar that I
I have got a project that is using jni to connect java wrapper and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.