Results 1 to 9 of 9

Thread: Qt + WINAPI + direct painting

  1. #1
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt + WINAPI + direct painting

    I'm experimenting with direct painting to device contexts using WINAPI. Here's what my widget's paintEvent function looks like:

    Qt Code:
    1. void ShotView::paintEvent(QPaintEvent *event)
    2. {
    3. qDebug() << "Start paint function";
    4.  
    5. HDC hdc = this->getDC();
    6.  
    7. if (hdc == NULL)
    8. {
    9. qDebug() << "HDC error";
    10. return;
    11. }
    12.  
    13. qDebug() << "Start painting";
    14.  
    15. HPEN hPen = CreatePen(PS_SOLID, 4, RGB(0, 255, 0));
    16. if (hPen == NULL)
    17. qDebug() << "CreatePen error";
    18. if (SelectObject(hdc, hPen) == HGDI_ERROR)
    19. qDebug() << "SelectObject error";
    20. if (MoveToEx(hdc, 0, 0, NULL) == 0)
    21. qDebug() << "MoveToEx error";
    22. if (LineTo(hdc, 200, 200) == 0)
    23. qDebug() << "LineTo error";
    24.  
    25. qDebug() << "End painting";
    26.  
    27. this->releaseDC(hdc);
    28.  
    29. qDebug() << "End paint function";
    30. }
    To copy to clipboard, switch view to plain text mode 

    When I run the program, I don't see a solid red line appearing as I should. The console output seems correct:

    Start paint function
    Start painting
    End painting
    End paint function
    so it looks like everything in the function is getting called properly, but I don't see an output. It's been a while since I've done WINAPI stuff, but I think I have it right. Can anyone offer any insight?

  2. #2
    Join Date
    Feb 2007
    Posts
    49
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt + WINAPI + direct painting

    What about setting flag Qt::WA_PaintOnScreen?

  3. #3
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt + WINAPI + direct painting

    Quote Originally Posted by minimoog View Post
    What about setting flag Qt::WA_PaintOnScreen?
    I read in the docs this attribute is only supported under X11, and I'm running Windows.
    Still, I tried it, no effect.

    [edit]
    I made a discovery. If I inherit from QWidget instead of QGraphicsView, it still doesn't work, but if I rapidly resize the widget I can see the line flickering, until it (presumably) gets painted over.

    So what in QWidget is being called after paintEvent that's painting over my line?
    Last edited by JovianGhost; 24th March 2010 at 12:22.

  4. #4
    Join Date
    Feb 2007
    Posts
    49
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt + WINAPI + direct painting

    Set Qt::WA_PaintOnScreen in the QWidget constructor. Reimplement QPaintDevice:aintEngine() to return a null pointer. Just like is written in documentation on Qt::WA_PaintOnScreen.

    It should be working.

  5. The following user says thank you to minimoog for this useful post:

    JovianGhost (25th March 2010)

  6. #5
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt + WINAPI + direct painting

    Why are you using WinAPI to paint? Is there a problem with QPainter?
    Last edited by pherthyl; 24th March 2010 at 17:14.

  7. The following user says thank you to pherthyl for this useful post:

    JovianGhost (25th March 2010)

  8. #6
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt + WINAPI + direct painting

    I got it working, finally!

    Quote Originally Posted by minimoog View Post
    Set Qt::WA_PaintOnScreen in the QWidget constructor. Reimplement QPaintDevice:aintEngine() to return a null pointer. Just like is written in documentation on Qt::WA_PaintOnScreen.

    It should be working.
    That's what I did at first, but it didn't work. After you said this I went back to check it out and noticed something: when overriding paintEngine() I forgot to put the trailing "const" in the declaration and definition, so instead of

    Qt Code:
    1. QPaintEngine* paintEngine() const { return 0; }
    To copy to clipboard, switch view to plain text mode 

    I had

    Qt Code:
    1. QPaintEngine* paintEngine() { return 0; }
    To copy to clipboard, switch view to plain text mode 

    which of course did nothing. So thanks!

    Quote Originally Posted by pherthyl View Post
    Why are you using WinAPI to paint? Is there a problem with QPainter?
    To be honest, I never really considered QPainter. I was trying to do this as low-level as possible to reduce the overhead introduced by Qt's painting framework. But it looks like QPainter will do exactly what I need, thanks!

  9. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt + WINAPI + direct painting

    But it looks like QPainter will do exactly what I need, thanks!
    Thats how it is done in Qt.

    To be honest, I never really considered QPainter. I was trying to do this as low-level as possible to reduce the overhead introduced by Qt's painting framework.
    Then why use Qt... there will be some overhead still :P
    juzz kidding.. theres always tradeoff between complexity of code and runtime. And Qt's paint framework is not that slow..also if you use QPainter, your code will be cross platform !

  10. #8
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt + WINAPI + direct painting

    Well I've been experimenting with QPainter and so far it's working very well!

  11. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt + WINAPI + direct painting

    Quote Originally Posted by JovianGhost View Post
    Well I've been experimenting with QPainter and so far it's working very well!
    Good to know ... I guess you had underestimated Qt's performance :P

Similar Threads

  1. Using Winapi in Qt?
    By Awareness in forum Qt Programming
    Replies: 3
    Last Post: 23rd March 2010, 03:04
  2. Winapi in Qt
    By Trok in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2009, 16:38
  3. Using WinAPI FindWindow with Qt
    By emostar in forum Qt Programming
    Replies: 16
    Last Post: 11th July 2009, 09:27
  4. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 07:07
  5. Getting QLabel to work with WinAPI GetWindowText?
    By m.parker in forum Qt Programming
    Replies: 3
    Last Post: 22nd December 2006, 21:22

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.