Results 1 to 6 of 6

Thread: Using StretchDIBits() in QWidget

  1. #1
    Join Date
    Sep 2006
    Posts
    19
    Thanks
    12

    Default Using StretchDIBits() in QWidget

    Hi

    I've subclasses a QWidget because I want to perform my own drawing using Windows' StretchDIBits() function (I'm painting video frames). I've overwritten QWidget::paintEvent() where I'm doing my drawing like this:

    Qt Code:
    1. void MyWidget::paintEvent(QPaintEvent *p)
    2. {
    3. HWND hWnd = winId();
    4. HDC hdc;
    5. PAINTSTRUCT ps;
    6. hdc = BeginPaint(hWnd, &ps);
    7. BOOL b = StretchDIBits(hdc, 0, 0, rect().width(), rect().height(), 0, 0,
    8. bmh.biWidth, bmh.biHeight*-1, frm->frame, &bmi,
    9. DIB_RGB_COLORS, SRCCOPY);
    10. EndPaint(hWnd, &ps);
    11. }
    To copy to clipboard, switch view to plain text mode 

    'bmh' is a BITMAPINFOHEADER and 'frm' is a byte-array of bitmap data.

    When the paintEvent is called nothing is, however, shown. Why is this? When I use the above code in my MFC app it works fine (when I receive WM_PAINT).

    -- Bjoern
    Last edited by wysota; 4th May 2009 at 19:20. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Using StretchDIBits() in QWidget

    You joined 2006 and now is your first post Strange. However, nowadays we have [ CODE ] tags for a good display of code fragments. Pleas use them in further post.

    To your question: Don't you have to use the painter of the widget:
    Qt Code:
    1. QPainter painter(this);
    To copy to clipboard, switch view to plain text mode 
    ?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using StretchDIBits() in QWidget

    WM_PAINT and paintEvent are not equivalent. I don't know if this will work but try setting the WA_PaintOnScreen attribute on the widget and do the other things mentioned in the attribute's docs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Sep 2006
    Posts
    19
    Thanks
    12

    Default Re: Using StretchDIBits() in QWidget

    Hi

    Thanks for the quick replies. Actually I'm trying to make a DLL which receives a HWND and then paints a bitmap in the window. I thought I could do this in paintEvent() by providing my DLL function with the window's HWND. Can't this work with QWidget?

    Btw, yes, it's been three years since I last used Qt. Trolltech changed their license model so it suddenly became interesting to try out in my new company.

    I've also tried the following which also doesn't work (and probably for the same reason):

    Qt Code:
    1. void MyWidget::paintEvent(QPaintEvent *p)
    2. {
    3. HWND hWnd = winId();
    4. HDC hdc;
    5. hdc = ::GetDC(hWnd);
    6. RECT r;
    7. r.left = this->rect().left()+50;
    8. r.top = this->rect().top()+50;
    9. r.right = this->rect().right();
    10. r.bottom = this->rect().bottom();
    11. int xx = ::DrawText(hdc, L"FOOOOOOOOOO", 5, &r, DT_VCENTER | DT_CENTER | DT_SINGLELINE);
    12. ReleaseDC(hWnd, hdc);
    13. }
    To copy to clipboard, switch view to plain text mode 

    -- Bjoern

  5. #5
    Join Date
    Sep 2006
    Posts
    19
    Thanks
    12

    Default Re: Using StretchDIBits() in QWidget

    Ah, I found the solution on page 417 in the QT book. It will work using something like this:
    Qt Code:
    1. void MyWidget::paintEvent(QPaintEvent *p)
    2. {
    3. QPainter painter(this);
    4. HWND hWnd = winId();
    5. HDC hdc = painter.paintEngine()->getDC();
    6. RECT r;
    7. r.left = this->rect().left()+50;
    8. r.top = this->rect().top()+50;
    9. r.right = this->rect().right();
    10. r.bottom = this->rect().bottom();
    11. int xx = ::DrawText(hdc, L"FOOOOOOOOOO", 5, &r, DT_VCENTER | DT_CENTER | DT_SINGLELINE);
    12. painter.paintEngine()->releaseDC(hdc);
    13. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using StretchDIBits() in QWidget

    Be aware this is not safe without proper preparations. Your paintEvent will be called to draw something to the backing store so you have to prevent that by using PaintOnScreen. And your application will behave slower, by the way but if you really need a DC context then there is no way to avoid that. You can obtain the same effect using Qt only calls without any downsides.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    bear101 (5th May 2009)

Similar Threads

  1. QtScript Binding problem with QWidget
    By zack in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 09:38
  2. Replies: 0
    Last Post: 11th November 2008, 15:36
  3. Dynamic updates of a QWidget in a QScrollArea
    By plamkata in forum Qt Programming
    Replies: 2
    Last Post: 20th July 2008, 23:45
  4. Error in put one QWidget in another QWidget
    By xjtu in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2008, 16:05
  5. Replies: 3
    Last Post: 8th March 2007, 14:54

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.