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