You are not logged in.
Hi everyone!
I have a GdkPixbuf that contains an icon taken from a window using libwnck (to be more precise I'm using wnck_window_get_icon(WnckWindow*)).
As I'm making a QT project I would like to "convert" that GdkPixbuf into a QPixmap which will be shown into a QLabel, but not knowing too much about those libraries I'm kinda stuck.
I was thinking to use gdk_pixbuf_save_to_buffer to save the image into memory and then load it into a QPixmap, but I don't know how to proceed.
Can someone help me?
Last edited by Gianfrix (2011-09-20 17:23:59)
~ Gianfrix
There's no place like 127.0.0.1
Offline
Dang, I got it ![]()
I had to create a QImage using raw data coming from GdkPixbuf functions and then create a QPixmap from this QImage ![]()
The problem now is that colors are shown wrong. The best format that shows the icon is QImage::Format_ARGB32, but, for example, red turns to blue, and Chrome's icon is shown as blue, cyan and green instead of red, yellow and green. What format should I use?
~ Gianfrix
There's no place like 127.0.0.1
Offline
@Gianfrix: You'll of course need to use the same image format GdkPixbuf uses to store image data.
However, I'd just use a standard format to exchange data instead of messing around with the internal data. Use "gdk_pixbuf_save_to_callbackv()" to save image data to a "QBuffer" in PNG format, and then "QImage::load()" the data from the buffer.
gboolean save_to_qbuffer_callback(const gchar *buffer, gsize count,
GError **error, gpointer data) {
QBuffer *output_buffer = static_cast<QBuffer>(data);
output_buffer->setData(buffer, count);
}
QImage toQImage(GdkPixbuf *image) {
QBuffer temp_buffer;
QImage qimage;
gboolean success = gdk_pixbuf_save_to_callbackv(
pixbuf, save_to_qbuffer_callback, &output_buffer, "png",
NULL, NULL, NULL);
if (success) {
qimage.load(temp_buffer, "PNG");
}
return qimage;
}Last edited by lunar (2011-09-20 07:49:08)
Offline
Thank you for the reply ![]()
I tried your code and initially it gave me loads of errors, so I tried correcting it ending up with this:
gboolean saveToQbufferCallback(const gchar *buffer, gsize count,
GError **error, gpointer data) {
QBuffer *outputBuffer = static_cast<QBuffer*>(data);
outputBuffer->setData(buffer, count);
}
QImage toQImage(GdkPixbuf *pixbuf) {
QBuffer imageBuffer;
QImage qimage;
gboolean success = gdk_pixbuf_save_to_callbackv(
pixbuf, saveToQbufferCallback, &imageBuffer, "png",
NULL, NULL, NULL);
if (success) {
qimage.load(&imageBuffer, "PNG");
}
return qimage;
}After that I'm retrieving and then attaching the pixmap using this code into another function:
...
GdkPixbuf* windowIcon;
windowIcon = wnck_window_get_icon(handledWindow);
panel->getAppIconLabel()->setPixmap(QPixmap::fromImage(toQImage(windowIcon)));
...handledWindow is the window whence I want to pick the icon and getAppIconLabel() returns a QLabel object (which will contain the pixmap).
The problem is that there isn't shown any icon at all! Have I done something wrong modifiying the function?
Anyways, the code I was using before was this:
GdkPixbuf* windowIcon;
windowIcon = wnck_window_get_icon(handledWindow);
QImage image(gdk_pixbuf_get_pixels(windowIcon),
gdk_pixbuf_get_width(windowIcon),
gdk_pixbuf_get_height(windowIcon),
gdk_pixbuf_get_rowstride(windowIcon),
QImage::Format_ARGB32);
panel->getAppIconLabel()->setPixmap(QPixmap::fromImage(image));But the result was wrongfully this (see the colors?)
I read on QImage reference that there is a QImage::setColorTable(QVector<QRgb>) function that I think could be useful, but obiviously I don't know where to put my hands...
EDIT: In toQImage I tried qDebug() << qimage.isNull(); and it returns true ![]()
Last edited by Gianfrix (2011-09-20 13:36:29)
~ Gianfrix
There's no place like 127.0.0.1
Offline
I've to admit that my code doesn't work. I assumed that the callback is called only once with the whole image data, which is not the case. Thus my code corrupts the image data and so the image isn't loaded. Sorry for the hassle.
As excuse, I've implemented a working example. Just copy "save_to_byte_array()" and "toQImage()" into your project.
Offline
Not an hassle, it was a good exercise for me to place pointers in the right place ![]()
Anyways, thank you very much, that code works really great. You're a master ![]()
~ Gianfrix
There's no place like 127.0.0.1
Offline