Include program in resources



  • I'm using Qt 5.3 on OS X. I have a program that depends on the android tool adb, so I'm trying to include it as a resource.

    In my .pro file I have

    @RESOURCES = app.qrc@

    In app.qrc I have:
    @
    <!DOCTYPE RCC><RCC version="1.0">
    <qresource>
    <file>assets/adb</file>
    </qresource>
    @

    In my app:

    @

    QResource::registerResource("app.rcc");

    QProcess *kill_adb=new QProcess;
    kill_adb->start("/assets/adb kill-server");
    kill_adb->waitForFinished(-1);
    int exitcode = kill_adb->exitCode();

    QString s = QString::number(exitcode);
    QMessageBox::critical(
    this,
    tr("exit code:"),
    s);
    delete kill_adb;
    @

    Calls to /assets/adb fail. If I use a path to a local adb the code works. How can I include adb with my program?


  • Moderators

    You don't need to register resources that are defined in qmake's RESOURCES variable: it is done automatically for you.

    The path to ADB is wrong; it should be:
    @
    ":/assets/adb"
    // or
    "qrc:///assets/adb"
    @



  • I'm not an expert on OS X, but normally you cannot run programs directly from the resources. For the operating system, the file does not exist, after all. Instead, you'll need to "extract" the resource to a temporary file, at runtime, before you can launch it as a program. At least that's what I currently do. I don't think QProcess handles this transparently. Does it?


  • Moderators

    [quote author="MuldeR" date="1401023977"]I'm not an expert on OS X, but normally you cannot run programs directly from the resources. For the operating system, the file does not exist, after all. Instead, you'll need to "extract" the resource to a temporary file, at runtime, before you can launch it as a program. At least that's what I currently do. I don't think QProcess handles this transparently. Does it?[/quote]

    You are most probably right. I forgot to mention that in my previous post. Let's see what the OP finds out.



  • Thanks for the feedback. How do you extract a resource?



  • You can do something like:
    @bool bSuccess = false;
    const QString binPath = QString("%1/adb_%2").arg(
    QDir::tempPath(),
    QString::number(qrand())
    );

    QResource myRes(":/assets/adb");
    if(myRes.isValid())
    {
    QFile outFile(binPath);
    if(outFile.open(QIODevice::WriteOnly)
    {
    if(outFile.write(myRes.data(), myRes.size()) == myRes.size())
    {
    bSuccess = true;
    qDebug("Success! :-)";
    }
    outFile.close();
    }
    }

    if(bSuccess)
    {
    QProcess *kill_adb=new QProcess;
    kill_adb->start(binPath);
    kill_adb->waitForFinished(-1);
    }@

    (Note: On Unix-like OS you might also need to setup the permissions)


  • Lifetime Qt Champion

    Hi,

    And also check if adb as any non-system dependencies. At least on OS X it doesn't seem so.



  • Thanks for the replies! Things are a little clearer now :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.