<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[I wanna develope chatprogram...]]></title><description><![CDATA[<p dir="auto">In chatmain.ui, there is a button to connect to the server by entering the IP and port number. When this button is clicked, a new client is created and the chatwindow.ui window is created where users can enter their username and chat with other clients. This window displays a list of connected users, a chat window, a text input field, and a send button. Clients can exchange messages in the chat window. I wrote the code for this chat program using QT C++ and QTcpSocket, but it doesn't run. Can you tell me what the problem is?</p>
<p dir="auto">chatmain.h</p>
<pre><code>#ifndef CHATMAIN_H
#define CHATMAIN_H

#include &lt;QMainWindow&gt;
#include &lt;QWidget&gt;
#include &lt;QTcpSocket&gt;
#include &lt;QTcpServer&gt;
#include &lt;QMessageBox&gt;
#include "chatwindow.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class ChatMain;
}
QT_END_NAMESPACE

class ChatMain : public QMainWindow
{
    Q_OBJECT

public:
    ChatMain(QWidget *parent = nullptr);
    ~ChatMain();

private slots:
    void connectToServer();
    void newConnectionSlot(QTcpSocket *socket, QWidget *parent);
    void broadcastMessage(const QString &amp;message);
    void clientDisconnected(QTcpSocket* socket);

private:
    Ui::ChatMain *ui;
    ChatWindow* chatwindow;
    QTcpServer* server;
    QTcpSocket* socket;
    QList&lt;ChatWindow*&gt; clients;
    int cnt = 0;
};
#endif // CHATMAIN_H

</code></pre>
<p dir="auto">chatmain.cpp</p>
<pre><code>#include "chatmain.h"
#include "ui_chatmain.h"
#include "chatwindow.h"

ChatMain::ChatMain(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::ChatMain)
{
    QMainWindow mainWindow;
    ui-&gt;setupUi(this);
    setWindowTitle("Chatting Client");
    ui-&gt;ipLineEdit-&gt;setText("192.168.219.104");

    server = new QTcpServer(this);
    connect(ui-&gt;connectBtn, SIGNAL(clicked()), this, SLOT(connectToServer()));

}

ChatMain::~ChatMain()
{
    delete ui;
    delete socket;
}


void ChatMain::connectToServer()
{
    cnt++; // connect횟수
    QString host = ui-&gt;ipLineEdit-&gt;text(); // 서버주소
    bool ok;
    qint16 port = ui-&gt;portLineEdit-&gt;text().toInt(&amp;ok);// 포트번호
    QString ports = QString::number(port);
    if (host.isEmpty()) {
        QMessageBox::critical(nullptr, "Error", "서버 주소를 입력하세요.");
        return;
    }
    if(!ok) {
        QMessageBox::critical(nullptr, "Error", "유효한 포트 번호를 입력하세요.");
        return;
    }

    if(server-&gt;listen(QHostAddress(host), port)) {
        ui-&gt;connectBtn-&gt;setText("연결 끊기");

    } else {
        ui-&gt;connect_state-&gt;append("Connection failed!");
        return;
    }


    ui-&gt;connect_state-&gt;append("Connected to server!");

    chatwindow = new ChatWindow(socket, this);
    chatwindow-&gt;show();

    connect(server, SIGNAL(newConnection()), this, SLOT(newConnectionSlot(socket, this)));

}

void ChatMain::newConnectionSlot(QTcpSocket *socket, QWidget *parent) {


    chatwindow = new ChatWindow(socket, parent);
    chatwindow-&gt;show();


    ChatWindow *client = new ChatWindow(socket, this);

    clients.append(client);

    connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected(socket)));


    client -&gt; sendMessage("USERNAME");

    // 클라이언트에게 사용자 이름 설정 요청 메세지 전송
    connect(client, SIGNAL(messageReceived(QString)), this, SLOT(broadcastMessage("USERNAME")));

}

void ChatMain::broadcastMessage(const QString &amp;message) {
    for (ChatWindow *client : clients) {
        client-&gt;sendMessage(message);
    }
}

void ChatMain::clientDisconnected(QTcpSocket* socket) {
    // 연결 종료 시 처리 내용 작성
    clients.removeOne((void*)socket);
    socket-&gt;deleteLater();
}

</code></pre>
<p dir="auto">chatwindow.h</p>
<pre><code>#ifndef CHATWINDOW_H
#define CHATWINDOW_H

#include &lt;QMainWindow&gt;
#include &lt;QObject&gt;
#include &lt;QTcpSocket&gt;

namespace Ui {
class ChatWindow;
}

class ChatWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit ChatWindow(QTcpSocket *socket, QWidget *parent = 0);
    ~ChatWindow();

signals:
    void disconnected();
    void messageReceived(QString);

private:
    Ui::ChatWindow *ui;
    QTcpSocket *socket;
    QString username;

public slots:
    void readMessage();
    void sendMessage(const QString &amp;message);
    void setUserName();

};


#endif // CHATWINDOW_H

</code></pre>
<p dir="auto">chatwindow.cpp</p>
<pre><code>#include "chatwindow.h"
#include "ui_chatwindow.h"

ChatWindow::ChatWindow(QTcpSocket *socket, QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::ChatWindow)
    , socket(socket)
{
    ui-&gt;setupUi(this);
    connect(ui-&gt;setUserName, SIGNAL(clicked()), this, SLOT(setUserName()));
    connect(parent, SIGNAL(&amp;QTcpSocket::readyRead), this, SLOT(readMessage()));
    connect(parent, SIGNAL(&amp;QTcpSocket::disconnected), this, SLOT(disconnected()));
}


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

void ChatWindow::setUserName() {
    username = ui-&gt;userName-&gt;text();

}


void ChatWindow::readMessage() {
    QByteArray data = socket-&gt;readAll();
    QString message(data);

    // 메시지 처리 (예: 사용자 이름 설정, 채팅 메시지 전송 등)
    if (message.startsWith("USERNAME")) {
        // 사용자 이름 설정
        username = ui-&gt;userName-&gt;text();
        emit messageReceived("[" + username + "] 접속했습니다.");
        ui-&gt;connectedUsers-&gt;setText("[" + username + "] 접속했습니다.");
    } else {
        // 채팅 메시지 전송
        emit messageReceived("[" + username + "]: " + message);
    }
}

void ChatWindow::sendMessage(const QString &amp;message) {
    socket-&gt;write(message.toUtf8());
}



</code></pre>
<p dir="auto">chatmain.ui<br />
<img src="https://ddgobkiprc33d.cloudfront.net/9782a6fa-fee6-4512-9fa6-eb4f35f3a082.png" alt="KakaoTalk_20240423_171041132.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">chatwindow.ui<br />
<img src="https://ddgobkiprc33d.cloudfront.net/8421b095-52db-4bb4-be71-afa5bf7879cd.png" alt="KakaoTalk_20240423_171048756.png" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/topic/156248/i-wanna-develope-chatprogram</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 00:06:48 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/156248.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 23 Apr 2024 08:27:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to I wanna develope chatprogram... on Tue, 23 Apr 2024 08:31:48 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hailee">@<bdi>hailee</bdi></a> said in <a href="/post/797678">I wanna develope chatprogram...</a>:</p>
<blockquote>
<p dir="auto">but it doesn't run. Can you tell me what the problem is?</p>
</blockquote>
<p dir="auto">Depends what "doesn't run" means....</p>
<blockquote>
<p dir="auto">connect(parent, SIGNAL(&amp;QTcpSocket::readyRead), this, SLOT(readMessage()));</p>
</blockquote>
<p dir="auto">Here at least you mix old style and new style signal/slot <code>connect()</code> syntax.  Don't, and change over to only using new style, it has been recommended for over a decade.</p>
<p dir="auto">How does <code>parent</code>, which is a <code>QWidget *</code>, have any <code>QTcpSocket</code> signals?</p>
]]></description><link>https://forum.qt.io/post/797681</link><guid isPermaLink="true">https://forum.qt.io/post/797681</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 23 Apr 2024 08:31:48 GMT</pubDate></item></channel></rss>