Jump to content

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

From Qt Wiki
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 56: Line 56:
</syntaxhighlight>
</syntaxhighlight>


* [[Qt_for_HarmonyOS/qtohosextras_doc/Want_FDs_Receiver_Example|abilitycontext/wantfdsreceiver/wantfdsreceiver.pro]]
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: