Jump to content

Qt for HarmonyOS/qtohosextras doc/Want FDs Receiver Example: Difference between revisions

From Qt Wiki
Fix example page conversion (QTFOROH-2307)
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
< [[Qt_for_HarmonyOS/qtohosextras_doc|Qt for HarmonyOS / qtohosextras doc]]
= Want FDs Receiver Example =
= Want FDs Receiver Example =


Line 9: Line 7:
The window logs every received want. For each entry in QOhosWant::fds the example prints the key, the descriptor value and the <code>dev</code> / <code>ino</code> numbers obtained with <code>fstat</code>, so they can be compared with the sender's log. Wants are handled both from QOhosAppContext::getAppLaunchWantInfo() at startup and from the QOhosAbilityContext::newWantInfoReceived() signal at runtime.
The window logs every received want. For each entry in QOhosWant::fds the example prints the key, the descriptor value and the <code>dev</code> / <code>ino</code> numbers obtained with <code>fstat</code>, so they can be compared with the sender's log. Wants are handled both from QOhosAppContext::getAppLaunchWantInfo() at startup and from the QOhosAbilityContext::newWantInfoReceived() signal at runtime.


* [[Qt_for_HarmonyOS/qtohosextras_doc/Want_FDs_Receiver_Example|abilitycontext/wantfdsreceiver/wantfdsreceiver.pro]]
Both want sources are wired to the same handler:
 
<syntaxhighlight lang="cpp">
 
    auto abilityContext = QtOhosExtras::QOhosAbilityContext::getDefaultInstance();
    QObject::connect(
        abilityContext.get(),
        &QtOhosExtras::QOhosAbilityContext::newWantInfoReceived,
        &window,
        [&window](const QSharedPointer<QtOhosExtras::QOhosWantInfo> &wantInfo) {
            window.handleWant(wantInfo->want());
        });
 
    auto launchWantInfo = QtOhosExtras::QOhosAppContext::getAppLaunchWantInfo();
    if (!launchWantInfo.isNull()) {
        window.handleWant(launchWantInfo->want());
    }
 
</syntaxhighlight>
 
The received descriptors are read from QOhosWant::fds and inspected:
 
<syntaxhighlight lang="cpp">
 
    for (auto it = want.fds.cbegin(); it != want.fds.cend(); ++it) {
        const QString fdKey = it.key();
        const int fd = it.value();
 
        struct stat statInfo;
        const int fstatResult = ::fstat(fd, &statInfo);
        const int fstatErrno = errno;
        if (fstatResult == 0) {
            appendLog(
                QStringLiteral("fd_info: key=%1 fd=%2 dev=%3 ino=%4")
                    .arg(fdKey)
                    .arg(fd)
                    .arg(static_cast<qulonglong>(statInfo.st_dev))
                    .arg(static_cast<qulonglong>(statInfo.st_ino)));
        } else {
            appendLog(
                QStringLiteral("fd_stat: key=%1 result=error errno=%2 message=%3")
                    .arg(fdKey)
                    .arg(fstatErrno)
                    .arg(QString::fromLocal8Bit(strerror(fstatErrno))));
        }
    }
 
</syntaxhighlight>
 
Files:
 
* [[Qt_for_HarmonyOS/qtohosextras_doc/Want_FDs_Receiver_Example_-_main.cpp|abilitycontext/wantfdsreceiver/main.cpp]]
* [[Qt_for_HarmonyOS/qtohosextras_doc/Want_FDs_Receiver_Example_-_wantfdsreceiver.pro|abilitycontext/wantfdsreceiver/wantfdsreceiver.pro]]


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

Latest revision as of 07:55, 17 September 2026

Want FDs Receiver Example

Demonstrates receiving file descriptors passed to the application through a want.


The window logs every received want. For each entry in QOhosWant::fds the example prints the key, the descriptor value and the dev / ino numbers obtained with fstat, so they can be compared with the sender's log. Wants are handled both from QOhosAppContext::getAppLaunchWantInfo() at startup and from the QOhosAbilityContext::newWantInfoReceived() signal at runtime.

Both want sources are wired to the same handler:

     auto abilityContext = QtOhosExtras::QOhosAbilityContext::getDefaultInstance();
     QObject::connect(
         abilityContext.get(),
         &QtOhosExtras::QOhosAbilityContext::newWantInfoReceived,
         &window,
         [&window](const QSharedPointer<QtOhosExtras::QOhosWantInfo> &wantInfo) {
             window.handleWant(wantInfo->want());
         });

     auto launchWantInfo = QtOhosExtras::QOhosAppContext::getAppLaunchWantInfo();
     if (!launchWantInfo.isNull()) {
         window.handleWant(launchWantInfo->want());
     }

The received descriptors are read from QOhosWant::fds and inspected:

     for (auto it = want.fds.cbegin(); it != want.fds.cend(); ++it) {
         const QString fdKey = it.key();
         const int fd = it.value();

         struct stat statInfo;
         const int fstatResult = ::fstat(fd, &statInfo);
         const int fstatErrno = errno;
         if (fstatResult == 0) {
             appendLog(
                 QStringLiteral("fd_info: key=%1 fd=%2 dev=%3 ino=%4")
                     .arg(fdKey)
                     .arg(fd)
                     .arg(static_cast<qulonglong>(statInfo.st_dev))
                     .arg(static_cast<qulonglong>(statInfo.st_ino)));
         } else {
             appendLog(
                 QStringLiteral("fd_stat: key=%1 result=error errno=%2 message=%3")
                     .arg(fdKey)
                     .arg(fstatErrno)
                     .arg(QString::fromLocal8Bit(strerror(fstatErrno))));
         }
     }

Files: