-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09fbc2e
commit ac0302a
Showing
15 changed files
with
1,151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<RCC> | ||
<qresource prefix="/"> | ||
<file>main.qml</file> | ||
</qresource> | ||
</RCC> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.