How to do it…

Let's learn how to apply image effects to text and graphics by following these steps:

  1. Create a new Qt Widgets Application project and remove the menuBar, mainToolBar, and StatusBar.
  1. Create a new resource file by going to File | New File or Project and adding all the images required by the project:

  1. Next, open up mainwindow.ui and add four labels to the window. Two of the labels will be text, and the two others we will load with the images we have just added to the resource file:

  1. You may already notice that the font sizes are way bigger than the default size. That can be achieved by adding a style sheet to the label widget, for example, as follows:
font: 26pt "MS Shell Dlg 2";
  1. After that, open up mainwindow.cpp and include the following headers at the top of the source code:
#include <QGraphicsBlurEffect>
#include <QGraphicsDropShadowEffect>
#include <QGraphicsColorizeEffect>
#include <QGraphicsOpacityEffect>
  1. Then, within the constructor of the MainWindow class, add the following code to create a DropShadowEffect, and apply it to one of the labels:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
QGraphicsDropShadowEffect* shadow = new
QGraphicsDropShadowEffect();
shadow->setXOffset(4);
shadow->setYOffset(4);
ui->label->setGraphicsEffect(shadow);
}
  1. Next, we will create a ColorizedEffect and apply it to one of the images, in this case the butterfly. We also set the effect color to red:
QGraphicsColorizeEffect* colorize = new QGraphicsColorizeEffect();
colorize->setColor(QColor(255, 0, 0));
ui->butterfly->setGraphicsEffect(colorize);
  1. Once we're done with that, create a BlurEffect and set its radius to 12. Then, apply the graphics effect to the other label:
QGraphicsBlurEffect* blur = new QGraphicsBlurEffect();
blur->setBlurRadius(12);
ui->label2->setGraphicsEffect(blur);
  1. Lastly, create an alpha effect and apply it to the penguin image. We set the opacity value to 0.2, which means 20% opacity:
QGraphicsOpacityEffect* alpha = new QGraphicsOpacityEffect();
alpha->setOpacity(0.2);
ui->penguin->setGraphicsEffect(alpha);
  1. Compile and run the program now, and you should be able to see something like this:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset