BЈовић
BЈовић

Reputation: 64283

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.

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?

Upvotes: 9

Views: 41060

Answers (4)

Harald Scheirich
Harald Scheirich

Reputation: 9764

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

Upvotes: 3

Dave
Dave

Reputation: 2741

You may want to look at:

setAttribute( Qt::WA_NoSystemBackground, true );

and

setAttribute( Qt::WA_OpaquePaintEvent, false );

Upvotes: 0

Violet Giraffe
Violet Giraffe

Reputation: 33617

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

Upvotes: 1

Caleb Huitt - cjhuitt
Caleb Huitt - cjhuitt

Reputation: 14951

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.

Upvotes: 4

Related Questions