Results 1 to 5 of 5

Thread: Freehand drawing on transparent widget.

  1. #1
    Join Date
    Mar 2009
    Location
    delhi India
    Posts
    56
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Freehand drawing on transparent widget.

    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

  2. #2
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Freehand drawing on transparent widget.

    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)

  3. #3
    Join Date
    Mar 2009
    Location
    delhi India
    Posts
    56
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Freehand drawing on transparent widget.

    Quote Originally Posted by totem View Post
    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.

  4. #4
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Freehand drawing on transparent widget.

    in the widget constructor :

    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent)
    2. : QGraphicsView(parent)
    3. {
    4. //...
    5. setWindowFlags( Qt::FramelessWindowHint ) ;
    6. setAttribute( Qt::WA_TranslucentBackground );
    7. //...
    8. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void MyWidget::paintEvent(QPaintEvent * e)
    2. {
    3. QPainter painter( viewport() );
    4.  
    5. // draw widget's frame and titlebar
    6. if( p_cache!=NULL )
    7. {
    8. painter.drawPixmap(0, 0, *p_cache);
    9. }
    10.  
    11. // draw scene items
    12. QGraphicsView::paintEvent(e);
    13. }
    To copy to clipboard, switch view to plain text mode 

    And manage a cached pixmap :

    Qt Code:
    1. void MyWidget::resizeEvent(QResizeEvent *)
    2. {
    3. // redraw window
    4. invalidateCache() ;
    5. }
    6.  
    7. void MyWidget::invalidateCache( void )
    8. {
    9. if( p_cache!=NULL )
    10. {
    11. delete p_cache;
    12. }
    13.  
    14. p_cache = new QPixmap(size());
    15.  
    16. p_cache->fill(QColor(0,0,0,1)/*Qt::transparent*/);
    17.  
    18. // draw whatever you want
    19. // ...
    20. }
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Mar 2009
    Location
    delhi India
    Posts
    56
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Freehand drawing on transparent widget.

    Thnx it worked....

Similar Threads

  1. Drawing QIconSet-s on a transparent QPixmap ?
    By ultr in forum Qt Programming
    Replies: 2
    Last Post: 12th March 2009, 15:10
  2. Freehand painting with gradient brush
    By olidem in forum Qt Programming
    Replies: 4
    Last Post: 6th March 2009, 09:25
  3. Regarding drawing on Transparent Window
    By Shalabh in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2007, 11:32
  4. Qt4.1 Transparent Widget
    By djoul in forum Qt Programming
    Replies: 14
    Last Post: 26th September 2006, 17:06
  5. transparent widget
    By hijinks in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2006, 10:43

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.