9

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?

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?

BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • Do you really need a _transparent_ background, or is it sufficient to have _no_ background? This is a real difference: with a transparent background, the OS will tell the _underlying_ window to paint the pixels. Without a background, the OS just expects the children to paint the entire window. – MSalters Nov 25 '10 at 16:22
  • @MSalters This widget is places in a window, and only the child widgets of this widgets should be visible. Whatever is behind this widget (if not covered by a child widget) should be seen. – BЈовић Nov 25 '10 at 16:28
  • I'm not sure I understand this question... by default in Qt4, a QWidget will draw nothing for its own background, and only its children will be drawn. – Caleb Huitt - cjhuitt Dec 01 '10 at 22:21
  • @Caleb Yes, that seams to be the correct answer. Why did you answer as comment? – BЈовић Dec 01 '10 at 22:51
  • I'll put in an answer, but I thought I must have been missing something, since the default behavior is so easy to observe working. – Caleb Huitt - cjhuitt Dec 02 '10 at 14:27
  • 1
    Possible duplicate of [Make QWidget transparent](https://stackoverflow.com/questions/25466030/make-qwidget-transparent) – Violet Giraffe Dec 01 '17 at 11:52

5 Answers5

4

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.

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
  • What if I just want to unset previously set background (`backgroundRole`). – Tomáš Zato Jan 26 '16 at 09:58
  • @TomášZato: The easiest way to to query and remember the previous setting, and use that to restore it. One of the harder ways would be to query the widget for it's parent, get the parent's background role, and set that on the widget. All this is complicated if you need to leave the rest of the palette as it is. – Caleb Huitt - cjhuitt Jan 26 '16 at 21:15
3

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Harald Scheirich
  • 9,676
  • 29
  • 53
0

You may want to look at:

setAttribute( Qt::WA_NoSystemBackground, true );

and

setAttribute( Qt::WA_OpaquePaintEvent, false );
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Dave
  • 2,653
  • 4
  • 20
  • 22
0

The solution that worked for me (I was setting a transparent background for a QTextEditor):

auto editorPalette = editorWidget->palette();
editorPalette.setColor(QPalette::Active, QPalette::Base, Qt::transparent);
editorPalette.setColor(QPalette::Inactive, QPalette::Base, Qt::transparent);
editorWidget->setPalette(editorPalette);
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
-1

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

The solution is for Qt4.1 but should be relevent.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263