c++


Qt: Modify alpha channel transparency of a windowless QLabel


I have a very small Qt application that uses labels to display a jpeg image without first putting it in a window. (I got a lot of help from Display QImage with QtGui)
Now I would like to change the alpha channel of this jpeg to make the image partially transparent. I have tried the following without any luck
int main (int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel label (0, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
label.resize(1280,720);
label.setPixmap(QPixmap("test.jpg"));
label.setScaledContents(true);
// This line should set the alpha transparency to 50%
label.setStyleSheet("background-color: rgba(255,255,255,50);");
label.show();
return app.exec();
}
It seems like the Style Sheet isn't affecting the label at all. I have experimented with changing the other rgb values (all 0's for instance) and alternated between the background-color and the color, but the image is always the same.
Update:
Thanks to eyllanesc, the following now works for me:
int main (int argc, char *argv[])
{
QApplication app(argc, argv);
QPixmap input ("test.jpg");
QImage image(input.size(), QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
QPainter p(&image);
p.setOpacity(0.5);
p.drawPixmap(0,0,input);
p.end();
QPixmap output = QPixmap::fromImage(image);
QLabel label (0, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
label.setStyleSheet("background-color: rgba(255,255,255,50);");
label.resize(1280,720);
label.setPixmap(output);
label.setScaledContents(true);
label.show();
return app.exec();
}
The StyleSheet is working fine, the problem is that the QPixmap object is drawn on the background (not the background). If you want QPixmap to be transparent you can use one of two methods:
First Method:
QPixmap input("test.jpg");
QImage image(input.size(), QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
QPainter p(&image);
p.setOpacity(0.2);
p.drawPixmap(0, 0, input);
p.end();
QPixmap output = QPixmap::fromImage(image);
label.setPixmap(output);
Second Method:
QPixmap input("test.jpg");
QPixmap output(input.size());
output.fill(Qt::transparent);
QPainter p(&output);
p.setOpacity(0.2);
p.drawPixmap(0, 0, input);
p.end();
label.setPixmap(output);

Related Links

virtual ClistCtrl with checkboxes on displayed report list style
Error when modifying vector member of another class
Interrupt Service Routine can't be called directly
Lambda capture by default reference vs named
convert bitset<64> to string every 8 bit
argument of type “int” incompatible with parameter of type “int”
When is the correct time to call restoreState for QHeaderView?
How does it pass lvalue reference to std::forward with CRTP?
Using lower_bound function in loop is giving runtime error?
Problems with rand(), always taking the same random generated number [duplicate]
Reading Credentials file fails with error 32
The mechanism of shared_ptr
C++ specialized function template
Compiler errors with cmath on Mac OS X
Array of fstream in C++
Error: (E107) bind interface to port failed: interface already bound to port: port 'MC8051_ALU().AM.port_27' (sc_in)

Categories

HOME
docker
gnuplot
google-analytics
logistic-regression
post
raspberry-pi3
amazon-cloudsearch
interaction
fontforge
alexa-voice-service
video.js
jxmaps
twincat-ads
odbc
divide-and-conquer
derivative
flip-flop
raycasting
visualvm
packet-capture
xfce
google-cloud-vision
apache-samza
jsx
ractivejs
oracle-data-integrator
github-api
rubinius
ext4
chinese-locale
atom-feed
jquery-chosen
android-ffmpeg
fread
sequences
tablet
javapoet
dexguard
pymodbus3
moa
dwt
ctags
object-oriented-analysis
ikvm
docker-registry
slug
c#-to-vb.net
use-case
banking
sonarqube-4.5
motorola
jwrapper
mockery
dart-polymer
ilmerge
rc4-cipher
simplify
dfdl
rgraph
idiorm
httpful
ooyala
velocity.js
jlist
distinct-on
7digital
sin
pull-request
authentication-flows
ms-query
mpmediapickercontroller
pixman
microsoft.mshtml
enplug-sdk
jquery-dropkick
rot13
navigationbar
class-relationship
function-prototypes
sqr
facade
firstdata
updatemodel
dynamic-html
divshot
amber-smalltalk
ruby-on-rails-plugins
django-sessions
fatwire
clang-complete
iphone-4
nsformatter
kiln
expandoobject
equivalent
validationsummary
asp.net-mvc-2-validation
incremental-linking
urldecode
olpc
table-driven

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile