Jump to content

Qt for HarmonyOS/qtohosextras doc/Want FDs Receiver Example

From Qt Wiki
Revision as of 03:00, 17 September 2026 by Shawn Luo (talk | contribs) (Publish example source listings and restore Files lists (QTFOROH-2307))

< Qt for HarmonyOS / qtohosextras doc

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: