Qt button transparency - [solve embedded devices, button press background color is black or different transparency]

Requirement: Non-sensing button (transparent button)

Reference: The same is the case, the following problem was encountered, and the elder brother gave the reason

  1. The effect of setFlat(true) is: do not click the button to be transparent, click the button, and the original outline and color of the button can be displayed;

  2. The effect of background-color: rgba(0, 0, 0, 0) is: do not click the button to be transparent, click the button, the outline of the button is displayed, but the button is black;

Solve the embedded device, the button press background color is black or different transparent

  • Use the setFlat function to achieve the effect of button transparency
QPushButton *mode =new QPushButton(this);
mode->setFlat(true);//实现按钮透明的效果

But sometimes it is not necessarily the setting truethat will take effect, but the setting falsewill take effect

solve the case

  1. No font key pressed opaque
// 不生效
bt->setFlat(true);
bt->setFocusPolicy(Qt::NoFocus);

bt->setStyleSheet("QPushButton{
    
    font-size:24px;
color:#ffffff;text-align:left;border-color:rgba(0,0,0,0);}");
// 生效
bt->setFlat(false);
bt->setFocusPolicy(Qt::NoFocus);

bt->setStyleSheet("border:nonr;background:transparent;
color:#ffffff;text-align:left;font-size:24px;");
  1. There are font keys pressed opaque
// 不好使
bt->setStyleSheet("QPushButton{
    
    font-size:%24px;color:#ffffff;
text-align:left;background-color:rgba(0,0,0,0);}");

// 好使
bt->setStyleSheet("border:nonr;background:transparent;
color:#ffffff;text-align:left;font-size:24px;");
  • In this way, you can press transparent, no need to set a blank transparent image



Guess you like

Origin blog.csdn.net/qq_47355554/article/details/128343123