PDA

View Full Version : Regarding drawing on Transparent Window



Shalabh
25th May 2007, 11:27
Hi All

I need help for what i am trying to do. Here is my task.



"After starting my app I want to make user able to draw an irregular shape on a transparent window by pressing and moving mouse on the transparent window. I have to make sure that i can always see the actives of all the window behind my transparent window. I want to do all this thing using QT since i want my app to work on Mac and Linux also."


My app collects path elements in a QPainterPath and i can draw it using "painter.drawPath(myPath)" but if i change opacity to 0.15 then it also make drawn path invisible which is quite obvious.

Thanks in advance for help.


Here is some code of my app.

File: QTransparentWindow.h

#include <QtGui>
class QTransparentWindow : public QMainWindow
{
Q_OBJECT

private:
QPainterPath *m_pPainterPath;

public:
QTransparentWindow();
~QTransparentWindow();
void paintEvent ( QPaintEvent * event);
void mousePressEvent ( QMouseEvent * e );
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);

};


File: QTransparentWindow.cpp

#include "QTransparentWindow.h"

QTransparentWindow::QTransparentWindow()
{
m_pPainterPath = NULL;

setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_NoSystemBackground);


// Make transparent window size equal to the whole Screen size
int nScreenWidth = QApplication::desktop()->width();
int nScreenHeight = QApplication::desktop()->height();
setGeometry(0, 0, nScreenWidth, nScreenHeight);

setMouseTracking(true);
}

QTransparentWindow::~QTransparentWindow()
{
if(m_pPainterPath != NULL)
{
delete m_pPainterPath;
m_pPainterPath = NULL;
}
}

void QTransparentWindow::paintEvent( QPaintEvent * event)
{
QPainter painter(this);

painter.save();
QBrush brush(Qt::Dense2Pattern);
QPen pen(brush, 3 , Qt::DashLine, Qt::FlatCap, Qt::MiterJoin);
painter.setPen(pen);

//painter.drawRect(100, 100, 200, 200);
if(m_pPainterPath != NULL)
{
if(!m_pPainterPath->isEmpty())
painter.drawPath(*m_pPainterPath);
}
painter.restore();

}


void QTransparentWindow::mousePressEvent ( QMouseEvent * e )
{
if(m_pPainterPath != NULL)
{
delete m_pPainterPath;
m_pPainterPath = NULL;
}

m_pPainterPath = new QPainterPath;
m_pPainterPath->setFillRule(Qt::WindingFill);
m_pPainterPath->moveTo(e->x(), e->y());

}

void QTransparentWindow::mouseMoveEvent(QMouseEvent *e)
{
if(m_pPainterPath != NULL)
{
QPoint ptMousePos = e->pos();
m_pPainterPath->lineTo(e->x(), e->y());

// We need to resize window so that window get repainted.
// Since after setting windows attribute to "Qt::WA_NoSystemBackground"
// repaint(visibleRegion()) doesn't calls paintEvent

QSize winSize = this->size();
winSize.setHeight(winSize.height()+1);
this->resize(winSize);

QApplication::processEvents();

winSize.setHeight(size.height()-1);
this->resize(winSize);

}
}

void QTransparentWindow::mouseReleaseEvent(QMouseEvent *e)
{
if(m_pPainterPath != NULL)
{
delete m_pPainterPath;
m_pPainterPath = NULL;
}
}


I am very confused about how can i acheive drawing on a transparent window using QT.
I want to use this app on Windows, Mac and Linux but i want to avoid platform specific coding as much as possible.
Is there any way to do the same.



Thanks and Regards,
Shalabh

Shalabh
30th May 2007, 12:14
Is there no one who can help me out in this matter?
:confused:

Did i asked a wrong question?? :crying:

Can anyone tell me that whats worng in this post?
Why no one has relied yet.. :eek:


Thanks in advance,
Shalabh

marcel
30th May 2007, 12:17
I'm afraid you cannot do this unless your window manager supports desktop compositions.
On XP will never work.

Regards

Shalabh
31st May 2007, 11:32
Ok,
Can i do it specifically for Mac and Linux.
Or just for Mac.
Please give me some suggestion....