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. 更多...
头文件: | #include <QMacCocoaViewContainer> |
qmake: | QT += widgets |
开始支持版本: | Qt 4.5 |
基类: | QWidget |
公有函数
QMacCocoaViewContainer(NSView *cocoaViewToWrap, QWidget *parent = Q_NULLPTR) | |
virtual | ~QMacCocoaViewContainer() |
NSView * | cocoaView() const |
void | setCocoaView(NSView *view) |
- 214 个公有函数继承自 QWidget
- 32 个公有函数继承自 QObject
- 14 个公有函数继承自 QPaintDevice
其他继承的成员
- 59 个属性继承自 QWidget
- 1 个属性继承自 QObject
- 19 个公有槽函数继承自 QWidget
- 1 个公有槽函数继承自 QObject
- 3 个信号继承自 QWidget
- 2 个信号继承自 QObject
- 1 个公有变量继承自 QObject
- 5 个静态公有成员继承自 QWidget
- 10 个静态公有成员继承自 QObject
- 35 个受保护的函数继承自 QWidget
- 9 个受保护的函数继承自 QObject
- 1 个受保护的函数继承自 QPaintDevice
- 1 个受保护的槽函数继承自 QWidget
- 2 个受保护的变量继承自 QObject
- 1 protected type inherited from QPaintDevice
详细描述
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 frameworks offer functionality that is not currently available (or may never end up) in Qt. Using QMacCocoaViewContainer, it is possible to take an arbitrary NSView-derived class from Cocoa and put it in a Qt widgets hierarchy. Depending on the level of integration you need, you can use QMacCocoaViewContainer directly or subclass it to wrap more functionality of the underlying NSView.
It should be also noted that, at the Cocoa level, there is a difference between top-level windows and views (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 window. The best way to ensure this is to make sure QMacCocoaViewContainer's parent widget is not null.
If you are using QMacCocoaViewContainer as a subclass and are accessing Cocoa API, it is probably simpler to have your file end with .mm
instead of .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 showing how to subclass QMacCocoaViewContainer to wrap an 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]; }
成员函数
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.
参见 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.
参见 cocoaView().