[Interest] Qt5: Converting from a QPixmap to an NSImage on OSX

Colin Dean cad at cad.cx
Sat Nov 3 07:31:16 CET 2012

Hello, I'm quite new to Qt development and my C++ and Obj-C skills are both long-time neophyte. Perfect conditions for learning, eh? 

I'm trying to upgrade a project from Qt 4.x to 5.x. I've worked through some of the issues here and there, but I'm finding one that I can't seem to shake.

I have this method in a Obj-C class:

void MacDockIconHandler::setIcon(const QIcon &icon) 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSImage *image;
if (icon.isNull())
image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
else {
QSize size = icon.actualSize(QSize(128, 128));
QPixmap pixmap = icon.pixmap(size);
CGImageRef cgImage = pixmap.toMacCGImageRef();//compile errors here
image = [[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize];

[NSApp setApplicationIconImage:image]; 
[image release];
[pool release];

When I compile against Qt 5.0.0-beta2, it errors at the line marked above. Looking through the Qt5 documentation, it appears that QPixmap::toMacCGImageRef() no longer exists. I looked through the documentation and verified its presence in Qt4 [1] and its absence in 5.0 [2].

As you can see, the code snippet here needs to take a QIcon and make it into an NSImage. I can obviously transform the QIcon to a QPixmap, but I'm not sure where to go from there. The QPixmap can be transformed to a QImage, but there doesn't seem to be anywhere to go from there.

Is there another recommended way to accomplish this? Could these CGImageRef methods have been erroneously removed? or perhaps simply moved to another class?

Any assistance welcome.

[1]: https://qt-project.org/doc/qt-4.8/QPixmap.html
[2]: https://qt-project.org/doc/qt-5.0/QPixmap.html

Colin Dean
cad at cad.cx

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20121103/5a11168c/attachment.html>

More information about the Interest mailing list