PDA

View Full Version : Freehand drawing on transparent widget.



vaibhav
19th November 2010, 06:59
Hi,
My requirement is that my Widget should be transparent and
receives all mouse Events so that i can draw Freehand drawing on transparent widget which gives me feeling as if i am drawing on desktop.

1.I tried using GrabMouse() for transparent widget but it propagates the events to windows(Desktop).I want those events into my Application.)
2.I tried to set transparency with windowOpacity(0.0) but then my Free Hand drawing also becomes transparent.
3.By using Qt::WA_TranslucentBackground for frame less window transparency is achieved but it doesn't receive mouse Move Event.
Is there any way to achieve this directly through QT or i have to use Windows API's....?.
Any help would be appreciated.

Thank you ....
Vaibhav

totem
19th November 2010, 08:08
This is possible using Qt::WA_TranslucentBackground on a QGraphicsView, and managing your paint events.
I did it, but I had to specify an opacity a bit greater than 0 in order to receive mouse events.
(I pm you a link)

vaibhav
19th November 2010, 09:27
This is possible using Qt::WA_TranslucentBackground on a QGraphicsView, and managing your paint events.
I did it, but I had to specify an opacity a bit greater than 0 in order to receive mouse events.
(I pm you a link)

Hi,
Thanx for replying.... I tried it using QGraphicsview. Qt::WA_TranslucentBackground for it is not working i even tried making my window frameless still its not getting transparent.

And specifying opacity is actually not required with Qt::WA_TranslucentBackground for frameless window.
Even if i specify my opacity to some value say 0.1 it makes my painting(painter) transparent too which i do not want.

Please let me know if i am missing anything.

totem
19th November 2010, 13:19
in the widget constructor :



MyWidget::MyWidget(QWidget *parent)
: QGraphicsView(parent)
{
//...
setWindowFlags( Qt::FramelessWindowHint ) ;
setAttribute( Qt::WA_TranslucentBackground );
//...
}


Then, you have to reimplement your widget's ::paintEvent method :



void MyWidget::paintEvent(QPaintEvent * e)
{
QPainter painter( viewport() );

// draw widget's frame and titlebar
if( p_cache!=NULL )
{
painter.drawPixmap(0, 0, *p_cache);
}

// draw scene items
QGraphicsView::paintEvent(e);
}


And manage a cached pixmap :



void MyWidget::resizeEvent(QResizeEvent *)
{
// redraw window
invalidateCache() ;
}

void MyWidget::invalidateCache( void )
{
if( p_cache!=NULL )
{
delete p_cache;
}

p_cache = new QPixmap(size());

p_cache->fill(QColor(0,0,0,1)/*Qt::transparent*/);

// draw whatever you want
// ...
}


worked for me
(and i was wrong: apparently the window is fully transparent)

vaibhav
24th November 2010, 06:33
Thnx it worked....