Jump to content

Qt for HarmonyOS/qtohosextras doc/Serial Port Permissions Example: Difference between revisions

From Qt Wiki
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 68: Line 68:
'''Note: '''The lifetime of the permission is strictly tied to the delivered context object: the caller must keep the [https://doc.qt.io/qt-5/qsharedpointer.html QSharedPointer] alive for as long as access is required, and releasing the last reference revokes the permission.
'''Note: '''The lifetime of the permission is strictly tied to the delivered context object: the caller must keep the [https://doc.qt.io/qt-5/qsharedpointer.html QSharedPointer] alive for as long as access is required, and releasing the last reference revokes the permission.


* [[Qt_for_HarmonyOS/qtohosextras_doc/Serial_Port_Permissions_Example|serialportpermissions/serialportpermissions.pro]]
Files:
 
* [[Qt_for_HarmonyOS/qtohosextras_doc/Serial_Port_Permissions_Example_-_main.cpp|serialportpermissions/main.cpp]]
* [[Qt_for_HarmonyOS/qtohosextras_doc/Serial_Port_Permissions_Example_-_serialportpermissions.pro|serialportpermissions/serialportpermissions.pro]]


[[Category:Qt for HarmonyOS]]
[[Category:Qt for HarmonyOS]]

Latest revision as of 07:53, 17 September 2026

Serial Port Permissions Example

Demonstrates requesting and checking OHOS serial port access rights.


The window lists the serial ports found with QSerialPortInfo::availablePorts(). For each port, one button requests access with QOhosAppContext::requestSerialPortAccessRightIfNeeded() and another checks it synchronously with QOhosAppContext::hasSerialPortAccessRight(). The asynchronous result arrives through the QOhosAppContext::serialPortAccessRightResponseReceived() signal, which delivers a context object when access was granted. The example stores the context in a dedicated window whose closing releases it.

Requesting access for a port:

         QObject::connect(
             requestButton, &QPushButton::clicked, &mainWindow,
             [portName]() {
                 QtOhosExtras::QOhosAppContext::instance()->requestSerialPortAccessRightIfNeeded(portName);
             });

Checking the current access right synchronously:

         QObject::connect(
             checkButton, &QPushButton::clicked, &mainWindow,
             [portName]() {
                 if (QtOhosExtras::QOhosAppContext::instance()->hasSerialPortAccessRight(portName)) {
                     QMessageBox::information(
                         nullptr, QLatin1String("Serial port permission"),
                         QLatin1String("Permission GRANTED for: ") + portName);
                 } else {
                     QMessageBox::warning(
                         nullptr, QLatin1String("Serial port permission"),
                         QLatin1String("Permission DENIED for: ") + portName);
                 }
             });

Handling the asynchronous response and keeping the delivered context alive:

         QObject::connect(
             QtOhosExtras::QOhosAppContext::instance(),
             &QtOhosExtras::QOhosAppContext::serialPortAccessRightResponseReceived,
             &mainWindow,
             [portName](const QString &responsePortName, QSharedPointer<QObject> responseContext) {
                 if (responsePortName != portName) {
                     return;
                 }

                 if (responseContext != nullptr) {
                     auto *window = new PermissionWindow(portName, responseContext);
                     window->setAttribute(Qt::WA_DeleteOnClose);
                     window->resize(400, 200);
                     window->show();
                 } else {
                     QMessageBox::warning(
                         nullptr, QLatin1String("Serial port permission"),
                         QLatin1String("Permission DENIED for: ") + portName);
                 }
             });

Note: The lifetime of the permission is strictly tied to the delivered context object: the caller must keep the QSharedPointer alive for as long as access is required, and releasing the last reference revokes the permission.

Files: