QLabel overlay png over gif - qt

I have a QLabel and I can set a gif to animate as qmovie or a png to show as qpixmap.
What I want to do is to animate the gif and put a png over it.
Both images have transparent background. The gif is animated "loading circle", png is a "check" icon.
The code below is displaying the last one only.
#define MOOD_RESULT ":/mood/mood_result.gif"
#define MOOD_SUCCESS ":/mood/mood_success.png"
ui->moodicon->setPixmap(QPixmap(MOOD_SUCCESS));
QMovie *movie = new QMovie(MOOD_RESULT);
ui->moodicon->setMovie(movie);
movie->start();
I hope a Qt guru can help me.
Thanks in advance

Here are workable ideas:
Combine the images manually by playing the movie one-by-one into a pixmap, then overpainting the static image, and setting the result on the label.
Derive from QLabel, overload paintEvent, and overpaint the static image after calling QLabel::paintEvent.
Overlay another label on top of the one with the movie.

Related

Scale Image in QWidget for printing

I use the following code to print a complex widget with text and static images:
printer = QPrinter()
printer.setResolution(PRINTER_DPI)
painter = QPainter(printer)
painter.scale(SCALE, SCALE)
my_widget.render(painter)
painter.end()
Text looks nice after applying scale() but the pixmaps on the widget still look pixelated when printed. I tried using a higher resolution source image and setting scaledContents on QLabel but this didn't seem to help.
Any ideas how I can increase the image resolution for printing?
Edit: I'm still interested in an answer but in the meantime I worked around the issue by using QSvgWidget with an svg source image instead of QLabel.

Change color of transparent image in Qt

I have a transparent image (QImage) overlayed over a video in Qt. I want to change the color of the transparent image only on clicks of button. Can some one tell me how to do this?
Thank You.
This can be done in many ways. I suggest to use QPainter to create new image. If you set SourceIn composition mode, starting image's alpha channel will be applied to any drawing that you will do. You just need to fill image with desired color.
QPixmap source_image; // should be preserved in a class member variable
QRgb base_color; // desired image color
QPixmap new_image = source_image;
QPainter painter(&new_image);
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
painter.fillRect(new_image.rect(), base_color);
painter.end();
ui->label->setPixmap(new_image); // showing result
Note that I use QPixmap instead of QImage because QPixmaps are more efficient to display (and possibly paint). If you for some reason still want to use QImage, this code will work with QImage without any changes (excluding the last line of course).
Source image:
Result:

Qt Window with transparent background image

I placed background image like this:
setWindowFlags(Qt::FramelessWindowHint);
QPixmap slika("some_image.png");
QPalette paleta;
paleta.setBrush(this->backgroundRole(), QBrush(slika));
this->setPalette(paleta);
If I make this picture transparent, when application loads, it will only blink and disappear. But if I make this image with no transparency, then everything is ok. Why Qt refuses to use transparent image?
I don't know what is your use case for this, but you can also try using setStyleSheet method to make background transparent.
setStyleSheet("background:transparent;");
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::FramelessWindowHint);
Hope this helps.

Qt 4.7.4 - Make transparent pixels clickable (expand hitbox) in QGraphicsItem pixmap

I am creating a circuit schematic editor using Qt Creator. I have a QGraphicsScene/QGraphicsView canvas that I would like to drop images of circuit components onto and move them around.
I am currently using a pixmap QGraphicsItem and adding it to the canvas and making it movable. This works great when you click directly on the symbol's lines, however the symbol does not move when the transparent areas in the image are clicked.
Is there a way to expand the hitbox/mouse area to make these transparent regions respond the same as the other regions do on the symbol? Below is how I am adding the image. (I need the image transparent so that other symbols are visible behind/infront of it)
QGraphicsItem* b = canvas.addPixmap(QPixmap(":/images/ground2.gif"));
b->setFlag(QGraphicsItem::ItemIsMovable);
b->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height()));
All help is much appreciated! Thanks!
Josh
You can call QGraphicsPixmapItem::setShapeMode( QGraphicsPixmapItem::BoundingRectShape ) to have the item treated a rectangle.

transparent QLabel with a pixmap

I have a QLabel, and I put an image on it using setpixmap(). That image has alpha channel.
The QLabel is on a QWidget which has a border-image specified by an image (so that the image is rescaled to fill the QWidget).
On the transparent parts of the QLabel, the result is not the image specified on the QWidget, but a gray color characteristic of "no color" Widget.
My question is how do I make this in such a way that the transparent part of the QLabel shows the border-image of the QWidget?
I've tried canceling autofillbackground, changing the background color of the QLabel to white transparent, but none helped.
You need to set the Widget Attributes and Window Flags appropriately:
Using QWidget::setAttribute() and Qt::WidgetAttribute...
Qt::WA_TranslucentBackground needs to be set to true.
Along with learning the Window Flags Example should help a lot.
If you are just looking for a single widget that paints a frameless image...
Here is a perfect example:
QSplashScreen Replacement

Resources