0

对于我的一生,我不能在 Qt 5 的paintEvent 中使用本机绘图。

我创建了一个简单的例子,应该用某种颜色(RGB 240,120,60)填充整个矩形。启动时(当然是在 Mac 上,我使用的是 10.9),窗口会在白色和所需的“橙色”颜色之间闪烁几次,然后保持白色。你知道为什么我的画不持久吗?在 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

主.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
}

可以自己编译看看,你们有什么想法吗?

问候和感谢,

尼尔斯

4

0 回答 0