简体   繁体   English

具有完全透明背景的Qt小部件

[英]A qt widget with fully transparent background

I need to create a qt widget, which will act as a parent for some other widgets, and which will order them. 我需要创建一个qt小部件,它将充当其他一些小部件的父级,并对它们进行排序。

Now, the question is how do I make it's background fully transparent? 现在,问题是如何使背景完全透明?

I thought to do it like this : 我想这样做:

struct Imp
{
  Imp( QWidget *parent ) : thisWidget( new QWidget( parent ) )
  {
    thisWidget->setAttribute( Qt::WA_TranslucentBackground, true );
  }

  QWidget *thisWidget;
};

Do you think that I need to set the attribute, or is it going to work fine without it? 您是否认为我需要设置属性,或者没有它,它是否可以正常工作?

By default in Qt4, a QWidget will draw nothing for its own background, and only its children will be drawn. 在Qt4中,默认情况下,QWidget不会为其背景绘制任何内容,并且只会绘制其子级。 If you want to override that, you specifically have to tell the widget to draw its background via one of its properties. 如果要覆盖它,则必须专门告诉小部件通过其属性之一来绘制其背景。 Note that some widgets derived from QWidget will automatically draw backgrounds. 请注意,一些从QWidget派生的小部件将自动绘制背景。

You should be able to do all the drawing customisation you need by changing the style of your widget i think 我认为您应该能够通过更改小部件的样式来完成所需的所有图形定制

MyWidget {background-color: none;}

should work, stylesheets can very easily be tested in the designer 应该工作,样式表可以很容易地在设计器中进行测试

You may want to look at: 您可能要看一下:

setAttribute( Qt::WA_NoSystemBackground, true );

and

setAttribute( Qt::WA_OpaquePaintEvent, false );

The solution that worked for me (I was setting a transparent background for a QTextEditor): 对我有用的解决方案(我为QTextEditor设置了透明背景):

auto editorPalette = editorWidget->palette();
editorPalette.setColor(QPalette::Active, QPalette::Base, Qt::transparent);
editorPalette.setColor(QPalette::Inactive, QPalette::Base, Qt::transparent);
editorWidget->setPalette(editorPalette);

Don't know if it fully solves your problem but it is discussed in this article 不知道它是否可以完全解决您的问题,但本文将对此进行讨论

Documentation is at http://doc.qt.nokia.com/4.1/qwidget.html#transparency-and-double-buffering 文档位于http://doc.qt.nokia.com/4.1/qwidget.html#transparency-and-double-buffering

The solution is for Qt4.1 but should be relevent. 该解决方案适用于Qt4.1,但应该是相关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM