'Qt stylesheet to use an image from memory

Qt stylesheets allow customizing of icons, for example the drop-down icon in a combo box. But all examples and docs which I have yet seen require having the image stored in a url. Then you can write for example QComboBox::down-arrow { image: url(path-to-file.png); }.

My question is: isn't there any trick which would allow to work around the fact that the file must be stored somewhere and use for example a pixmap from the memory?

I am asking because recently I found a nice hack which allows using QPixmap data to be used when displaying images in widgets which otherwise accept richtext (HTML formatted). See this code:

QPixmap preview;
// ... generate the pixmap here
QByteArray data;
QBuffer buffer(&data);
preview.save(&buffer, "PNG");
QString img = QStringLiteral("<img src='data:image/png;base64, %1'/>").arg(QString::fromLatin1(data.toBase64()));
//... and now you can display the image anywhere Qt accepts HTML formatted text, 
// e.g. in QToolTip (which is my usecase).

This way I can use data from memory without saving it to file.

I am curious if there isn't any similar trick for images used in Qt style sheets.



Solution 1:[1]

There's nothing special, just put path to your image:

setStyleSheet("background-image: url(/path/to/file.png);");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1