Results 1 to 4 of 4

Thread: Regarding drawing on Transparent Window

  1. #1
    Join Date
    Apr 2007
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Question Regarding drawing on Transparent Window

    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

  2. #2
    Join Date
    Apr 2007
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Unhappy Re: Regarding drawing on Transparent Window

    Is there no one who can help me out in this matter?


    Did i asked a wrong question??

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


    Thanks in advance,
    Shalabh
    Thanks and Regards,
    Shalabh

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Regarding drawing on Transparent Window

    I'm afraid you cannot do this unless your window manager supports desktop compositions.
    On XP will never work.

    Regards

  4. #4
    Join Date
    Apr 2007
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Regarding drawing on Transparent Window

    Ok,
    Can i do it specifically for Mac and Linux.
    Or just for Mac.
    Please give me some suggestion....
    Thanks and Regards,
    Shalabh

Similar Threads

  1. Transparent window with QGLWidget
    By ultr in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2008, 08:01
  2. Window not responding
    By markcole in forum Qt Programming
    Replies: 10
    Last Post: 18th April 2007, 22:53
  3. move parent window to the front.
    By hvengel in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2007, 09:41
  4. Making some part of window transparent
    By yogeshm02 in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2006, 21:36
  5. cannot make a main window modal
    By Dark_Tower in forum Qt Programming
    Replies: 12
    Last Post: 23rd March 2006, 11:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.