Skip to content

Commit

Permalink
final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
muditmahajan21 committed Jan 17, 2022
1 parent 09fbc2e commit ac0302a
Show file tree
Hide file tree
Showing 15 changed files with 1,151 additions and 0 deletions.
56 changes: 56 additions & 0 deletions QT_Chat_Client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
cmake_minimum_required(VERSION 3.14)

project(qt-chat-client LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check https://doc.qt.io/qt/deployment-android.html for more information.
# They need to be set before the find_package(...) calls below.

#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()

find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick Network REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick Network REQUIRED)

set(PROJECT_SOURCES
TcpClient.cpp
TcpClient.hpp
main.cpp
qml.qrc
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(qt-chat-client
${PROJECT_SOURCES}
)
else()
if(ANDROID)
add_library(qt-chat-client SHARED
${PROJECT_SOURCES}
)
else()
add_executable(qt-chat-client
${PROJECT_SOURCES}
)
endif()
endif()

target_compile_definitions(qt-chat-client
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(qt-chat-client
PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick Qt${QT_VERSION_MAJOR}::Network)
331 changes: 331 additions & 0 deletions QT_Chat_Client/CMakeLists.txt.user

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions QT_Chat_Client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# qt-chat-client
Simple chat client implemented using Qt 5.15

![Chat client](img/chat-client.png?raw=true "Chat Client")
35 changes: 35 additions & 0 deletions QT_Chat_Client/TcpClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "TcpClient.hpp"

TcpClient::TcpClient(QObject *parent) : QObject(parent)
{
connect(&_socket, &QTcpSocket::connected, this, &TcpClient::onConnected);
connect(&_socket, &QTcpSocket::errorOccurred, this, &TcpClient::onErrorOccurred);
connect(&_socket, &QTcpSocket::readyRead, this, &TcpClient::onReadyRead);
}

void TcpClient::connectToServer(const QString &ip, const QString &port)
{
_socket.connectToHost(ip, port.toUInt());
}

void TcpClient::sendMessage(const QString &message)
{
_socket.write(message.toUtf8());
_socket.flush();
}

void TcpClient::onConnected()
{
qInfo() << "Connected to host.";
}

void TcpClient::onReadyRead()
{
const auto message = _socket.readAll();
emit newMessage(message);
}

void TcpClient::onErrorOccurred(QAbstractSocket::SocketError error)
{
qWarning() << "Error:" << error;
}
29 changes: 29 additions & 0 deletions QT_Chat_Client/TcpClient.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef TCPCLIENT_HPP
#define TCPCLIENT_HPP

#include <QObject>
#include <QTcpSocket>

class TcpClient : public QObject
{
Q_OBJECT
public:
explicit TcpClient(QObject *parent = nullptr);

signals:
void newMessage(const QByteArray &ba);

public slots:
void connectToServer(const QString &ip, const QString &port);
void sendMessage(const QString &message);

private slots:
void onConnected();
void onReadyRead();
void onErrorOccurred(QAbstractSocket::SocketError error);

private:
QTcpSocket _socket;
};

#endif // TCPCLIENT_HPP
27 changes: 27 additions & 0 deletions QT_Chat_Client/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "TcpClient.hpp"

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

QGuiApplication app(argc, argv);

QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);

TcpClient client;
engine.rootContext()->setContextProperty("client", &client);
engine.load(url);

return app.exec();
}
76 changes: 76 additions & 0 deletions QT_Chat_Client/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

Window {
width: 640
height: 480
visible: true
title: qsTr("Chat Client")

Connections {
target: client
function onNewMessage(ba) {
listModelMessages.append({
message: ba + ""
})
}
}

ColumnLayout {
anchors.fill: parent
RowLayout {
Layout.fillWidth: true
TextField {
id: textFieldIp
placeholderText: qsTr("Server IP")
Layout.fillWidth: true
onAccepted: buttonConnect.clicked()
}
TextField {
id: textFieldPort
placeholderText: qsTr("Server port")
Layout.fillWidth: true
onAccepted: buttonConnect.clicked()
}
Button {
id: buttonConnect
text: qsTr("Connect")
onClicked: client.connectToServer(textFieldIp.text, textFieldPort.text)
}
}
ListView {
Layout.fillHeight: true
Layout.fillWidth: true
clip: true
model: ListModel {
id: listModelMessages
ListElement {
message: "Welcome to chat client"
}
}
delegate: ItemDelegate {
text: message
}
ScrollBar.vertical: ScrollBar {}
}
RowLayout {
Layout.fillWidth: true
TextField {
id: textFieldMessage
placeholderText: qsTr("Your message ...")
Layout.fillWidth: true
onAccepted: buttonSend.clicked()
}
Button {
id: buttonSend
text: qsTr("Send")
onClicked: {
client.sendMessage(textFieldMessage.text)
textFieldMessage.clear()
}
}
}
}
}
5 changes: 5 additions & 0 deletions QT_Chat_Client/qml.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
68 changes: 68 additions & 0 deletions QT_Chat_Server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
cmake_minimum_required(VERSION 3.14)

project(QT_Chat_Server VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check https://doc.qt.io/qt/deployment-android.html for more information.
# They need to be set before the find_package(...) calls below.

#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()

find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick Network REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick Network REQUIRED)

set(PROJECT_SOURCES
tcpserver.cpp
tcpserver.hpp
main.cpp
qml.qrc
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(QT_Chat_Server
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
else()
if(ANDROID)
add_library(QT_Chat_Server SHARED
${PROJECT_SOURCES}
)
else()
add_executable(QT_Chat_Server
${PROJECT_SOURCES}
)
endif()
endif()

target_compile_definitions(QT_Chat_Server
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(QT_Chat_Server
PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick Qt${QT_VERSION_MAJOR}::Network)

set_target_properties(QT_Chat_Server PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)

if(QT_VERSION_MAJOR EQUAL 6)
qt_import_qml_plugins(QT_Chat_Server)
qt_finalize_executable(QT_Chat_Server)
endif()
Loading

0 comments on commit ac0302a

Please sign in to comment.