Image Composition Example▲
Setting Up The Resource File▲
The Image Composition example requires two source images, butterfly.png and checker.png that are embedded within imagecomposition.qrc. The file contains the following code:
&
lt;!
DOCTYPE RCC&
gt;&
lt;RCC version=
"1.0"
&
gt;
&
lt;qresource&
gt;
&
lt;file&
gt;images/
butterfly.png&
lt;/
file&
gt;
&
lt;file&
gt;images/
checker.png&
lt;/
file&
gt;
&
lt;/
qresource&
gt;
&
lt;/
RCC&
gt;
For more information on resource files, see The Qt Resource System.
ImageComposer Class Definition▲
The ImageComposer class is a subclass of QWidget that implements three private slots, chooseSource(), chooseDestination(), and recalculateResult().
class
ImageComposer : public
QWidget
{
Q_OBJECT
public
:
ImageComposer();
private
slots:
void
chooseSource();
void
chooseDestination();
void
recalculateResult();
In addition, ImageComposer consists of five private functions, addOp(), chooseImage(), loadImage(), currentMode(), and imagePos(), as well as private instances of QToolButton, QComboBox, QLabel, and QImage.
private
:
void
addOp(QPainter::
CompositionMode mode, const
QString &
amp;name);
void
chooseImage(const
QString &
amp;title, QImage *
image, QToolButton *
button);
void
loadImage(const
QString &
amp;fileName, QImage *
image, QToolButton *
button);
QPainter::
CompositionMode currentMode() const
;
QPoint imagePos(const
QImage &
amp;image) const
;
QToolButton *
sourceButton;
QToolButton *
destinationButton;
QComboBox *
operatorComboBox;
QLabel *
equalLabel;
QLabel *
resultLabel;
QImage sourceImage;
QImage destinationImage;
QImage resultImage;
}
;
ImageComposer Class Implementation▲
We declare a QSize object, resultSize, as a static constant with width and height equal to 200.
static
const
QSize resultSize(200
, 200
);
Within the constructor, we instantiate a QToolButton object, sourceButton and set its iconSize property to resultSize. The operatorComboBox is instantiated and then populated using the addOp() function. This function accepts a QPainter::CompositionMode, mode, and a QString, name, representing the name of the composition mode.
ImageComposer::
ImageComposer()
{
sourceButton =
new
QToolButton;
sourceButton-&