I've got a QPixmap and I would like to draw it on a QWidget. However, I would like to make it 50% transparent so that the background can be seen below. How can I do that?
You set the compositionmode in the QPainter and then either use a mask to define which bits of the iage are transparent or use QImage::Format_ARGB32_Premultiplied type for the image with the alpha channel set.
See the example http://doc.qt.io/archives/4.6/demos-composition.html
Related
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:
I want to achieve this :
Have a transparent surface of some width and height (A transparent widget)
Draw something on this surface such that only outlines of that figure are visible on the screen and nothing else (no background of the surface on which I am drawing should be there)
I made a widget and achieved to make it transparent like this :
window.setAttribute( Qt::WA_TranslucentBackground);
window.setWindowFlags (Qt::FramelessWindowHint);
window.setGeometry( 0,0,1200, 800 );
window.show();
Then I overrode the function paintEvent(QPaintEvent *); to make a ellipse using QPainter.
But still the surface is coming to be black color only. Can someone explain ?
Looks similar to what I did in a similar app, but I also did:
window.setAutoFillBackground(false);
Perhaps that's the missing piece for you?
Edit: Another idea: In your paintEvent, do you fill the widget rect() with a fully transparent color before you paint the ellipse?
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
Is it possible to assign an alpha-transparent mask to a QWidget? I know how to set a mask using setMask but it seems it only supports black&white masks. Is it possible to make it support a true alpha channel?
i.e. currently I have a PNG like this:
and a widget like this:
If I load my PNG in a QPixmap and set it as a mask, I get this (notice the edges):
However I would like to get this (smooth edges):
Any idea how to do that?
Note: I'm doing some more complex drawing on the widget, which must be restricted to the mask area, so I cannot simply set my PNG as the widget's background image.
I think your best route is in QPainter's composition modes.
For example:
QPixmap PixmapToBeMasked(Size);
PixmapToBeMasked.fill(QColor(255, 255, 255, 120));
QPixmap Mask = DoSomethingToGetAMask();
QPainter Painter(&PixmapToBeMasked);
Painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
Painter.drawPixmap(0, 0, Mask.width(), Mask.height(), Mask);
That will handle drawing your widget nicely. If you still need to mask mouse events you might need to do some extra work though.
I would like to change the stock cursor with a translucent one, a simple filled circle, of various sizes, depending on the level of zoom in the underlying widget (say, RGBA = 200, 200, 200, 128).
Is this at all possible with Qt? If not, is it a limitation in Qt or the underlying libs? Do you have suggestions as to how this could be accomplished by other means, e.g., hiding the cursor and overlaying a transparent pixmap at the cursor position (albeit slower)? TIA
QCursor can take a QPixmap which does support alpha channel. So I don't see why it can't be done.
I just figured this out for a project of my own. I did it with this code in the constructor of the relevant widget:
m_LPixmap = new QPixmap(32,32);
m_LPixmap->fill(Qt::transparent); // Otherwise you get a black background :(
QPainter painter(m_LPixmap);
QColor red(255,0,0,128);
painter.setPen(Qt::NoPen); // Otherwise you get an thin black border
painter.setBrush(red);
painter.drawEllipse(0,0,32,32);
m_Cursor = QCursor(*m_LPixmap);
setCursor(m_Cursor);