Qt Resource System search path



  • The Qt Resource System uses a search path if a resource is referenced with : instead of :/ . What is the proper way of changing the search path?

    The documentation refers to QDir::addResourceSearchPath() which is obsolete and its docs refer to QDir::addSearchPath(). However what would be the prefix for affecting the resource system which is unprefixed?



  • [quote author="Bradley" date="1285006678"]However what would be the prefix for affecting the resource system which is unprefixed?
    [/quote]

    It seems to me that the prefix remains empty, i.e. unprefixed resources stay unprefixed.



  • [quote author="Bradley" date="1285006678"]The Qt Resource System uses a search path if a resource is referenced with : instead of :/ . What is the proper way of changing the search path?[/quote]

    I treat setting a search path and setting resource prefix in different ways.

    As for me, setting searching path gives you flexibility in terms of accessing your resources in your source code. On the other hand it may bring you to ambiguity, which should be avoided. At least I would sacrifice flexibility in order to eliminate ambiguity. I.e. accessing an icon like

    @QIcon(":myicon.png");@

    will end up in searching for this icon in all available resource searching paths. Here comes the question: What if you have 2 different myicon.png files in two different paths?

    Setting resource prefix is more like separating resources.

    @<RCC>
    <qresource prefix="/module1">
    <file>myicon.png</file>
    </qresource>
    </RCC>@



  • Using QResource::addSearchPath() or QDir::addResourceSearchPath() allows controlling the search for QIcon(":myicon.png"). For example, if I want the icon to be from one location by default, but from an overriding location if the search path is changed, I can use the search path to affect what icon is found.

    However, both those methods are marked as obsolete. My question is: How do I control the search for ":myicon.png" using non-obsolete methods?

    Using an empty prefix to add search paths doesn't work.

    @
    #include <QtCore>

    int main(int argc, char *argv[])
    {
    qDebug() << "QDir::searchPaths(QString::null):" << QDir::searchPaths(QString::null);
    qDebug() << "QDir::searchPaths(""):" << QDir::searchPaths("");
    qDebug() << "QResource::searchPaths():" << QResource::searchPaths();

    qDebug();
    qDebug() << "Call QResource::addSearchPath(\"/test/resource/path\");";
    qDebug() << "Call QDir::addResourceSearchPath(\"/test/qdir/resource/path\");";
    qDebug() << "Call QDir::addSearchPath(QString::null, \"/test/qdir/null/path\");";
    qDebug() << "Call QDir::addSearchPath(\"\", \"/test/qdir/empty/path\");";
    qDebug();
    QResource::addSearchPath("/test/resource/path");   // Works but obsolete
    QDir::addResourceSearchPath("/test/qdir/resource/path");  // Works but obsolete
    QDir::addSearchPath(QString::null, "/test/qdir/null/path");
    QDir::addSearchPath("", "/test/qdir/empty/path");
    
    qDebug() << "QDir::searchPaths(QString::null):" << QDir::searchPaths(QString::null);
    qDebug() << "QDir::searchPaths(\"\"):" << QDir::searchPaths("");
    qDebug() << "QResource::searchPaths():" << QResource::searchPaths();
    

    }
    @



  • [quote author="Bradley" date="1287642042"]For example, if I want the icon to be from one location by default, but from an overriding location if the search path is changed, I can use the search path to affect what icon is found.
    ...
    How do I control the search for &quot;:myicon.png&quot; using non-obsolete methods?
    [/quote]

    Is it necessary for you to make resource system responsible for that? You can write your own logic for getting icons, i.e.

    @void MyWidget::setIcon()
    {
    if (condition1) {
    set(QIcon(":/default/icon.png"));
    } else {
    set(QIcon(":/special/icon.png"));
    }
    }@



  • It is not necessary, but it would be convenient. Imagine that your "condition1" is an application level setting which would be used in many widgets. It is cleaner to not have the logic for the choice of icons in every widget.

    More specifically, the application supports two custom color palettes, light and dark. Some icons are hard to see in the dark palette and vice versa. Thus the need for two icons. When setting the palette, it seems appropriate to also set a search path for which icons to use. However, I would prefer not to depend upon obsolete methods. What is the new way for the same capabilities?



  • In case of paletes, I would think their icons are located in separate directories, i.e.
    @paletes
    |_dark
    | |_icon.png
    |_light
    |_icon.png
    @
    Honestly, I would do something like this, because this way I have more control of what's going on rather than letting resource system do this.

    @enum Palette {
    PALETTE_DEFAULT = 0,
    PALETTE_DARK,
    PALETTE_LIGHT
    };

    void switchPalette(Palette iPalette)
    {
    QString prefix;
    switch (iPalette) {
    case PALETTE_DARK:
    prefix = "dark";
    break;
    case PALETTE_LIGHT:
    case PALETTE_DEFAULT:
    prefix = "light";
    break;
    }

    // use QIcon(QString(":/%1/icon.png").arg(prefix));
    

    }@

    I admit this solution is not "beautiful" enough (as we say). But I would do this way.


Log in to reply
 

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