Clean up btchat example

- Replace old connect syntax
- Clean up includes
- Replace 0 with nullptr
- Add socket error handling

Change-Id: Id4c7634db29a23184d3ce2d92ffa39c71505de12
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
This commit is contained in:
Oliver Wolff 2018-08-14 10:38:48 +02:00
parent bfb696f799
commit f1aea39b23
9 changed files with 90 additions and 70 deletions

View File

@ -53,34 +53,30 @@
#include "chatserver.h" #include "chatserver.h"
#include "chatclient.h" #include "chatclient.h"
#include <qbluetoothuuid.h> #include <QtCore/qdebug.h>
#include <qbluetoothserver.h>
#include <qbluetoothservicediscoveryagent.h> #include <QtBluetooth/qbluetoothdeviceinfo.h>
#include <qbluetoothdeviceinfo.h> #include <QtBluetooth/qbluetoothlocaldevice.h>
#include <qbluetoothlocaldevice.h> #include <QtBluetooth/qbluetoothuuid.h>
#ifdef Q_OS_ANDROID #ifdef Q_OS_ANDROID
#include <QtAndroidExtras/QtAndroid> #include <QtAndroidExtras/QtAndroid>
#endif #endif
#include <QTimer>
#include <QDebug>
static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8"); static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
#ifdef Q_OS_ANDROID #ifdef Q_OS_ANDROID
static const QLatin1String reverseUuid("c8e96402-0102-cf9c-274b-701a950fe1e8"); static const QLatin1String reverseUuid("c8e96402-0102-cf9c-274b-701a950fe1e8");
#endif #endif
Chat::Chat(QWidget *parent) Chat::Chat(QWidget *parent)
: QDialog(parent), currentAdapterIndex(0), ui(new Ui_Chat) : QDialog(parent), ui(new Ui_Chat)
{ {
//! [Construct UI] //! [Construct UI]
ui->setupUi(this); ui->setupUi(this);
connect(ui->quitButton, SIGNAL(clicked()), this, SLOT(accept())); connect(ui->quitButton, &QPushButton::clicked, this, &Chat::accept);
connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(connectClicked())); connect(ui->connectButton, &QPushButton::clicked, this, &Chat::connectClicked);
connect(ui->sendButton, SIGNAL(clicked()), this, SLOT(sendClicked())); connect(ui->sendButton, &QPushButton::clicked, this, &Chat::sendClicked);
//! [Construct UI] //! [Construct UI]
localAdapters = QBluetoothLocalDevice::allDevices(); localAdapters = QBluetoothLocalDevice::allDevices();
@ -93,19 +89,21 @@ Chat::Chat(QWidget *parent)
arg(localAdapters.at(0).address().toString())); arg(localAdapters.at(0).address().toString()));
ui->secondAdapter->setText(localAdapters.at(1).address().toString()); ui->secondAdapter->setText(localAdapters.at(1).address().toString());
ui->firstAdapter->setChecked(true); ui->firstAdapter->setChecked(true);
connect(ui->firstAdapter, SIGNAL(clicked()), this, SLOT(newAdapterSelected())); connect(ui->firstAdapter, &QRadioButton::clicked, this, &Chat::newAdapterSelected);
connect(ui->secondAdapter, SIGNAL(clicked()), this, SLOT(newAdapterSelected())); connect(ui->secondAdapter, &QRadioButton::clicked, this, &Chat::newAdapterSelected);
QBluetoothLocalDevice adapter(localAdapters.at(0).address()); QBluetoothLocalDevice adapter(localAdapters.at(0).address());
adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable); adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
} }
//! [Create Chat Server] //! [Create Chat Server]
server = new ChatServer(this); server = new ChatServer(this);
connect(server, SIGNAL(clientConnected(QString)), this, SLOT(clientConnected(QString))); connect(server, QOverload<const QString &>::of(&ChatServer::clientConnected),
connect(server, SIGNAL(clientDisconnected(QString)), this, SLOT(clientDisconnected(QString))); this, &Chat::clientConnected);
connect(server, SIGNAL(messageReceived(QString,QString)), connect(server, QOverload<const QString &>::of(&ChatServer::clientDisconnected),
this, SLOT(showMessage(QString,QString))); this, QOverload<const QString &>::of(&Chat::clientDisconnected));
connect(this, SIGNAL(sendMessage(QString)), server, SLOT(sendMessage(QString))); connect(server, &ChatServer::messageReceived,
this, &Chat::showMessage);
connect(this, &Chat::sendMessage, server, &ChatServer::sendMessage);
server->startServer(); server->startServer();
//! [Create Chat Server] //! [Create Chat Server]
@ -137,6 +135,7 @@ void Chat::connected(const QString &name)
{ {
ui->chat->insertPlainText(QString::fromLatin1("Joined chat with %1.\n").arg(name)); ui->chat->insertPlainText(QString::fromLatin1("Joined chat with %1.\n").arg(name));
} }
//! [connected]
void Chat::newAdapterSelected() void Chat::newAdapterSelected()
{ {
@ -163,7 +162,11 @@ int Chat::adapterFromUserSelection() const
} }
return result; return result;
} }
//! [connected]
void Chat::reactOnSocketError(const QString &error)
{
ui->chat->insertPlainText(error);
}
//! [clientDisconnected] //! [clientDisconnected]
void Chat::clientDisconnected() void Chat::clientDisconnected()
@ -206,11 +209,15 @@ void Chat::connectClicked()
ChatClient *client = new ChatClient(this); ChatClient *client = new ChatClient(this);
qDebug() << "Connecting..."; qDebug() << "Connecting...";
connect(client, SIGNAL(messageReceived(QString,QString)), connect(client, &ChatClient::messageReceived,
this, SLOT(showMessage(QString,QString))); this, &Chat::showMessage);
connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); connect(client, &ChatClient::disconnected,
connect(client, SIGNAL(connected(QString)), this, SLOT(connected(QString))); this, QOverload<>::of(&Chat::clientDisconnected));
connect(this, SIGNAL(sendMessage(QString)), client, SLOT(sendMessage(QString))); connect(client, QOverload<const QString &>::of(&ChatClient::connected),
this, &Chat::connected);
connect(client, &ChatClient::socketErrorOccurred,
this, &Chat::reactOnSocketError);
connect(this, &Chat::sendMessage, client, &ChatClient::sendMessage);
qDebug() << "Start client"; qDebug() << "Start client";
client->startClient(service); client->startClient(service);

View File

@ -50,13 +50,9 @@
#include "ui_chat.h" #include "ui_chat.h"
#include <QDialog> #include <QtWidgets/qdialog.h>
#include <qbluetoothserviceinfo.h> #include <QtBluetooth/qbluetoothhostinfo.h>
#include <qbluetoothsocket.h>
#include <qbluetoothhostinfo.h>
#include <QDebug>
QT_USE_NAMESPACE QT_USE_NAMESPACE
@ -69,7 +65,7 @@ class Chat : public QDialog
Q_OBJECT Q_OBJECT
public: public:
Chat(QWidget *parent = 0); explicit Chat(QWidget *parent = nullptr);
~Chat(); ~Chat();
signals: signals:
@ -85,12 +81,13 @@ private slots:
void clientDisconnected(const QString &name); void clientDisconnected(const QString &name);
void clientDisconnected(); void clientDisconnected();
void connected(const QString &name); void connected(const QString &name);
void reactOnSocketError(const QString &error);
void newAdapterSelected(); void newAdapterSelected();
private: private:
int adapterFromUserSelection() const; int adapterFromUserSelection() const;
int currentAdapterIndex; int currentAdapterIndex = 0;
Ui_Chat *ui; Ui_Chat *ui;
ChatServer *server; ChatServer *server;

View File

@ -50,10 +50,10 @@
#include "chatclient.h" #include "chatclient.h"
#include <qbluetoothsocket.h> #include <QtCore/qmetaobject.h>
ChatClient::ChatClient(QObject *parent) ChatClient::ChatClient(QObject *parent)
: QObject(parent), socket(0) : QObject(parent)
{ {
} }
@ -74,9 +74,12 @@ void ChatClient::startClient(const QBluetoothServiceInfo &remoteService)
socket->connectToService(remoteService); socket->connectToService(remoteService);
qDebug() << "ConnectToService done"; qDebug() << "ConnectToService done";
connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket())); connect(socket, &QBluetoothSocket::readyRead, this, &ChatClient::readSocket);
connect(socket, SIGNAL(connected()), this, SLOT(connected())); connect(socket, &QBluetoothSocket::connected, this, QOverload<>::of(&ChatClient::connected));
connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected())); connect(socket, &QBluetoothSocket::disconnected, this, &ChatClient::disconnected);
connect(socket, QOverload<QBluetoothSocket::SocketError>::of(&QBluetoothSocket::error),
this, &ChatClient::onSocketErrorOccurred);
} }
//! [startClient] //! [startClient]
@ -84,7 +87,7 @@ void ChatClient::startClient(const QBluetoothServiceInfo &remoteService)
void ChatClient::stopClient() void ChatClient::stopClient()
{ {
delete socket; delete socket;
socket = 0; socket = nullptr;
} }
//! [stopClient] //! [stopClient]
@ -110,6 +113,18 @@ void ChatClient::sendMessage(const QString &message)
} }
//! [sendMessage] //! [sendMessage]
void ChatClient::onSocketErrorOccurred(QBluetoothSocket::SocketError error)
{
if (error == QBluetoothSocket::NoSocketError)
return;
QMetaEnum metaEnum = QMetaEnum::fromType<QBluetoothSocket::SocketError>();
QString errorString = socket->peerName() + QLatin1Char(' ')
+ metaEnum.valueToKey(error) + QLatin1String(" occurred");
emit socketErrorOccurred(errorString);
}
//! [connected] //! [connected]
void ChatClient::connected() void ChatClient::connected()
{ {

View File

@ -51,9 +51,10 @@
#ifndef CHATCLIENT_H #ifndef CHATCLIENT_H
#define CHATCLIENT_H #define CHATCLIENT_H
#include <qbluetoothserviceinfo.h> #include <QtCore/qobject.h>
#include <QtCore/QObject> #include <QtBluetooth/qbluetoothserviceinfo.h>
#include <QtBluetooth/qbluetoothsocket.h>
QT_FORWARD_DECLARE_CLASS(QBluetoothSocket) QT_FORWARD_DECLARE_CLASS(QBluetoothSocket)
@ -65,7 +66,7 @@ class ChatClient : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit ChatClient(QObject *parent = 0); explicit ChatClient(QObject *parent = nullptr);
~ChatClient(); ~ChatClient();
void startClient(const QBluetoothServiceInfo &remoteService); void startClient(const QBluetoothServiceInfo &remoteService);
@ -78,13 +79,15 @@ signals:
void messageReceived(const QString &sender, const QString &message); void messageReceived(const QString &sender, const QString &message);
void connected(const QString &name); void connected(const QString &name);
void disconnected(); void disconnected();
void socketErrorOccurred(const QString &errorString);
private slots: private slots:
void readSocket(); void readSocket();
void connected(); void connected();
void onSocketErrorOccurred(QBluetoothSocket::SocketError);
private: private:
QBluetoothSocket *socket; QBluetoothSocket *socket = nullptr;
}; };
//! [declaration] //! [declaration]

View File

@ -50,16 +50,15 @@
#include "chatserver.h" #include "chatserver.h"
#include <qbluetoothserver.h> #include <QtBluetooth/qbluetoothserver.h>
#include <qbluetoothsocket.h> #include <QtBluetooth/qbluetoothsocket.h>
#include <qbluetoothlocaldevice.h>
//! [Service UUID] //! [Service UUID]
static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8"); static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
//! [Service UUID] //! [Service UUID]
ChatServer::ChatServer(QObject *parent) ChatServer::ChatServer(QObject *parent)
: QObject(parent), rfcommServer(0) : QObject(parent)
{ {
} }
@ -75,7 +74,8 @@ void ChatServer::startServer(const QBluetoothAddress& localAdapter)
//! [Create the server] //! [Create the server]
rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected())); connect(rfcommServer, &QBluetoothServer::newConnection,
this, QOverload<>::of(&ChatServer::clientConnected));
bool result = rfcommServer->listen(localAdapter); bool result = rfcommServer->listen(localAdapter);
if (!result) { if (!result) {
qWarning() << "Cannot bind chat server to" << localAdapter.toString(); qWarning() << "Cannot bind chat server to" << localAdapter.toString();
@ -145,7 +145,7 @@ void ChatServer::stopServer()
// Close server // Close server
delete rfcommServer; delete rfcommServer;
rfcommServer = 0; rfcommServer = nullptr;
} }
//! [stopServer] //! [stopServer]
@ -166,8 +166,8 @@ void ChatServer::clientConnected()
if (!socket) if (!socket)
return; return;
connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket())); connect(socket, &QBluetoothSocket::readyRead, this, &ChatServer::readSocket);
connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); connect(socket, &QBluetoothSocket::disconnected, this, QOverload<>::of(&ChatServer::clientDisconnected));
clientSockets.append(socket); clientSockets.append(socket);
emit clientConnected(socket->peerName()); emit clientConnected(socket->peerName());
} }

View File

@ -51,11 +51,10 @@
#ifndef CHATSERVER_H #ifndef CHATSERVER_H
#define CHATSERVER_H #define CHATSERVER_H
#include <qbluetoothserviceinfo.h> #include <QtCore/qobject.h>
#include <qbluetoothaddress.h>
#include <QtCore/QObject> #include <QtBluetooth/qbluetoothaddress.h>
#include <QtCore/QList> #include <QtBluetooth/qbluetoothserviceinfo.h>
QT_FORWARD_DECLARE_CLASS(QBluetoothServer) QT_FORWARD_DECLARE_CLASS(QBluetoothServer)
QT_FORWARD_DECLARE_CLASS(QBluetoothSocket) QT_FORWARD_DECLARE_CLASS(QBluetoothSocket)
@ -68,7 +67,7 @@ class ChatServer : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit ChatServer(QObject *parent = 0); explicit ChatServer(QObject *parent = nullptr);
~ChatServer(); ~ChatServer();
void startServer(const QBluetoothAddress &localAdapter = QBluetoothAddress()); void startServer(const QBluetoothAddress &localAdapter = QBluetoothAddress());
@ -88,7 +87,7 @@ private slots:
void readSocket(); void readSocket();
private: private:
QBluetoothServer *rfcommServer; QBluetoothServer *rfcommServer = nullptr;
QBluetoothServiceInfo serviceInfo; QBluetoothServiceInfo serviceInfo;
QList<QBluetoothSocket *> clientSockets; QList<QBluetoothSocket *> clientSockets;
}; };

View File

@ -50,7 +50,7 @@
#include "chat.h" #include "chat.h"
#include <QApplication> #include <QtWidgets/qapplication.h>
//#include <QtCore/QLoggingCategory> //#include <QtCore/QLoggingCategory>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -59,7 +59,7 @@ int main(int argc, char *argv[])
QApplication app(argc, argv); QApplication app(argc, argv);
Chat d; Chat d;
QObject::connect(&d, SIGNAL(accepted()), &app, SLOT(quit())); QObject::connect(&d, &Chat::accepted, &app, &QApplication::quit);
#ifdef Q_OS_ANDROID #ifdef Q_OS_ANDROID
d.showMaximized(); d.showMaximized();

View File

@ -51,9 +51,8 @@
#include "remoteselector.h" #include "remoteselector.h"
#include "ui_remoteselector.h" #include "ui_remoteselector.h"
#include <qbluetoothdeviceinfo.h> #include <QtBluetooth/qbluetoothlocaldevice.h>
#include <qbluetoothaddress.h> #include <QtBluetooth/qbluetoothservicediscoveryagent.h>
#include <qbluetoothlocaldevice.h>
QT_USE_NAMESPACE QT_USE_NAMESPACE

View File

@ -51,13 +51,13 @@
#ifndef REMOTESELECTOR_H #ifndef REMOTESELECTOR_H
#define REMOTESELECTOR_H #define REMOTESELECTOR_H
#include <QDialog> #include <QtWidgets/qdialog.h>
#include <qbluetoothuuid.h> #include <QtBluetooth/qbluetoothaddress.h>
#include <qbluetoothserviceinfo.h> #include <QtBluetooth/qbluetoothserviceinfo.h>
#include <qbluetoothservicediscoveryagent.h> #include <QtBluetooth/qbluetoothuuid.h>
QT_FORWARD_DECLARE_CLASS(QModelIndex) QT_FORWARD_DECLARE_CLASS(QBluetoothServiceDiscoveryAgent)
QT_FORWARD_DECLARE_CLASS(QListWidgetItem) QT_FORWARD_DECLARE_CLASS(QListWidgetItem)
QT_USE_NAMESPACE QT_USE_NAMESPACE
@ -73,7 +73,7 @@ class RemoteSelector : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit RemoteSelector(const QBluetoothAddress &localAdapter, QWidget *parent = 0); explicit RemoteSelector(const QBluetoothAddress &localAdapter, QWidget *parent = nullptr);
~RemoteSelector(); ~RemoteSelector();
void startDiscovery(const QBluetoothUuid &uuid); void startDiscovery(const QBluetoothUuid &uuid);