PDA

View Full Version : QGraphicsView and adding QWidget with transparent areas



sting73
27th April 2009, 14:55
Hello,

I want to use my own Button-Widget in a QGraphicsView. Adding and displaying the widget works fine. Unfortunately this Button has round corners which are not displayed transparent in the view tree. When I use the the same widget in a QMainWindow the outside areas are drawn transparent. The same effect occur by using a default Qt-Widget like QDial.

This code shows the widget with transparent behavior:
(I used QTextEdit just to get (quick and dirty) a white background)


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow main;
QTextEdit * pEdit = new QTextEdit(&main);
QDial * pDial = new QDial(&main);
pEdit->setGeometry(0,0,main.size().width(),main.size().he ight());
pDial->setGeometry(40,00,100,100);
main.show();
return a.exec();
}

The following code shows the same widget in a QGraphicsView with opaque behavior:


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
scene.setBackgroundBrush(Qt::white);
QDial * pDial = new QDial(NULL);
QGraphicsProxyWidget * pWidget = scene.addWidget(pDial,Qt::Widget);
QGraphicsView view(&scene);
view.show();
return a.exec();
}

Can anyone give me a hint how to render the widget with transparent corners?
Thanks in advance!

Denis

aamer4yu
27th April 2009, 20:13
Why did u set background of scene as white ?

You should try setting background of scene and view as transparent. And also you can set the top level window flag WA_TranslucentBackground (chk spelling :D) .

There was also some example on Qt Labs where graphics item with transparent background were used... cant remember the link :(

sting73
28th April 2009, 12:19
hi aamer4yu,

the hint with the WA_TranslucentBackground flag was all I needed. Setting this flag in the constructor of the derrived widget sets the background to transparent.

Thank you very much!