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

View File

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

View File

@ -50,10 +50,10 @@
#include "chatclient.h"
#include <qbluetoothsocket.h>
#include <QtCore/qmetaobject.h>
ChatClient::ChatClient(QObject *parent)
: QObject(parent), socket(0)
: QObject(parent)
{
}
@ -74,9 +74,12 @@ void ChatClient::startClient(const QBluetoothServiceInfo &remoteService)
socket->connectToService(remoteService);
qDebug() << "ConnectToService done";
connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
connect(socket, &QBluetoothSocket::readyRead, this, &ChatClient::readSocket);
connect(socket, &QBluetoothSocket::connected, this, QOverload<>::of(&ChatClient::connected));
connect(socket, &QBluetoothSocket::disconnected, this, &ChatClient::disconnected);
connect(socket, QOverload<QBluetoothSocket::SocketError>::of(&QBluetoothSocket::error),
this, &ChatClient::onSocketErrorOccurred);
}
//! [startClient]
@ -84,7 +87,7 @@ void ChatClient::startClient(const QBluetoothServiceInfo &remoteService)
void ChatClient::stopClient()
{
delete socket;
socket = 0;
socket = nullptr;
}
//! [stopClient]
@ -110,6 +113,18 @@ void ChatClient::sendMessage(const QString &message)
}
//! [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]
void ChatClient::connected()
{

View File

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

View File

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

View File

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

View File

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

View File

@ -51,14 +51,13 @@
#include "remoteselector.h"
#include "ui_remoteselector.h"
#include <qbluetoothdeviceinfo.h>
#include <qbluetoothaddress.h>
#include <qbluetoothlocaldevice.h>
#include <QtBluetooth/qbluetoothlocaldevice.h>
#include <QtBluetooth/qbluetoothservicediscoveryagent.h>
QT_USE_NAMESPACE
RemoteSelector::RemoteSelector(const QBluetoothAddress &localAdapter, QWidget *parent)
: QDialog(parent), ui(new Ui::RemoteSelector)
: QDialog(parent), ui(new Ui::RemoteSelector)
{
ui->setupUi(this);

View File

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