diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2023-03-17 13:53:40 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2023-03-22 13:45:03 +0000 |
commit | c9b61fc300ef7a1d94e95c94a428fe84061fa651 (patch) | |
tree | a1fcee8ed80ee428e73804de770f4de2b2fbb9e2 | |
parent | 2f036252a7162ca016ce2597825e1dfb07e00a52 (diff) | |
download | qtconnectivity-c9b61fc300ef7a1d94e95c94a428fe84061fa651.tar.gz |
BtScanner example: move to manual tests
The example mostly shows the usage of
QBluetooth{Device,Service}DiscoveryAgent classes, which is also
illustrated by other examples. Move it, because it's not fully
functional on mobile platforms (because it's widget based).
Do not remove it completely, because it might still be useful
for doing Classic device scan.
Also implement some improvements while on it:
* fix CMakeLists.txt by using qt_standard_project_setup() and PRIVATE
linking
* fix memory leak in DeviceDiscoveryDialog
* rework the code to use Qt parent-child model instead of manually
deleting objects where possible
* fix includes
* fix forward declarations
Task-number: QTBUG-111972
Change-Id: Ie4bc9e25ccdda6d5f5de2f57528df349c71cdc12
Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
(cherry picked from commit 6168203d7c0e8e4df1908a62338b8abdd57855eb)
-rw-r--r-- | examples/bluetooth/CMakeLists.txt | 1 | ||||
-rw-r--r-- | examples/bluetooth/bluetooth.pro | 3 | ||||
-rw-r--r-- | examples/bluetooth/btscanner/doc/images/btscanner-example.png | bin | 112720 -> 0 bytes | |||
-rw-r--r-- | examples/bluetooth/btscanner/doc/src/btscanner.qdoc | 17 | ||||
-rw-r--r-- | src/bluetooth/doc/src/bluetooth-index.qdoc | 1 | ||||
-rw-r--r-- | src/bluetooth/doc/src/examples.qdoc | 3 | ||||
-rw-r--r-- | tests/manual/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/manual/examples/btscanner/CMakeLists.txt (renamed from examples/bluetooth/btscanner/CMakeLists.txt) | 9 | ||||
-rw-r--r-- | tests/manual/examples/btscanner/Info.plist (renamed from examples/bluetooth/btscanner/Info.plist) | 0 | ||||
-rw-r--r-- | tests/manual/examples/btscanner/btscanner.pro (renamed from examples/bluetooth/btscanner/btscanner.pro) | 2 | ||||
-rw-r--r-- | tests/manual/examples/btscanner/device.cpp (renamed from examples/bluetooth/btscanner/device.cpp) | 22 | ||||
-rw-r--r-- | tests/manual/examples/btscanner/device.h (renamed from examples/bluetooth/btscanner/device.h) | 27 | ||||
-rw-r--r-- | tests/manual/examples/btscanner/device.ui (renamed from examples/bluetooth/btscanner/device.ui) | 0 | ||||
-rw-r--r-- | tests/manual/examples/btscanner/main.cpp (renamed from examples/bluetooth/btscanner/main.cpp) | 12 | ||||
-rw-r--r-- | tests/manual/examples/btscanner/service.cpp (renamed from examples/bluetooth/btscanner/service.cpp) | 18 | ||||
-rw-r--r-- | tests/manual/examples/btscanner/service.h (renamed from examples/bluetooth/btscanner/service.h) | 24 | ||||
-rw-r--r-- | tests/manual/examples/btscanner/service.ui (renamed from examples/bluetooth/btscanner/service.ui) | 0 |
17 files changed, 61 insertions, 79 deletions
diff --git a/examples/bluetooth/CMakeLists.txt b/examples/bluetooth/CMakeLists.txt index 309e4bbe..de0996cc 100644 --- a/examples/bluetooth/CMakeLists.txt +++ b/examples/bluetooth/CMakeLists.txt @@ -4,7 +4,6 @@ qt_internal_add_example(heartrate-server) if(TARGET Qt::Widgets) qt_internal_add_example(btchat) - qt_internal_add_example(btscanner) endif() if(TARGET Qt::Quick) qt_internal_add_example(pingpong) diff --git a/examples/bluetooth/bluetooth.pro b/examples/bluetooth/bluetooth.pro index 8145ab39..73b78477 100644 --- a/examples/bluetooth/bluetooth.pro +++ b/examples/bluetooth/bluetooth.pro @@ -3,8 +3,7 @@ TEMPLATE = subdirs SUBDIRS += heartrate-server qtHaveModule(widgets) { - SUBDIRS += btchat \ - btscanner + SUBDIRS += btchat } qtHaveModule(quick): SUBDIRS += pingpong \ diff --git a/examples/bluetooth/btscanner/doc/images/btscanner-example.png b/examples/bluetooth/btscanner/doc/images/btscanner-example.png Binary files differdeleted file mode 100644 index b28be84a..00000000 --- a/examples/bluetooth/btscanner/doc/images/btscanner-example.png +++ /dev/null diff --git a/examples/bluetooth/btscanner/doc/src/btscanner.qdoc b/examples/bluetooth/btscanner/doc/src/btscanner.qdoc deleted file mode 100644 index bae5cbde..00000000 --- a/examples/bluetooth/btscanner/doc/src/btscanner.qdoc +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only - -/*! - \example btscanner - \title Bluetooth Scanner Example - \brief An example showing how to locate Bluetooth devices. - - An example showing how to locate Bluetooth devices. - - \image btscanner-example.png - - \include examples-run.qdocinc - - \sa {Qt Bluetooth} - -*/ diff --git a/src/bluetooth/doc/src/bluetooth-index.qdoc b/src/bluetooth/doc/src/bluetooth-index.qdoc index 7f2a0315..385c383b 100644 --- a/src/bluetooth/doc/src/bluetooth-index.qdoc +++ b/src/bluetooth/doc/src/bluetooth-index.qdoc @@ -168,7 +168,6 @@ quick way to enable all QtBluetooth logging is to add the following line to the \li C++ \list \li \l {btchat}{Bluetooth Chat} - \li \l {btscanner}{Bluetooth Scanner} \endlist \endlist diff --git a/src/bluetooth/doc/src/examples.qdoc b/src/bluetooth/doc/src/examples.qdoc index be2792c8..4d9969f9 100644 --- a/src/bluetooth/doc/src/examples.qdoc +++ b/src/bluetooth/doc/src/examples.qdoc @@ -23,9 +23,6 @@ \row \li \l{btchat}{Bluetooth Chat} \li Simple chat server and client via Bluetooth. - \row - \li \l{btscanner}{Bluetooth Scanner} - \li Scan for Bluetooth devices. \endtable \section2 QML Examples diff --git a/tests/manual/CMakeLists.txt b/tests/manual/CMakeLists.txt index 2cce14f6..47fdbfb8 100644 --- a/tests/manual/CMakeLists.txt +++ b/tests/manual/CMakeLists.txt @@ -4,5 +4,6 @@ if(TARGET Qt::Bluetooth) add_subdirectory(qlowenergycontroller) add_subdirectory(qlowenergycontroller_peripheral) + add_subdirectory(examples/btscanner) endif() diff --git a/examples/bluetooth/btscanner/CMakeLists.txt b/tests/manual/examples/btscanner/CMakeLists.txt index 758ac485..61d44dfc 100644 --- a/examples/bluetooth/btscanner/CMakeLists.txt +++ b/tests/manual/examples/btscanner/CMakeLists.txt @@ -4,9 +4,6 @@ cmake_minimum_required(VERSION 3.16) project(btscanner 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/btscanner") find_package(Qt6 REQUIRED COMPONENTS Bluetooth Core Widgets) +qt_standard_project_setup() + qt_add_executable(btscanner device.cpp device.h device.ui main.cpp @@ -33,14 +32,14 @@ if (APPLE) ) else() # Using absolute path for shared plist files is a Ninja bug workaround - get_filename_component(SHARED_PLIST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../shared ABSOLUTE) + get_filename_component(SHARED_PLIST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../examples/bluetooth/shared ABSOLUTE) set_target_properties(btscanner PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${SHARED_PLIST_DIR}/Info.cmake.macos.plist" ) endif() endif() -target_link_libraries(btscanner PUBLIC +target_link_libraries(btscanner PRIVATE Qt::Bluetooth Qt::Core Qt::Widgets diff --git a/examples/bluetooth/btscanner/Info.plist b/tests/manual/examples/btscanner/Info.plist index 49fd2191..49fd2191 100644 --- a/examples/bluetooth/btscanner/Info.plist +++ b/tests/manual/examples/btscanner/Info.plist diff --git a/examples/bluetooth/btscanner/btscanner.pro b/tests/manual/examples/btscanner/btscanner.pro index ee781ff7..3ef49969 100644 --- a/examples/bluetooth/btscanner/btscanner.pro +++ b/tests/manual/examples/btscanner/btscanner.pro @@ -10,7 +10,7 @@ SOURCES = \ service.cpp ios: QMAKE_INFO_PLIST = Info.plist -macos: QMAKE_INFO_PLIST = ../shared/Info.qmake.macos.plist +macos: QMAKE_INFO_PLIST = ../../../../examples/bluetooth/shared/Info.qmake.macos.plist HEADERS = \ device.h \ diff --git a/examples/bluetooth/btscanner/device.cpp b/tests/manual/examples/btscanner/device.cpp index 62f67124..ecbde2f8 100644 --- a/examples/bluetooth/btscanner/device.cpp +++ b/tests/manual/examples/btscanner/device.cpp @@ -5,12 +5,11 @@ #include "service.h" #include "ui_device.h" -#include <qbluetoothaddress.h> -#include <qbluetoothdevicediscoveryagent.h> -#include <qbluetoothlocaldevice.h> +#include <QtBluetooth/qbluetoothaddress.h> +#include <QtBluetooth/qbluetoothdevicediscoveryagent.h> +#include <QtBluetooth/qbluetoothlocaldevice.h> -#include <QMenu> -#include <QDebug> +#include <QtWidgets/qmenu.h> static QColor colorForPairing(QBluetoothLocalDevice::Pairing pairing) { @@ -21,9 +20,12 @@ static QColor colorForPairing(QBluetoothLocalDevice::Pairing pairing) DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent) : QDialog(parent), - localDevice(new QBluetoothLocalDevice), - ui(new Ui_DeviceDiscovery) + localDevice(new QBluetoothLocalDevice(this)), + ui(new Ui::DeviceDiscovery) { +#ifdef Q_OS_ANDROID + this->setWindowState(Qt::WindowMaximized); +#endif ui->setupUi(this); ui->stopScan->setVisible(false); @@ -31,9 +33,9 @@ DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent) : // to be used. Example code: // // QBluetoothAddress address("XX:XX:XX:XX:XX:XX"); - // discoveryAgent = new QBluetoothDeviceDiscoveryAgent(address); + // discoveryAgent = new QBluetoothDeviceDiscoveryAgent(address, this); - discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); + discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); connect(ui->scan, &QAbstractButton::clicked, this, &DeviceDiscoveryDialog::startScan); connect(ui->stopScan, &QAbstractButton::clicked, this, &DeviceDiscoveryDialog::stopScan); @@ -60,7 +62,7 @@ DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent) : DeviceDiscoveryDialog::~DeviceDiscoveryDialog() { - delete discoveryAgent; + delete ui; } void DeviceDiscoveryDialog::addDevice(const QBluetoothDeviceInfo &info) diff --git a/examples/bluetooth/btscanner/device.h b/tests/manual/examples/btscanner/device.h index f1161696..389062bb 100644 --- a/examples/bluetooth/btscanner/device.h +++ b/tests/manual/examples/btscanner/device.h @@ -4,17 +4,20 @@ #ifndef DEVICE_H #define DEVICE_H -#include <qbluetoothlocaldevice.h> +#include <QtBluetooth/qbluetoothlocaldevice.h> -#include <QDialog> +#include <QtWidgets/qdialog.h> -QT_FORWARD_DECLARE_CLASS(QBluetoothDeviceDiscoveryAgent) -QT_FORWARD_DECLARE_CLASS(QBluetoothDeviceInfo) -QT_FORWARD_DECLARE_CLASS(QListWidgetItem) +QT_BEGIN_NAMESPACE +class QBluetoothAddress; +class QBluetoothDeviceDiscoveryAgent; +class QBluetoothDeviceInfo; +class QListWidgetItem; -QT_FORWARD_DECLARE_CLASS(Ui_DeviceDiscovery) - -QT_USE_NAMESPACE +namespace Ui { + class DeviceDiscovery; +} +QT_END_NAMESPACE class DeviceDiscoveryDialog : public QDialog { @@ -25,22 +28,22 @@ public: ~DeviceDiscoveryDialog(); public slots: - void addDevice(const QBluetoothDeviceInfo&); + void addDevice(const QBluetoothDeviceInfo &info); void on_power_clicked(bool clicked); void on_discoverable_clicked(bool clicked); void displayPairingMenu(const QPoint &pos); - void pairingDone(const QBluetoothAddress&, QBluetoothLocalDevice::Pairing); + void pairingDone(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing); private slots: void startScan(); void stopScan(); void scanFinished(); void itemActivated(QListWidgetItem *item); - void hostModeStateChanged(QBluetoothLocalDevice::HostMode); + void hostModeStateChanged(QBluetoothLocalDevice::HostMode mode); private: QBluetoothDeviceDiscoveryAgent *discoveryAgent; QBluetoothLocalDevice *localDevice; - Ui_DeviceDiscovery *ui; + Ui::DeviceDiscovery *ui; }; #endif diff --git a/examples/bluetooth/btscanner/device.ui b/tests/manual/examples/btscanner/device.ui index fd86a358..fd86a358 100644 --- a/examples/bluetooth/btscanner/device.ui +++ b/tests/manual/examples/btscanner/device.ui diff --git a/examples/bluetooth/btscanner/main.cpp b/tests/manual/examples/btscanner/main.cpp index f6d4bed6..330c6888 100644 --- a/examples/bluetooth/btscanner/main.cpp +++ b/tests/manual/examples/btscanner/main.cpp @@ -3,16 +3,16 @@ #include "device.h" -#include <QApplication> -#include <QtCore/QLoggingCategory> +#include <QtCore/qloggingcategory.h> +#include <QtWidgets/qapplication.h> int main(int argc, char *argv[]) { // QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); QApplication app(argc, argv); - DeviceDiscoveryDialog d; - d.exec(); - return 0; -} + d.show(); + + return app.exec(); +} diff --git a/examples/bluetooth/btscanner/service.cpp b/tests/manual/examples/btscanner/service.cpp index 324e0208..aa6fe43c 100644 --- a/examples/bluetooth/btscanner/service.cpp +++ b/tests/manual/examples/btscanner/service.cpp @@ -5,16 +5,16 @@ #include "service.h" #include "ui_service.h" -#include <qbluetoothaddress.h> -#include <qbluetoothservicediscoveryagent.h> -#include <qbluetoothserviceinfo.h> -#include <qbluetoothlocaldevice.h> -#include <qbluetoothuuid.h> +#include <QtBluetooth/qbluetoothaddress.h> +#include <QtBluetooth/qbluetoothlocaldevice.h> +#include <QtBluetooth/qbluetoothservicediscoveryagent.h> +#include <QtBluetooth/qbluetoothserviceinfo.h> +#include <QtBluetooth/qbluetoothuuid.h> ServiceDiscoveryDialog::ServiceDiscoveryDialog(const QString &name, const QBluetoothAddress &address, QWidget *parent) -: QDialog(parent), ui(new Ui_ServiceDiscovery) + : QDialog(parent), ui(new Ui::ServiceDiscovery) { ui->setupUi(this); @@ -27,10 +27,9 @@ ServiceDiscoveryDialog::ServiceDiscoveryDialog(const QString &name, // Example code: // // QBluetoothAddress adapterAddress("XX:XX:XX:XX:XX:XX"); - // discoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress); - - discoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress); + // discoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress, this); + discoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress, this); discoveryAgent->setRemoteAddress(address); setWindowTitle(name); @@ -45,7 +44,6 @@ ServiceDiscoveryDialog::ServiceDiscoveryDialog(const QString &name, ServiceDiscoveryDialog::~ServiceDiscoveryDialog() { - delete discoveryAgent; delete ui; } diff --git a/examples/bluetooth/btscanner/service.h b/tests/manual/examples/btscanner/service.h index de7e802d..1c84f38c 100644 --- a/examples/bluetooth/btscanner/service.h +++ b/tests/manual/examples/btscanner/service.h @@ -4,31 +4,33 @@ #ifndef SERVICE_H #define SERVICE_H +#include <QtWidgets/qdialog.h> -#include <QDialog> +QT_BEGIN_NAMESPACE +class QBluetoothAddress; +class QBluetoothServiceDiscoveryAgent; +class QBluetoothServiceInfo; -QT_FORWARD_DECLARE_CLASS(QBluetoothAddress) -QT_FORWARD_DECLARE_CLASS(QBluetoothServiceInfo) -QT_FORWARD_DECLARE_CLASS(QBluetoothServiceDiscoveryAgent) - -QT_FORWARD_DECLARE_CLASS(Ui_ServiceDiscovery) - -QT_USE_NAMESPACE +namespace Ui { + class ServiceDiscovery; +} +QT_END_NAMESPACE class ServiceDiscoveryDialog : public QDialog { Q_OBJECT public: - ServiceDiscoveryDialog(const QString &name, const QBluetoothAddress &address, QWidget *parent = nullptr); + ServiceDiscoveryDialog(const QString &name, const QBluetoothAddress &address, + QWidget *parent = nullptr); ~ServiceDiscoveryDialog(); public slots: - void addService(const QBluetoothServiceInfo&); + void addService(const QBluetoothServiceInfo &info); private: QBluetoothServiceDiscoveryAgent *discoveryAgent; - Ui_ServiceDiscovery *ui; + Ui::ServiceDiscovery *ui; }; #endif diff --git a/examples/bluetooth/btscanner/service.ui b/tests/manual/examples/btscanner/service.ui index 4ca12ee0..4ca12ee0 100644 --- a/examples/bluetooth/btscanner/service.ui +++ b/tests/manual/examples/btscanner/service.ui |