Qt Window with transparent background image - qt

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.

Related

Transparent QGraphicsWebview over QGLWidget leads to super imposed images

I have a transparent QGraphicsWebview inside a QGraphicsView with the following settings:
The QGraphicsView is the high level widget, and is shown in full screen mode
The graphics view uses a QGLWidget as its view port (to use opengl-es)
Alpha channel and double buffering are enabled in this QGLWidget
Transparency is achieved by graphicsView->setStyleSheet("background:transparent")
Following attributes are set for QGraphicsView and QGraphicsWebview
WA_TranslucentBackground = true
WA_NoSystemBackground = true
WA_OpaquePaintEvent = false
The QPalette::Base and QPalette::Window brushes of webview and webview->page() are set to Qt::transparent
At the beginning, the transparency works fine. But as the screen get updated (when I scroll), it looks like the new bitmap is blended on top of the existing one to get a superimposed picture. After about 5-6 screen updates, this blending causes the colors to accumulate and form an opaque rectangle (with a corrupted image). Following images show first, second and final stages of the problem.
How do I tell qt/opengl to stop blending and just draw the new image to the frame buffer?
I tried calling fillRect(boundRect(), Qt::transparent) from overridden Webview::paint and GraphicsView::paintEvent; but it didn't work except for making the updates slower.
I am new to Qt and OpenGl, so I might be missing some basic flags or settings.
I tried all the methods mentioned above. They did not work for me. I then debugged into qtbase code and found that setting the Opacity level make the top browser layer transparent.
this->setOpacity(0.1);
this pointer points to the QGraphicsWebView object.
With this method, the whole front browser contents including background will be transparent. To separate header, paragraph and background and to specify different transparent levels for them, I will have to dig into webkit code a little further to figure out the problem. But for now, setOpacity() did the trick and is good enough for what I am doing.
It turns out the problem is graphicsView->setStyleSheet("background:transparent");. Who would have guessed?!
Yeah, the line that I thought made transparency work was actually causing troubles with transparency. The application works fine without that line (or if you change it to background:none)
In short, steps to get a transparent QGraphicsWebview using QGLWidget:
Set the Base palettes of QGraphicsWebview, QWebpage and the outer QGraphicsView to Qt::transparent
scene->setBackgroundBrush(QBrush(Qt::transparent));
You should also make sure that html body background is set to transparent values:
html, body {
background-color: rgba(127, 127, 0, 0.5);
}

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

How to make Widgets background transparent in Qt

I am building an App in which I had given background to my mainWindow only and all other widgets are used without any background, but when I run the app they are not 100% transparent they are somewhat translucent, is there any way to make them 100 % transparent so that only foreground can appear with no background hint.
Could you post the code you are using to get transparancy at the moment? If you say that you get "something translucent" I think that you created a tool window which is not what you really want.
A really transparent main window could be achieved by removing the title bar (give the QWidget constructor Qt::FramelessWindowHint as second parameter - WindowFlags), draw everything in the widget that should be transparent in a uncommon color (like 255,0,255) and then cut it off.
A very primitive example of cutting off parts of a QWidget:
QBitmap b(100, 100);
b.fill(Qt::black);
setMask(b);
QBitmap must be black on that pixels that shall be visible and white on that are not. In this example just the 100,100 area starting at 0,0 will be visible, the rest of your window will be invisible.

Fullscreen borderless QDialog in Qt on Symbian

Is there a way to show QDialog in Qt on Symbian without a border?
I show the dialog by this way:
QDialog dialog;
dialog.setWindowState(Qt::WindowFullScreen);
dialog.exec();
But there's an awful border at the screen, that's what I want to hide.
I've tried to use Qt::Splashscreen, but then there's a qdialog with transparent background, that doesn't achieve my goal.
Qt::FramelessWindowHint doesn't change something also (borders shows again).
You can call the setWindowOpacity after setting the SplahScreen flag in order to make it completely opaque.
dialog->setWindowOpacity(1.0);

Resources