Qt 5.3.2 / OSX不能在paintEvent / Flickering中使用本机CGContext绘图

时间:2014-11-17 21:41:15

标签: macos qt qt5 cgcontext paintevent

对于我的生活,我不能在Qt 5上的paintEvent中使用原生绘图。

我创建了一个简单的例子,它应该用一定的颜色(RGB 240,120,60)填充整个Rectangle。 当开始时(在Mac上当然,我使用10.9),WIndow在白色和所需的“橙色”颜色之间闪烁一些,然后保持白色。 你知道为什么我的画不持久吗? 在Qt4中绘制的一种非常类似的方法正常工作。

这是我的样本:

nativepainter.pro:

QT       += core gui macextras

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = nativePainter
TEMPLATE = app
LIBS += -framework CoreGraphics

SOURCES += main.cpp\
    nativepainter.cpp

HEADERS  += \
    nativepainter.h

main.cpp中:

#include <QApplication>
#include <nativepainter.h>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    nativePainter myNP;
    myNP.show();

    return a.exec();
}

nativepainter.h:

#ifndef NATIVEPAINTER_H
#define NATIVEPAINTER_H

#include <QApplication>
#include <QWidget>
#include <QPaintEvent>
#include <CoreGraphics/CGColorSpace.h>
#include <ApplicationServices/ApplicationServices.h>

class nativePainter : public QWidget
{
    Q_OBJECT
public:
    explicit nativePainter(QWidget *parent = 0);
signals:

public slots:
protected:
    void paintEvent(QPaintEvent * event);
private:
};

#endif // NATIVEPAINTER_H

nativepainter.cpp:

#include <Qpainter>
#include <QDebug>
#include "nativepainter.h"
#ifdef Q_OS_MACX
#include <QtMac>
#endif

nativePainter::nativePainter(QWidget *parent) :
    QWidget(parent)
{

}

void nativePainter::paintEvent(QPaintEvent* e)
{
#ifdef Q_OS_MACX
//prepare painting
    QPainter painter;
    painter.begin(this);
    painter.beginNativePainting();

//create pixelbuffer and fill with RGB color (240,120,60)
    int componentCount(4), w(e->rect().width()), h(e->rect().height());
    size_t bufferLength = w * h * componentCount*2;
    unsigned char* pixels = (unsigned char*)malloc(bufferLength);
    for(ulong i=0; i < bufferLength; i+=componentCount)
    {
        pixels[i] = 240;
        pixels[i+1] = 120;
        pixels[i+2] = 60;
        pixels[i+3] = 255;
    }
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, bufferLength, NULL);
    size_t bitsPerComponent = 8;
    size_t bitsPerPixel = bitsPerComponent * componentCount;
    size_t bytesPerRow = componentCount * w;
    CGColorSpaceRef RGBcolorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

//create CGImageRef with pixels
    CGImageRef iref = CGImageCreate(
      w,
      h,
      bitsPerComponent,
      bitsPerPixel,
      bytesPerRow,
      RGBcolorSpaceRef,
      bitmapInfo,
      provider,   // data provider
      NULL,       // decode
      true,        // should interpolate
      renderingIntent);

//draw CGImage to current context
    CGRect cgrect = CGRectMake(e->rect().x(), e->rect().y(), e->rect().x() + e->rect().width(), e->rect().y() + e->rect().height());
    CGContextRef context = QtMac::currentCGContext();
    CGContextDrawImage(context, cgrect, iref);
    CGContextFlush(context);
    CGContextSynchronize(context);
//end painting
    painter.endNativePainting();
    painter.end();
#else
    QPainter painter;
    painter.begin(this);
    painter.fillRect(e->rect(), QColor(240, 120, 60));
    painter.end();
#endif
}

可以尝试编译并亲眼看看,你们有什么想法吗?

问候&amp;谢谢,

尼尔斯

0 个答案:

没有答案