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

2022-01-18 00:00:00 qt widget transparent c++

我需要创建一个 qt 小部件,它将充当其他一些小部件的父级,并为它们排序.

I need to create a qt widget, which will act as a parent for some other widgets, and which will order them.

现在的问题是如何让它的背景完全透明?

Now, the question is how do I make it's background fully transparent?

我想这样做:

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?

推荐答案

在 Qt4 中默认情况下,QWidget 不会为自己的背景绘制任何内容,只会绘制其子项.如果你想覆盖它,你必须特别告诉小部件通过它的属性之一来绘制它的背景.请注意,从 QWidget 派生的一些小部件会自动绘制背景.

By default in Qt4, a QWidget will draw nothing for its own background, and only its children will be drawn. 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.

相关文章