Jump to content

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

From Qt Wiki
Fix example page conversion (QTFOROH-2307)
Add code snippets to example pages (QTFOROH-2307)
Line 8: Line 8:


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.
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:
<syntaxhighlight lang="cpp">
        QObject::connect(
            requestButton, &QPushButton::clicked, &mainWindow,
            [portName]() {
                QtOhosExtras::QOhosAppContext::instance()->requestSerialPortAccessRightIfNeeded(portName);
            });
</syntaxhighlight>
Checking the current access right synchronously:
<syntaxhighlight lang="cpp">
        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);
                }
            });
</syntaxhighlight>
Handling the asynchronous response and keeping the delivered context alive:
<syntaxhighlight lang="cpp">
        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);
                }
            });
</syntaxhighlight>


'''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.

Revision as of 10:17, 16 September 2026

< Qt for HarmonyOS / qtohosextras doc

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.