summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2023-03-16 18:26:38 +0100
committerIvan Solovev <ivan.solovev@qt.io>2023-03-20 11:23:38 +0100
commit30bb6406024831d8f79a0718da170eff7b41ff79 (patch)
tree51d4ff3dc6888212764e7b88873bf90789c65526
parent411b863cd8f7314a20324b90ad837744f54589ae (diff)
downloadqtconnectivity-30bb6406024831d8f79a0718da170eff7b41ff79.tar.gz
BtChat example: improve code
Improve the example code according to guidelines: * fix CMakeLists.txt by using qt_standard_project_setup() and PRIVATE linking * fix memory leak in Chat dialog * fix includes * fix forward declarations and do not use QT_USE_NAMESPACE * use Qt::StringLiterals * remove outdated warning for Windows platform * split too long lines Task-number: QTBUG-111972 Pick-to: 6.5 6.5.0 Change-Id: I81e472cfacf8c6adf97b31e97254797d40b01519 Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
-rw-r--r--examples/bluetooth/btchat/CMakeLists.txt9
-rw-r--r--examples/bluetooth/btchat/chat.cpp32
-rw-r--r--examples/bluetooth/btchat/chat.h10
-rw-r--r--examples/bluetooth/btchat/chatclient.cpp8
-rw-r--r--examples/bluetooth/btchat/chatclient.h5
-rw-r--r--examples/bluetooth/btchat/chatserver.cpp13
-rw-r--r--examples/bluetooth/btchat/chatserver.h2
-rw-r--r--examples/bluetooth/btchat/main.cpp4
-rw-r--r--examples/bluetooth/btchat/remoteselector.cpp7
-rw-r--r--examples/bluetooth/btchat/remoteselector.h11
10 files changed, 54 insertions, 47 deletions
diff --git a/examples/bluetooth/btchat/CMakeLists.txt b/examples/bluetooth/btchat/CMakeLists.txt
index dc7ac2a0..ef578b88 100644
--- a/examples/bluetooth/btchat/CMakeLists.txt
+++ b/examples/bluetooth/btchat/CMakeLists.txt
@@ -1,12 +1,9 @@
-# Copyright (C) 2022 The Qt Company Ltd.
+# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(btchat LANGUAGES CXX)
-set(CMAKE_AUTOMOC ON)
-set(CMAKE_AUTOUIC ON)
-
if(NOT DEFINED INSTALL_EXAMPLESDIR)
set(INSTALL_EXAMPLESDIR "examples")
endif()
@@ -15,6 +12,8 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/bluetooth/btchat")
find_package(Qt6 REQUIRED COMPONENTS Bluetooth Core Widgets)
+qt_standard_project_setup()
+
qt_add_executable(btchat
chat.cpp chat.h chat.ui
chatclient.cpp chatclient.h
@@ -28,7 +27,7 @@ set_target_properties(btchat PROPERTIES
MACOSX_BUNDLE TRUE
)
-target_link_libraries(btchat PUBLIC
+target_link_libraries(btchat PRIVATE
Qt::Bluetooth
Qt::Core
Qt::Widgets
diff --git a/examples/bluetooth/btchat/chat.cpp b/examples/bluetooth/btchat/chat.cpp
index d8cea5b8..cdaa28d8 100644
--- a/examples/bluetooth/btchat/chat.cpp
+++ b/examples/bluetooth/btchat/chat.cpp
@@ -2,24 +2,26 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "chat.h"
-#include "remoteselector.h"
-#include "chatserver.h"
#include "chatclient.h"
+#include "chatserver.h"
+#include "remoteselector.h"
+#include "ui_chat.h"
-#include <QCoreApplication>
#include <QtCore/qdebug.h>
#include <QtBluetooth/qbluetoothdeviceinfo.h>
#include <QtBluetooth/qbluetoothlocaldevice.h>
#include <QtBluetooth/qbluetoothuuid.h>
-static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
+using namespace Qt::StringLiterals;
+
+static constexpr auto serviceUuid = "e8e10f95-1a70-4b27-9ccf-02010264e9c8"_L1;
#ifdef Q_OS_ANDROID
-static const QLatin1String reverseUuid("c8e96402-0102-cf9c-274b-701a950fe1e8");
+static constexpr auto reverseUuid = "c8e96402-0102-cf9c-274b-701a950fe1e8"_L1;
#endif
Chat::Chat(QWidget *parent)
- : QDialog(parent), ui(new Ui_Chat)
+ : QDialog(parent), ui(new Ui::Chat)
{
//! [Construct UI]
ui->setupUi(this);
@@ -28,6 +30,7 @@ Chat::Chat(QWidget *parent)
connect(ui->connectButton, &QPushButton::clicked, this, &Chat::connectClicked);
connect(ui->sendButton, &QPushButton::clicked, this, &Chat::sendClicked);
//! [Construct UI]
+ ui->connectButton->setFocus();
localAdapters = QBluetoothLocalDevice::allDevices();
if (localAdapters.size() < 2) {
@@ -49,12 +52,6 @@ Chat::Chat(QWidget *parent)
adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
} else {
qWarning("Local adapter is not found! The application might work incorrectly.");
-#ifdef Q_OS_WIN
- // WinRT implementation does not support adapter information yet. So it
- // will always return an empty list.
- qWarning("If the adapter exists, make sure to pair the devices manually before launching"
- " the chat.");
-#endif
}
//! [Create Chat Server]
@@ -78,6 +75,7 @@ Chat::~Chat()
{
qDeleteAll(clients);
delete server;
+ delete ui;
}
//! [clientConnected clientDisconnected]
@@ -162,13 +160,11 @@ void Chat::connectClicked()
if (remoteSelector.exec() == QDialog::Accepted) {
QBluetoothServiceInfo service = remoteSelector.service();
- qDebug() << "Connecting to service 2" << service.serviceName()
+ qDebug() << "Connecting to service" << service.serviceName()
<< "on" << service.device().name();
// Create client
- qDebug() << "Going to create client";
ChatClient *client = new ChatClient(this);
-qDebug() << "Connecting...";
connect(client, &ChatClient::messageReceived,
this, &Chat::showMessage);
@@ -179,7 +175,6 @@ qDebug() << "Connecting...";
connect(client, &ChatClient::socketErrorOccurred,
this, &Chat::reactOnSocketError);
connect(this, &Chat::sendMessage, client, &ChatClient::sendMessage);
-qDebug() << "Start client";
client->startClient(service);
clients.append(client);
@@ -202,7 +197,12 @@ void Chat::sendClicked()
ui->sendText->setEnabled(true);
ui->sendButton->setEnabled(true);
+#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
+ // avoid keyboard automatically popping up again on mobile devices
+ ui->sendButton->setFocus();
+#else
ui->sendText->setFocus();
+#endif
}
//! [sendClicked]
diff --git a/examples/bluetooth/btchat/chat.h b/examples/bluetooth/btchat/chat.h
index 846e91dd..a9e99395 100644
--- a/examples/bluetooth/btchat/chat.h
+++ b/examples/bluetooth/btchat/chat.h
@@ -1,13 +1,15 @@
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#include "ui_chat.h"
-
#include <QtWidgets/qdialog.h>
#include <QtBluetooth/qbluetoothhostinfo.h>
-QT_USE_NAMESPACE
+QT_BEGIN_NAMESPACE
+namespace Ui {
+ class Chat;
+}
+QT_END_NAMESPACE
class ChatServer;
class ChatClient;
@@ -41,7 +43,7 @@ private slots:
private:
int adapterFromUserSelection() const;
int currentAdapterIndex = 0;
- Ui_Chat *ui;
+ Ui::Chat *ui;
ChatServer *server;
QList<ChatClient *> clients;
diff --git a/examples/bluetooth/btchat/chatclient.cpp b/examples/bluetooth/btchat/chatclient.cpp
index c7bb3795..f69fc455 100644
--- a/examples/bluetooth/btchat/chatclient.cpp
+++ b/examples/bluetooth/btchat/chatclient.cpp
@@ -5,6 +5,10 @@
#include <QtCore/qmetaobject.h>
+#include <QtBluetooth/qbluetoothserviceinfo.h>
+
+using namespace Qt::StringLiterals;
+
ChatClient::ChatClient(QObject *parent)
: QObject(parent)
{
@@ -70,8 +74,8 @@ void ChatClient::onSocketErrorOccurred(QBluetoothSocket::SocketError error)
return;
QMetaEnum metaEnum = QMetaEnum::fromType<QBluetoothSocket::SocketError>();
- QString errorString = socket->peerName() + QLatin1Char(' ')
- + metaEnum.valueToKey(static_cast<int>(error)) + QLatin1String(" occurred");
+ QString errorString = socket->peerName() + ' '_L1
+ + metaEnum.valueToKey(static_cast<int>(error)) + " occurred"_L1;
emit socketErrorOccurred(errorString);
}
diff --git a/examples/bluetooth/btchat/chatclient.h b/examples/bluetooth/btchat/chatclient.h
index 89c4813f..829562ac 100644
--- a/examples/bluetooth/btchat/chatclient.h
+++ b/examples/bluetooth/btchat/chatclient.h
@@ -6,12 +6,9 @@
#include <QtCore/qobject.h>
-#include <QtBluetooth/qbluetoothserviceinfo.h>
#include <QtBluetooth/qbluetoothsocket.h>
-QT_FORWARD_DECLARE_CLASS(QBluetoothSocket)
-
-QT_USE_NAMESPACE
+QT_FORWARD_DECLARE_CLASS(QBluetoothServiceInfo)
//! [declaration]
class ChatClient : public QObject
diff --git a/examples/bluetooth/btchat/chatserver.cpp b/examples/bluetooth/btchat/chatserver.cpp
index 2e6c314d..7a6ef80d 100644
--- a/examples/bluetooth/btchat/chatserver.cpp
+++ b/examples/bluetooth/btchat/chatserver.cpp
@@ -6,8 +6,10 @@
#include <QtBluetooth/qbluetoothserver.h>
#include <QtBluetooth/qbluetoothsocket.h>
+using namespace Qt::StringLiterals;
+
//! [Service UUID]
-static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
+static constexpr auto serviceUuid = "e8e10f95-1a70-4b27-9ccf-02010264e9c8"_L1;
//! [Service UUID]
ChatServer::ChatServer(QObject *parent)
@@ -64,10 +66,10 @@ void ChatServer::startServer(const QBluetoothAddress& localAdapter)
//! [Service UUID set]
//! [Service Discoverability]
+ const auto groupUuid = QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::PublicBrowseGroup);
QBluetoothServiceInfo::Sequence publicBrowse;
- publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::PublicBrowseGroup));
- serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
- publicBrowse);
+ publicBrowse << QVariant::fromValue(groupUuid);
+ serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, publicBrowse);
//! [Service Discoverability]
//! [Protocol descriptor list]
@@ -122,7 +124,8 @@ void ChatServer::clientConnected()
return;
connect(socket, &QBluetoothSocket::readyRead, this, &ChatServer::readSocket);
- connect(socket, &QBluetoothSocket::disconnected, this, QOverload<>::of(&ChatServer::clientDisconnected));
+ connect(socket, &QBluetoothSocket::disconnected,
+ this, QOverload<>::of(&ChatServer::clientDisconnected));
clientSockets.append(socket);
clientNames[socket] = socket->peerName();
emit clientConnected(socket->peerName());
diff --git a/examples/bluetooth/btchat/chatserver.h b/examples/bluetooth/btchat/chatserver.h
index b7c60ef7..a8811f77 100644
--- a/examples/bluetooth/btchat/chatserver.h
+++ b/examples/bluetooth/btchat/chatserver.h
@@ -12,8 +12,6 @@
QT_FORWARD_DECLARE_CLASS(QBluetoothServer)
QT_FORWARD_DECLARE_CLASS(QBluetoothSocket)
-QT_USE_NAMESPACE
-
//! [declaration]
class ChatServer : public QObject
{
diff --git a/examples/bluetooth/btchat/main.cpp b/examples/bluetooth/btchat/main.cpp
index 1af468f0..6347c914 100644
--- a/examples/bluetooth/btchat/main.cpp
+++ b/examples/bluetooth/btchat/main.cpp
@@ -3,8 +3,8 @@
#include "chat.h"
+#include <QtCore/qloggingcategory.h>
#include <QtWidgets/qapplication.h>
-#include <QtCore/QLoggingCategory>
int main(int argc, char *argv[])
{
@@ -14,7 +14,7 @@ int main(int argc, char *argv[])
Chat d;
QObject::connect(&d, &Chat::accepted, &app, &QApplication::quit);
-#ifdef Q_OS_ANDROID
+#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
d.showMaximized();
#else
d.show();
diff --git a/examples/bluetooth/btchat/remoteselector.cpp b/examples/bluetooth/btchat/remoteselector.cpp
index a0a8430c..a7a9a00e 100644
--- a/examples/bluetooth/btchat/remoteselector.cpp
+++ b/examples/bluetooth/btchat/remoteselector.cpp
@@ -4,15 +4,20 @@
#include "remoteselector.h"
#include "ui_remoteselector.h"
+#include <QtBluetooth/qbluetoothaddress.h>
#include <QtBluetooth/qbluetoothlocaldevice.h>
#include <QtBluetooth/qbluetoothservicediscoveryagent.h>
+#include <QtBluetooth/qbluetoothuuid.h>
-QT_USE_NAMESPACE
+#include <QtWidgets/qlistwidget.h>
RemoteSelector::RemoteSelector(const QBluetoothAddress &localAdapter, QWidget *parent)
: QDialog(parent), ui(new Ui::RemoteSelector)
{
ui->setupUi(this);
+#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
+ setWindowState(Qt::WindowMaximized);
+#endif
m_discoveryAgent = new QBluetoothServiceDiscoveryAgent(localAdapter);
diff --git a/examples/bluetooth/btchat/remoteselector.h b/examples/bluetooth/btchat/remoteselector.h
index 5aa3427d..e319caa6 100644
--- a/examples/bluetooth/btchat/remoteselector.h
+++ b/examples/bluetooth/btchat/remoteselector.h
@@ -6,16 +6,15 @@
#include <QtWidgets/qdialog.h>
-#include <QtBluetooth/qbluetoothaddress.h>
#include <QtBluetooth/qbluetoothserviceinfo.h>
-#include <QtBluetooth/qbluetoothuuid.h>
-QT_FORWARD_DECLARE_CLASS(QBluetoothServiceDiscoveryAgent)
-QT_FORWARD_DECLARE_CLASS(QListWidgetItem)
+QT_BEGIN_NAMESPACE
-QT_USE_NAMESPACE
+class QBluetoothAddress;
+class QBluetoothServiceDiscoveryAgent;
+class QBluetoothUuid;
+class QListWidgetItem;
-QT_BEGIN_NAMESPACE
namespace Ui {
class RemoteSelector;
}