[Qt UI related] Qt sets the background color of a form or control to be transparent


Qt sets the appearance color and transparency of a form or control

1. Use QPalette to set form transparency

In Qt, QPalettethe (palette) class is used to set the appearance color of a form or control. QPaletteFirst, you need to include or header files in your code QGui.

#include <QPalette>
// 或
#include <QGuiApplication>

Then, add the following code in the constructor to set the form's transparency:

QPalette pal = palette();
pal.setColor(QPalette::Background, QColor(0x00, 0xff, 0x00, 0x00));
setPalette(pal);

Here, we use QPalette::Backgroundto set the background color of the form and set it to be fully transparent.

Effect:
The entire window is transparent, but the window controls are opaque. For example, QLabelthe control only displays text and the background color of the control is transparent. The form client area is also completely transparent.

2. Use the setWindowOpacity method

Another way to set the transparency of a form is to use setWindowOpacitya function.

setWindowOpacity(0.5);

This will set the form's transparency to 50%.

3. Use the setStyleSheet method

You can also use setStyleSheetmethods to set the transparency of a form or control.

setStyleSheet("background-color: rgba(255, 255, 255, 50);");

Here, we use the RGBA color model, where A (Alpha) is used to set the transparency.

4. Use setAttribute and setAutoFillBackground methods

setAttribute(Qt::WA_TranslucentBackground);
setAutoFillBackground(false);

This method is often used to make the form background transparent while preserving the opacity of the controls.

5. Use the QGraphicsOpacityEffect class

QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
effect->setOpacity(0.5);
setGraphicsEffect(effect);

Here, we create an QGraphicsOpacityEffectobject and set its transparency to 0.5.

6. Use window flags

setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);

Here, we use the window flag to hide the form's borders and keep it on top, thus indirectly affecting its transparency.

Summarize

The above are six methods for setting the transparency of a form or control. Each method has its use cases and limitations, so which one you choose depends on your specific needs.

As Bjarne Stroustrup said in "The C++ Programming Language": "C++ is a multi-paradigm programming language that can be used for a variety of programming styles." These methods demonstrate the flexibility of C++ and Qt libraries in graphical interface design.

Through these methods, we can not only achieve the transparency effect of the form, but also have a deeper understanding of how the Qt library interacts with the underlying graphical interface and how to affect the visual performance through code.

method Applicable scene limit
QPalette Simple background transparent Control is opaque
setWindowOpacity The entire form is transparent none
setStyleSheet Highly customizable Requires familiarity with CSS
setAttribute和setAutoFillBackground Control and background transparency are different none
QGraphicsOpacityEffect Dynamically change transparency Additional CPU consumption
window sign Hide form borders May affect other properties of the form

Hopefully this article helped you better understand how to control the transparency of forms and controls using Qt and C++, and how to choose the method that best suits your needs.

Conclusion

In our programming learning journey, understanding is an important step for us to move to a higher level. However, mastering new skills and ideas always requires time and persistence. From a psychological point of view, learning is often accompanied by constant trial and error and adjustment, which is like our brain gradually optimizing its "algorithm" for solving problems.

This is why when we encounter mistakes, we should view them as opportunities to learn and improve, not just as annoyances. By understanding and solving these problems, we can not only fix the current code, but also improve our programming skills and prevent making the same mistakes in future projects.

I encourage everyone to actively participate and continuously improve their programming skills. Whether you are a beginner or an experienced developer, I hope my blog will be helpful on your learning journey. If you find this article useful, you may wish to click to bookmark it, or leave your comments to share your insights and experiences. You are also welcome to make suggestions and questions about the content of my blog. Every like, comment, share and attention is the greatest support for me and the motivation for me to continue sharing and creating.


Read my CSDN homepage and unlock more exciting content: Bubble’s CSDN homepage
Insert image description here

Guess you like

Origin blog.csdn.net/qq_21438461/article/details/132913944