Archived:Opacity effect with the Qt graphics view framework
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on C++ for the Qt UI) is deprecated.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on C++ for the Qt UI) is deprecated.
This article shows how to use the opacity effect using the Qt QGraphicsOpacityEffect.
Article Metadata
Code Example
Source file: Media:GraphicsEffect-Opacity.zip
Compatibility
Platform(s): any
Article
Keywords: QGraphicsOpacityEffect
Created: gnuton
(24 Dec 2010)
Last edited: hamishwillee
(11 Oct 2012)
Introduction
Qt Graphics view framework offers a convenient way to apply graphics effects to Graphics Items.
Code
This is a minimal application which changes the opacity of a QGraphicsPixmap using the QGraphicsOpacityEffect.
The item opacity can be set with two methods:
- setOpacity(): it accepts values from 0.0 (image completely transparent) to 1.0.
- setOpacityBrush(): it supports gradients or masks
The following example shows how to use a mask to set the opacity of the given pixmap item.
#include <QtGui/QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsEffect>
#include <QBrush>
#include <QBitmap>
#include <QGraphicsPixmapItem>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setGraphicsSystem("raster");
QGraphicsView view;
view.setScene(new QGraphicsScene);
QGraphicsPixmapItem *p = view.scene()->addPixmap(QPixmap(":/cappella_sistina.jpg"));
QGraphicsOpacityEffect opacity;
opacity.setOpacityMask(QPixmap(":/star.png").mask());
p->setGraphicsEffect(&opacity);
view.show();
return a.exec();
}
Source code
You can download the code from here: Media:GraphicsEffect-Opacity.zip



(no comments yet)