当前位置: 首页> 房产> 家装 > seo运营做什么_网架有限公司_seo链接优化建议_搜索优化指的是什么

seo运营做什么_网架有限公司_seo链接优化建议_搜索优化指的是什么

时间:2025/7/9 7:35:00来源:https://blog.csdn.net/qq_44849154/article/details/144527296 浏览次数:0次
seo运营做什么_网架有限公司_seo链接优化建议_搜索优化指的是什么

在Qt开发中,你可以通过QBluetoothDeviceDiscoveryAgent来搜索蓝牙设备,并将搜索到的设备显示在一个列表中。每个设备后面可以添加一个“连接”按钮,用户点击按钮后可以连接到对应的蓝牙设备。

以下是一个完整的示例,展示如何实现这一功能:


1. 项目结构

  • 创建一个Qt Widgets应用程序。
  • 使用QListWidgetQTableWidget来显示蓝牙设备列表。
  • 为每个设备添加一个“连接”按钮。

2. 代码实现

2.1 在.pro文件中添加蓝牙模块
QT += bluetooth widgets
2.2 主窗口实现
#include <QWidget>
#include <QVBoxLayout>
#include <QListWidget>
#include <QPushButton>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
#include <QBluetoothSocket>
#include <QDebug>class BluetoothDeviceItem : public QWidget
{Q_OBJECTpublic:BluetoothDeviceItem(const QBluetoothDeviceInfo &deviceInfo, QWidget *parent = nullptr): QWidget(parent), m_deviceInfo(deviceInfo){QHBoxLayout *layout = new QHBoxLayout(this);// 显示设备名称和地址QString deviceText = QString("%1 (%2)").arg(deviceInfo.name()).arg(deviceInfo.address().toString());QLabel *label = new QLabel(deviceText, this);layout->addWidget(label);// 添加连接按钮QPushButton *connectButton = new QPushButton("连接", this);connect(connectButton, &QPushButton::clicked, this, &BluetoothDeviceItem::onConnectClicked);layout->addWidget(connectButton);setLayout(layout);}private slots:void onConnectClicked(){qDebug() << "Connecting to:" << m_deviceInfo.name() << m_deviceInfo.address().toString();// 创建蓝牙套接字并连接到设备QBluetoothSocket *socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this);connect(socket, &QBluetoothSocket::connected, this, [=]() {qDebug() << "Connected to device:" << m_deviceInfo.name();});connect(socket, &QBluetoothSocket::disconnected, this, [=]() {qDebug() << "Disconnected from device:" << m_deviceInfo.name();});connect(socket, QOverload<QBluetoothSocket::SocketError>::of(&QBluetoothSocket::error), this, [=](QBluetoothSocket::SocketError error) {qDebug() << "Socket error:" << error;});// 连接到设备的RFCOMM服务socket->connectToService(m_deviceInfo.address(), QBluetoothUuid::SerialPort);}private:QBluetoothDeviceInfo m_deviceInfo;
};class BluetoothDiscoveryWidget : public QWidget
{Q_OBJECTpublic:BluetoothDiscoveryWidget(QWidget *parent = nullptr): QWidget(parent){QVBoxLayout *layout = new QVBoxLayout(this);// 设备列表m_deviceList = new QListWidget(this);layout->addWidget(m_deviceList);// 开始搜索按钮QPushButton *startScanButton = new QPushButton("开始搜索", this);connect(startScanButton, &QPushButton::clicked, this, &BluetoothDiscoveryWidget::startDiscovery);layout->addWidget(startScanButton);setLayout(layout);}private slots:void startDiscovery(){// 清空设备列表m_deviceList->clear();// 创建设备发现代理m_discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BluetoothDiscoveryWidget::deviceDiscovered);connect(m_discoveryAgent, QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error), this, &BluetoothDiscoveryWidget::deviceScanError);connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &BluetoothDiscoveryWidget::deviceScanFinished);// 开始搜索m_discoveryAgent->start();}void deviceDiscovered(const QBluetoothDeviceInfo &deviceInfo){// 为每个设备创建一个自定义的设备项BluetoothDeviceItem *itemWidget = new BluetoothDeviceItem(deviceInfo, this);QListWidgetItem *listItem = new QListWidgetItem(m_deviceList);m_deviceList->setItemWidget(listItem, itemWidget);listItem->setSizeHint(itemWidget->sizeHint());}void deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error){qDebug() << "Scan error:" << error;}void deviceScanFinished(){qDebug() << "Scan finished";}private:QBluetoothDeviceDiscoveryAgent *m_discoveryAgent = nullptr;QListWidget *m_deviceList = nullptr;
};int main(int argc, char *argv[])
{QApplication app(argc, argv);BluetoothDiscoveryWidget widget;widget.show();return app.exec();
}#include "main.moc"

3. 代码说明

3.1 BluetoothDeviceItem
  • 这是一个自定义的QWidget,用于显示每个蓝牙设备的名称和地址。
  • 每个设备后面都有一个“连接”按钮,点击按钮后会尝试连接到该设备。
3.2 BluetoothDiscoveryWidget
  • 这是主窗口,包含一个设备列表和一个“开始搜索”按钮。
  • 点击“开始搜索”按钮后,会调用QBluetoothDeviceDiscoveryAgent来搜索附近的蓝牙设备。
  • 搜索到的设备会显示在列表中,每个设备后面都有一个“连接”按钮。
3.3 连接逻辑
  • 当用户点击“连接”按钮时,会创建一个QBluetoothSocket,并尝试连接到设备的RFCOMM服务。
  • 连接成功或失败时会输出相应的日志。

4. 运行效果

  1. 运行程序后,点击“开始搜索”按钮,程序会搜索附近的蓝牙设备。
  2. 搜索完成后,设备列表中会显示设备的名称和地址,每个设备后面都有一个“连接”按钮。
  3. 点击“连接”按钮,程序会尝试连接到对应的蓝牙设备。

5. 注意事项

  • 权限:在Android或iOS上运行时,确保应用程序具有蓝牙权限。
  • 平台差异:在某些平台上(如Linux),可能需要额外的配置才能使用蓝牙功能。
  • RFCOMM服务:连接时使用的是QBluetoothUuid::SerialPort,这是标准的RFCOMM服务UUID。如果设备使用其他服务,需要根据实际情况修改UUID。

通过以上代码,你可以轻松实现一个蓝牙设备搜索和连接的界面。

关键字:seo运营做什么_网架有限公司_seo链接优化建议_搜索优化指的是什么

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: