QBluetoothLeUart/qbluetoothleuartdevicemodel.h

70 lines
1.6 KiB
C++

#ifndef QBLUETOOTHLEUARTDEVICEMODEL_H
#define QBLUETOOTHLEUARTDEVICEMODEL_H
#include <QAbstractListModel>
#include <QObject>
#include <qbluetoothleuartdevice.h>
/*!
* \brief The QBluetoothLeUartDeviceModel class can be used to display available devices in a QML ListView.
*
* Example implementation:
* \code{.qml}
* import de.itsblue.bluetoothleuart 1.0
*
* QBluetoothLeUartClient {
* id: ble
* Component.onCompleted: {
* ble.startScanningForDevices()
* }
* }
*
* ListView {
* model: ble.availableDevicesModel
*
* delegate: ItemDelegate {
* width: parent.width
*
* text: name
*
* onClicked: backend.bleController.connectToDevice(device)
* }
* }
* \endcode
*/
class QBluetoothLeUartDeviceModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged)
public:
friend class QBluetoothLeUartClient;
enum QBluetoothLeUartDeviceModelRole {
NameRole = Qt::DisplayRole,
IdRole,
AddressRole,
DeviceRole
};
Q_ENUM(QBluetoothLeUartDeviceModelRole)
int rowCount(const QModelIndex & = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QHash<int, QByteArray> roleNames() const;
protected:
QBluetoothLeUartDeviceModel(QList<QBluetoothLeUartDevice*> availableDevices, QObject *parent = nullptr);
void append(QBluetoothLeUartDevice* device);
void remove(int row);
void clear();
private:
QList<QBluetoothLeUartDevice*> availableDevices;
signals:
void rowCountChanged();
};
#endif // QBLUETOOTHLEUARTDEVICEMODEL_H