Qt for HarmonyOS/qtohosextras doc/Want FDs Receiver Example: Difference between revisions
Fix example page conversion (QTFOROH-2307) |
Add code snippets to example pages (QTFOROH-2307) |
||
| Line 8: | Line 8: | ||
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. | ||
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> | |||
* [[Qt_for_HarmonyOS/qtohosextras_doc/Want_FDs_Receiver_Example|abilitycontext/wantfdsreceiver/wantfdsreceiver.pro]] | * [[Qt_for_HarmonyOS/qtohosextras_doc/Want_FDs_Receiver_Example|abilitycontext/wantfdsreceiver/wantfdsreceiver.pro]] | ||
[[Category:Qt for HarmonyOS]] | [[Category:Qt for HarmonyOS]] | ||
Revision as of 10:21, 16 September 2026
< 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))));
}
}