views:

32

answers:

2

I am using stylesheets in QT to skin several buttons with images from the QT resource system:

QFrame#DialogButtonTitle_SaveAsNew
{
  background-image: url(images:DialogButtonTitle_SaveAsNew.png);
}

This works great, but I would really like to write a warning to our logs if the image file could not be found (and the button is thus naked). Any way to catch such errors?

+1  A: 

It's possible that Qt will call one of their message functions when something like this happens (although I don't know for sure). If it does, you could install a message handler function and append some or all of the messages to your log file. There is some information about doing so in the documentation for qInstallMsgHandler.

Caleb Huitt - cjhuitt
I don't know... There is already a default handler in place that prints to the standard output. If no message appears on the console, Qt is probably not writing it to begin with.
andref
Yes, the message handler does not help in this particular situation.
Christopher Oezbek
A: 

I believe you can do it like this:

Haven't tested, but I think it should work. Code:

bool MyEngine::open(QIODevice::OpenMode mode)
{
    bool r = QFSFileEngine::open(mode);
    if (!r) {
        qWarning() << "Failed to open" << fileName();
    }
    return r;
}

QAbstractFileEngine *MyEngineHandler::create(const QString &fileName) const
{
     return fileName.startsWith("images:") ? new MyEngine(fileName) : 0;
}

Edit.

This will not work. The resource file system, “:”, is handled by a private file engine called QResourceFileEngine, not by QFSFileEngine.

andref
Thanks! Good solution. Will give it a spin!
Christopher Oezbek