diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-19 11:39:31 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-19 14:39:07 +0200 |
commit | cd540c71ba00b995c484e465abeb93efe171424b (patch) | |
tree | 436ac509cd112442d5abf293957a6e91fb8209f1 /examples | |
parent | 2a7ef291d696745887e71f8b0e27cb4c8701bdd2 (diff) |
Polish the btscanner example
- Use Qt 5 connection syntax
- Use a QDialogButtonBox instead of a single button in the service dialog
- Forward-declare the UI classes
- Streamline code
Pick-to: 6.2
Change-Id: I30db78043a8e3a62ea10223252a59b8415296e55
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/bluetooth/btscanner/device.cpp | 110 | ||||
-rw-r--r-- | examples/bluetooth/btscanner/device.h | 7 | ||||
-rw-r--r-- | examples/bluetooth/btscanner/main.cpp | 8 | ||||
-rw-r--r-- | examples/bluetooth/btscanner/service.cpp | 24 | ||||
-rw-r--r-- | examples/bluetooth/btscanner/service.h | 5 | ||||
-rw-r--r-- | examples/bluetooth/btscanner/service.ui | 63 |
6 files changed, 97 insertions, 120 deletions
diff --git a/examples/bluetooth/btscanner/device.cpp b/examples/bluetooth/btscanner/device.cpp index bf882265..33e29e07 100644 --- a/examples/bluetooth/btscanner/device.cpp +++ b/examples/bluetooth/btscanner/device.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtBluetooth module of the Qt Toolkit. @@ -50,49 +50,57 @@ #include "device.h" #include "service.h" +#include "ui_device.h" #include <qbluetoothaddress.h> #include <qbluetoothdevicediscoveryagent.h> #include <qbluetoothlocaldevice.h> + #include <QMenu> #include <QDebug> -DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent) -: QDialog(parent), localDevice(new QBluetoothLocalDevice), +static QColor colorForPairing(QBluetoothLocalDevice::Pairing pairing) +{ + return pairing == QBluetoothLocalDevice::Paired + || pairing == QBluetoothLocalDevice::AuthorizedPaired + ? QColor(Qt::green) : QColor(Qt::red); +} + +DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent) : + QDialog(parent), + localDevice(new QBluetoothLocalDevice), ui(new Ui_DeviceDiscovery) { ui->setupUi(this); - /* - * In case of multiple Bluetooth adapters it is possible to set adapter - * which will be used. Example code: - * - * QBluetoothAddress address("XX:XX:XX:XX:XX:XX"); - * discoveryAgent = new QBluetoothDeviceDiscoveryAgent(address); - * - **/ + // In case of multiple Bluetooth adapters it is possible to set the adapter + // to be used. Example code: + // + // QBluetoothAddress address("XX:XX:XX:XX:XX:XX"); + // discoveryAgent = new QBluetoothDeviceDiscoveryAgent(address); discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); - connect(ui->scan, SIGNAL(clicked()), this, SLOT(startScan())); + connect(ui->scan, &QAbstractButton::clicked, this, &DeviceDiscoveryDialog::startScan); - connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), - this, SLOT(addDevice(QBluetoothDeviceInfo))); - connect(discoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished())); + connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, + this, &DeviceDiscoveryDialog::addDevice); + connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, + this, &DeviceDiscoveryDialog::scanFinished); - connect(ui->list, SIGNAL(itemActivated(QListWidgetItem*)), - this, SLOT(itemActivated(QListWidgetItem*))); + connect(ui->list, &QListWidget::itemActivated, + this, &DeviceDiscoveryDialog::itemActivated); - connect(localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)), - this, SLOT(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); + connect(localDevice, &QBluetoothLocalDevice::hostModeStateChanged, + this, &DeviceDiscoveryDialog::hostModeStateChanged); hostModeStateChanged(localDevice->hostMode()); // add context menu for devices to be able to pair device ui->list->setContextMenuPolicy(Qt::CustomContextMenu); - connect(ui->list, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(displayPairingMenu(QPoint))); - connect(localDevice, SIGNAL(pairingFinished(QBluetoothAddress,QBluetoothLocalDevice::Pairing)) - , this, SLOT(pairingDone(QBluetoothAddress,QBluetoothLocalDevice::Pairing))); - + connect(ui->list, &QWidget::customContextMenuRequested, + this, &DeviceDiscoveryDialog::displayPairingMenu); + connect(localDevice, &QBluetoothLocalDevice::pairingFinished, + this, &DeviceDiscoveryDialog::pairingDone); } DeviceDiscoveryDialog::~DeviceDiscoveryDialog() @@ -102,19 +110,14 @@ DeviceDiscoveryDialog::~DeviceDiscoveryDialog() void DeviceDiscoveryDialog::addDevice(const QBluetoothDeviceInfo &info) { - QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name()); - QList<QListWidgetItem *> items = ui->list->findItems(label, Qt::MatchExactly); - if (items.empty()) { + const QString label = info.address().toString() + u' ' + info.name(); + const auto items = ui->list->findItems(label, Qt::MatchExactly); + if (items.isEmpty()) { QListWidgetItem *item = new QListWidgetItem(label); QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(info.address()); - if (pairingStatus == QBluetoothLocalDevice::Paired || pairingStatus == QBluetoothLocalDevice::AuthorizedPaired ) - item->setForeground(QColor(Qt::green)); - else - item->setForeground(QColor(Qt::black)); - + item->setForeground(colorForPairing(pairingStatus)); ui->list->addItem(item); } - } void DeviceDiscoveryDialog::startScan() @@ -130,10 +133,8 @@ void DeviceDiscoveryDialog::scanFinished() void DeviceDiscoveryDialog::itemActivated(QListWidgetItem *item) { - QString text = item->text(); - - int index = text.indexOf(' '); - + const QString text = item->text(); + const auto index = text.indexOf(' '); if (index == -1) return; @@ -162,19 +163,10 @@ void DeviceDiscoveryDialog::on_power_clicked(bool clicked) void DeviceDiscoveryDialog::hostModeStateChanged(QBluetoothLocalDevice::HostMode mode) { - if (mode != QBluetoothLocalDevice::HostPoweredOff) - ui->power->setChecked(true); - else - ui->power->setChecked( false); - - if (mode == QBluetoothLocalDevice::HostDiscoverable) - ui->discoverable->setChecked(true); - else - ui->discoverable->setChecked(false); - - bool on = !(mode == QBluetoothLocalDevice::HostPoweredOff); - + ui->power->setChecked(mode != QBluetoothLocalDevice::HostPoweredOff); + ui->discoverable->setChecked(mode == QBluetoothLocalDevice::HostDiscoverable); + const bool on = mode != QBluetoothLocalDevice::HostPoweredOff; ui->scan->setEnabled(on); ui->discoverable->setEnabled(on); } @@ -189,7 +181,7 @@ void DeviceDiscoveryDialog::displayPairingMenu(const QPoint &pos) QListWidgetItem *currentItem = ui->list->currentItem(); QString text = currentItem->text(); - int index = text.indexOf(' '); + const auto index = text.indexOf(' '); if (index == -1) return; @@ -200,19 +192,11 @@ void DeviceDiscoveryDialog::displayPairingMenu(const QPoint &pos) localDevice->requestPairing(address, QBluetoothLocalDevice::Unpaired); } } -void DeviceDiscoveryDialog::pairingDone(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing) +void DeviceDiscoveryDialog::pairingDone(const QBluetoothAddress &address, + QBluetoothLocalDevice::Pairing pairing) { - QList<QListWidgetItem *> items = ui->list->findItems(address.toString(), Qt::MatchContains); - - if (pairing == QBluetoothLocalDevice::Paired || pairing == QBluetoothLocalDevice::AuthorizedPaired ) { - for (int var = 0; var < items.count(); ++var) { - QListWidgetItem *item = items.at(var); - item->setForeground(QColor(Qt::green)); - } - } else { - for (int var = 0; var < items.count(); ++var) { - QListWidgetItem *item = items.at(var); - item->setForeground(QColor(Qt::red)); - } - } + const auto items = ui->list->findItems(address.toString(), Qt::MatchContains); + const QColor color = colorForPairing(pairing); + for (auto *item : items) + item->setForeground(color); } diff --git a/examples/bluetooth/btscanner/device.h b/examples/bluetooth/btscanner/device.h index 15082141..0bad0361 100644 --- a/examples/bluetooth/btscanner/device.h +++ b/examples/bluetooth/btscanner/device.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtBluetooth module of the Qt Toolkit. @@ -51,14 +51,15 @@ #ifndef DEVICE_H #define DEVICE_H -#include "ui_device.h" - #include <qbluetoothlocaldevice.h> #include <QDialog> QT_FORWARD_DECLARE_CLASS(QBluetoothDeviceDiscoveryAgent) QT_FORWARD_DECLARE_CLASS(QBluetoothDeviceInfo) +QT_FORWARD_DECLARE_CLASS(QListWidgetItem) + +QT_FORWARD_DECLARE_CLASS(Ui_DeviceDiscovery) QT_USE_NAMESPACE diff --git a/examples/bluetooth/btscanner/main.cpp b/examples/bluetooth/btscanner/main.cpp index 156f02ad..4752a11d 100644 --- a/examples/bluetooth/btscanner/main.cpp +++ b/examples/bluetooth/btscanner/main.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtBluetooth module of the Qt Toolkit. @@ -59,11 +59,7 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); DeviceDiscoveryDialog d; - QObject::connect(&d, SIGNAL(accepted()), &app, SLOT(quit())); - d.show(); - - app.exec(); - + d.exec(); return 0; } diff --git a/examples/bluetooth/btscanner/service.cpp b/examples/bluetooth/btscanner/service.cpp index 12e8bd2b..51cb19a1 100644 --- a/examples/bluetooth/btscanner/service.cpp +++ b/examples/bluetooth/btscanner/service.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Copyright (C) 2013 BlackBerry Limited. All rights reserved. ** Contact: https://www.qt.io/licensing/ ** @@ -50,6 +50,7 @@ ****************************************************************************/ #include "service.h" +#include "ui_service.h" #include <qbluetoothaddress.h> #include <qbluetoothservicediscoveryagent.h> @@ -68,14 +69,12 @@ ServiceDiscoveryDialog::ServiceDiscoveryDialog(const QString &name, QBluetoothLocalDevice localDevice; QBluetoothAddress adapterAddress = localDevice.address(); - /* - * In case of multiple Bluetooth adapters it is possible to - * set which adapter will be used by providing MAC Address. - * Example code: - * - * QBluetoothAddress adapterAddress("XX:XX:XX:XX:XX:XX"); - * discoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress); - */ + // In case of multiple Bluetooth adapters it is possible to + // set which adapter will be used by providing MAC Address. + // Example code: + // + // QBluetoothAddress adapterAddress("XX:XX:XX:XX:XX:XX"); + // discoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress); discoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress); @@ -83,9 +82,10 @@ ServiceDiscoveryDialog::ServiceDiscoveryDialog(const QString &name, setWindowTitle(name); - connect(discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), - this, SLOT(addService(QBluetoothServiceInfo))); - connect(discoveryAgent, SIGNAL(finished()), ui->status, SLOT(hide())); + connect(discoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered, + this, &ServiceDiscoveryDialog::addService); + connect(discoveryAgent, &QBluetoothServiceDiscoveryAgent::finished, + ui->status, &QWidget::hide); discoveryAgent->start(); } diff --git a/examples/bluetooth/btscanner/service.h b/examples/bluetooth/btscanner/service.h index 93d1e12b..1c6a7867 100644 --- a/examples/bluetooth/btscanner/service.h +++ b/examples/bluetooth/btscanner/service.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtBluetooth module of the Qt Toolkit. @@ -51,7 +51,6 @@ #ifndef SERVICE_H #define SERVICE_H -#include "ui_service.h" #include <QDialog> @@ -59,6 +58,8 @@ QT_FORWARD_DECLARE_CLASS(QBluetoothAddress) QT_FORWARD_DECLARE_CLASS(QBluetoothServiceInfo) QT_FORWARD_DECLARE_CLASS(QBluetoothServiceDiscoveryAgent) +QT_FORWARD_DECLARE_CLASS(Ui_ServiceDiscovery) + QT_USE_NAMESPACE class ServiceDiscoveryDialog : public QDialog diff --git a/examples/bluetooth/btscanner/service.ui b/examples/bluetooth/btscanner/service.ui index 68ebc5dc..4ca12ee0 100644 --- a/examples/bluetooth/btscanner/service.ui +++ b/examples/bluetooth/btscanner/service.ui @@ -6,8 +6,8 @@ <rect> <x>0</x> <y>0</y> - <width>400</width> - <height>300</height> + <width>539</width> + <height>486</height> </rect> </property> <property name="windowTitle"> @@ -25,50 +25,45 @@ </widget> </item> <item> - <layout class="QHBoxLayout" name="horizontalLayout"> - <item> - <spacer name="horizontalSpacer"> - <property name="orientation"> - <enum>Qt::Horizontal</enum> - </property> - <property name="sizeHint" stdset="0"> - <size> - <width>40</width> - <height>20</height> - </size> - </property> - </spacer> - </item> - <item> - <widget class="QPushButton" name="close"> - <property name="text"> - <string>Close</string> - </property> - </widget> - </item> - </layout> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="standardButtons"> + <set>QDialogButtonBox::Close</set> + </property> + </widget> </item> </layout> </widget> - <tabstops> - <tabstop>list</tabstop> - <tabstop>close</tabstop> - </tabstops> <resources/> <connections> <connection> - <sender>close</sender> - <signal>clicked()</signal> + <sender>buttonBox</sender> + <signal>accepted()</signal> <receiver>ServiceDiscovery</receiver> <slot>accept()</slot> <hints> <hint type="sourcelabel"> - <x>350</x> - <y>277</y> + <x>396</x> + <y>457</y> + </hint> + <hint type="destinationlabel"> + <x>535</x> + <y>443</y> + </hint> + </hints> + </connection> + <connection> + <sender>buttonBox</sender> + <signal>rejected()</signal> + <receiver>ServiceDiscovery</receiver> + <slot>reject()</slot> + <hints> + <hint type="sourcelabel"> + <x>339</x> + <y>464</y> </hint> <hint type="destinationlabel"> - <x>237</x> - <y>269</y> + <x>535</x> + <y>368</y> </hint> </hints> </connection> |