Jump to content

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

From Qt Wiki
Publish example source listings and restore Files lists (QTFOROH-2307)
No edit summary
 
Line 1: Line 1:
< [[Qt_for_HarmonyOS/qtohosextras_doc|Qt for HarmonyOS / qtohosextras doc]]
= Serial Port Permissions Example =
= Serial Port Permissions Example =



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: