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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4278723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:01:11  来源:igfitidea点击:

A qt widget with fully transparent background

c++qtwidgettransparent

提问by B?ови?

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 Caleb Huitt - cjhuitt

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.

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

回答by Harald Scheirich

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

应该可以工作,样式表可以很容易地在设计器中进行测试

回答by Dave

You may want to look at:

你可能想看看:

setAttribute( Qt::WA_NoSystemBackground, true );

and

setAttribute( Qt::WA_OpaquePaintEvent, false );

回答by Violet Giraffe

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);

回答by Martin Beckett

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,但应该是相关的。