QMacCocoaViewContainer Class

The QMacCocoaViewContainer class provides a widget for macOS that can be used to wrap arbitrary Cocoa views (i.e., NSView subclasses) and insert them into Qt hierarchies. More...

Header: #include <QMacCocoaViewContainer>
qmake: QT += widgets
Since: Qt 4.5
Inherits: QWidget

Public Functions

QMacCocoaViewContainer(NSView *cocoaViewToWrap, QWidget *parent = Q_NULLPTR)
virtual ~QMacCocoaViewContainer()
NSView *cocoaView() const
void setCocoaView(NSView *view)
  • 211 public functions inherited from QWidget
  • 31 public functions inherited from QObject
  • 14 public functions inherited from QPaintDevice

Additional Inherited Members

  • 58 properties inherited from QWidget
  • 1 property inherited from QObject
  • 19 public slots inherited from QWidget
  • 1 public slot inherited from QObject
  • 3 signals inherited from QWidget
  • 2 signals inherited from QObject
  • 1 public variable inherited from QObject
  • 5 static public members inherited from QWidget
  • 10 static public members inherited from QObject
  • 35 protected functions inherited from QWidget
  • 9 protected functions inherited from QObject
  • 1 protected function inherited from QPaintDevice
  • 1 protected slot inherited from QWidget
  • 2 protected variables inherited from QObject
  • 1 protected type inherited from QPaintDevice

Detailed Description

The QMacCocoaViewContainer class provides a widget for macOS that can be used to wrap arbitrary Cocoa views (i.e., NSView subclasses) and insert them into Qt hierarchies.

While Qt offers a lot of classes for writing your application, Apple's Cocoa framework offers lots of functionality that is not currently in Qt or may never end up in Qt. Using QMacCocoaViewContainer, it is possible to put an arbitrary NSView-derived class from Cocoa and put it in a Qt hierarchy. Depending on how comfortable you are with using objective-C, you can use QMacCocoaViewContainer directly, or subclass it to wrap further functionality of the underlying NSView.

QMacCocoaViewContainer works regardless if Qt is built against Carbon or Cocoa. However, QCocoaContainerView requires macOS 10.5 or better to be used with Carbon.

It should be also noted that at the low level on macOS, there is a difference between windows (top-levels) and view (widgets that are inside a window). For this reason, make sure that the NSView that you are wrapping doesn't end up as a top-level. The best way to ensure this is to make sure you always have a parent and not set the parent to 0.

If you are using QMacCocoaViewContainer as a sub-class and are mixing and matching objective-C with C++ (a.k.a. objective-C++). It is probably simpler to have your file end with .mm than .cpp. Most Apple tools will correctly identify the source as objective-C++.

QMacCocoaViewContainer requires knowledge of how Cocoa works, especially in regard to its reference counting (retain/release) nature. It is noted in the functions below if there is any change in the reference count. Cocoa views often generate temporary objects that are released by an autorelease pool. If this is done outside of a running event loop, it is up to the developer to provide the autorelease pool.

The following is a snippet of subclassing QMacCocoaViewContainer to wrap a NSSearchField.


  SearchWidget::SearchWidget(QWidget *parent)
      : QMacCocoaViewContainer(0, parent)
  {
      // Many Cocoa objects create temporary autorelease objects,
      // so create a pool to catch them.
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

      // Create the NSSearchField, set it on the QCocoaViewContainer.
      NSSearchField *search = [[NSSearchField alloc] init];
      setCocoaView(search);

      // Use a Qt menu for the search field menu.
      QMenu *qtMenu = createMenu(this);
      NSMenu *nsMenu = qtMenu->macMenu(0);
      [[search cell] setSearchMenuTemplate:nsMenu];

      // Release our reference, since our super class takes ownership and we
      // don't need it anymore.
      [search release];

      // Clean up our pool as we no longer need it.
      [pool release];
  }

Member Function Documentation

QMacCocoaViewContainer::QMacCocoaViewContainer(NSView *cocoaViewToWrap, QWidget *parent = Q_NULLPTR)

Create a new QMacCocoaViewContainer using the NSView pointer in cocoaViewToWrap with parent, parent. QMacCocoaViewContainer will retain cocoaViewToWrap.

[virtual] QMacCocoaViewContainer::~QMacCocoaViewContainer()

Destroy the QMacCocoaViewContainer and release the wrapped view.

NSView *QMacCocoaViewContainer::cocoaView() const

Returns the NSView that has been set on this container.

See also setCocoaView().

void QMacCocoaViewContainer::setCocoaView(NSView *view)

Sets view as the NSView to contain and retains it. If this container already had a view set, it will release the previously set view.

See also cocoaView().