In this post, you will learn how to change window opacity using Qt creator.
Steps:
- Open Qt Creator Click on File from the menu and click on New File or Project…(Ctrl + N).
- From the left column select Application, from the middle column select Qt Widget Application.
- Click on Choose.
- Give a name to the project and set the path where you want to save this project files.
- Click Next,
- Default kits will be selected.
- Click next.
- Give the class name of your choice. I gave opacity.
- Select Baseclass as QMainWindow from the dropdown list.
- (Generate form box can be unchecked as we are not going to use it, we will be creating UI with code only).
- Click Next
- Click on Finish.
main.cpp doesn`t need to be modified.
Open opacity.h
and modify it as
#ifndef OPACITY_H #define OPACITY_H #include <QMainWindow> #include <QDebug> #include <QLabel> #include <QKeyEvent> #include <QEvent> namespace Ui { class Opacity; } class Opacity : public QMainWindow { Q_OBJECT QLabel *lbl_opacity_val; QMap <int,qreal> map; QMap<int, qreal>::iterator itr; public: explicit Opacity(QWidget *parent = nullptr); ~Opacity(); private: Ui::Opacity *ui; bool eventFilter(QObject*,QEvent*); void setOpacity(qreal); }; #endif // OPACITY_H
Open opacity.cpp
and copy-paste below mentioned code.
Restricted content. Please login to view the content.
Download Project files
Restricted content. Please login to view the content.
Video
Thank you for reading.