Results 1 to 3 of 3

Thread: Qt 5.3.2 / OSX cannot use native CGContext drawing in paintEvent / Flickering

  1. #1
    Join Date
    Jun 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Qt 5.3.2 / OSX cannot use native CGContext drawing in paintEvent / Flickering

    Hi !
    For the life of me i cannot use native drawing in a paintEvent on Qt 5.

    I created a simple example that should fill the whole Rectangle with a certain color (RGB 240,120,60).
    When started (on Mac of course, i used 10.9) the WIndow flickers some times between white and the desired “orange” color and then stays white.
    Do you have any idea why my drawing is not persistent ?
    A very similar way of drawing in Qt4 is working just fine.

    Here is my sample:

    nativepainter.pro:
    Qt Code:
    1. QT += core gui macextras
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. TARGET = nativePainter
    6. TEMPLATE = app
    7. LIBS += -framework CoreGraphics
    8.  
    9. SOURCES += main.cpp\
    10. nativepainter.cpp
    11.  
    12. HEADERS += \
    13. nativepainter.h
    To copy to clipboard, switch view to plain text mode 


    main.cpp:

    Qt Code:
    1. #include <QApplication>
    2. #include <nativepainter.h>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. nativePainter myNP;
    8. myNP.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    nativepainter.h:

    Qt Code:
    1. #ifndef NATIVEPAINTER_H
    2. #define NATIVEPAINTER_H
    3.  
    4. #include <QApplication>
    5. #include <QWidget>
    6. #include <QPaintEvent>
    7. #include <CoreGraphics/CGColorSpace.h>
    8. #include <ApplicationServices/ApplicationServices.h>
    9.  
    10. class nativePainter : public QWidget
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit nativePainter(QWidget *parent = 0);
    15. signals:
    16.  
    17. public slots:
    18. protected:
    19. void paintEvent(QPaintEvent * event);
    20. private:
    21. };
    22.  
    23. #endif // NATIVEPAINTER_H
    To copy to clipboard, switch view to plain text mode 
    nativepainter.cpp:

    Qt Code:
    1. #include <Qpainter>
    2. #include <QDebug>
    3. #include "nativepainter.h"
    4. #ifdef Q_OS_MACX
    5. #include <QtMac>
    6. #endif
    7.  
    8. nativePainter::nativePainter(QWidget *parent) :
    9. QWidget(parent)
    10. {
    11.  
    12. }
    13.  
    14. void nativePainter::paintEvent(QPaintEvent* e)
    15. {
    16. #ifdef Q_OS_MACX
    17. //prepare painting
    18. QPainter painter;
    19. painter.begin(this);
    20. painter.beginNativePainting();
    21.  
    22. //create pixelbuffer and fill with RGB color (240,120,60)
    23. int componentCount(4), w(e->rect().width()), h(e->rect().height());
    24. size_t bufferLength = w * h * componentCount*2;
    25. unsigned char* pixels = (unsigned char*)malloc(bufferLength);
    26. for(ulong i=0; i < bufferLength; i+=componentCount)
    27. {
    28. pixels[i] = 240;
    29. pixels[i+1] = 120;
    30. pixels[i+2] = 60;
    31. pixels[i+3] = 255;
    32. }
    33. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, bufferLength, NULL);
    34. size_t bitsPerComponent = 8;
    35. size_t bitsPerPixel = bitsPerComponent * componentCount;
    36. size_t bytesPerRow = componentCount * w;
    37. CGColorSpaceRef RGBcolorSpaceRef = CGColorSpaceCreateDeviceRGB();
    38. CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
    39. CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    40.  
    41. //create CGImageRef with pixels
    42. CGImageRef iref = CGImageCreate(
    43. w,
    44. h,
    45. bitsPerComponent,
    46. bitsPerPixel,
    47. bytesPerRow,
    48. RGBcolorSpaceRef,
    49. bitmapInfo,
    50. provider, // data provider
    51. NULL, // decode
    52. true, // should interpolate
    53. renderingIntent);
    54.  
    55. //draw CGImage to current context
    56. CGRect cgrect = CGRectMake(e->rect().x(), e->rect().y(), e->rect().x() + e->rect().width(), e->rect().y() + e->rect().height());
    57. CGContextRef context = QtMac::currentCGContext();
    58. CGContextDrawImage(context, cgrect, iref);
    59. CGContextFlush(context);
    60. CGContextSynchronize(context);
    61. //end painting
    62. painter.endNativePainting();
    63. painter.end();
    64. #else
    65. QPainter painter;
    66. painter.begin(this);
    67. painter.fillRect(e->rect(), QColor(240, 120, 60));
    68. painter.end();
    69. #endif
    70. }
    To copy to clipboard, switch view to plain text mode 
    May try to compile and see for yourself, do you guys have any idea ?

    Greetings & Thanks,

    Nils

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.3.2 / OSX cannot use native CGContext drawing in paintEvent / Flickering

    Maybe using widget attributes Qt::WA_OpaquePaintEvent or Qt::WA_NoSystemBackground could help?

    Cheers,
    _

  3. #3
    Join Date
    Jun 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Qt 5.3.2 / OSX cannot use native CGContext drawing in paintEvent / Flickering

    Hello anda_skoa,
    damn, that sounded just like it, BUT:
    Using either of your proposed attributes or both ends up in the same kind of flickering only with black as the end state instead of "regular windows-backround-color". Same happened in one of my experiments before when i set the background color to transparent(via pallette entry):
    Argh, i am frustrated :-(

    Thanks though !

    Nils

Similar Threads

  1. QGraphicsItem flickering
    By michalk in forum Newbie
    Replies: 4
    Last Post: 17th May 2011, 13:05
  2. Flickering when drawing Qt widgets over an OpenGL window
    By ksierens in forum Qt Programming
    Replies: 0
    Last Post: 31st May 2010, 14:31
  3. Flickering
    By Pembar in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2009, 19:21
  4. Problems with drawing image in paintEvent of QTreeView
    By Tito Serenti in forum Qt Programming
    Replies: 7
    Last Post: 24th December 2008, 23:25
  5. Replies: 2
    Last Post: 26th April 2006, 10:43

Tags for this Thread

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.