QMacNativeWidget Class Reference |
QMacNativeWidget ( void * parentView = 0 ) | |
~QMacNativeWidget () |
virtual QSize | sizeHint () const |
virtual bool | event ( QEvent * ev ) |
The QMacNativeWidget class provides a widget for Mac OS X that provides a way to put Qt widgets into Carbon or Cocoa hierarchies depending on how Qt was configured.
On Mac OS X, there is a difference between a window and view; normally expressed as widgets in Qt. Qt makes assumptions about its parent-child hierarchy that make it complex to put an arbitrary Qt widget into a hierarchy of "normal" views from Apple frameworks. QMacNativeWidget bridges the gap between views and windows and makes it possible to put a hierarchy of Qt widgets into a non-Qt window or view.
QMacNativeWidget pretends it is a window (i.e. isWindow() will return true), but it cannot be shown on its own. It needs to be put into a window when it is created or later through a native call.
QMacNativeWidget works for either Carbon or Cocoa depending on how Qt was configured. If Qt is using Carbon, QMacNativeWidget will embed into Carbon hierarchies. If Qt is using Cocoa, QMacNativeWidget embeds into Cocoa hierarchies.
Here is an example of putting a QPushButton into a NSWindow:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(200, app.desktop()->height() - 200, 239, 200) styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO]; QMacNativeWidget *nativeWidget = new QMacNativeWidget(); nativeWidget->move(0, 0); nativeWidget->setPalette(QPalette(Qt::red)); nativeWidget->setAutoFillBackground(true); QVBoxLayout *layout = new QVBoxLayout(); QPushButton *pushButton = new QPushButton("An Embedded Qt Button!", nativeWidget); pushButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // Don't use the layout rect calculated from QMacStyle. layout->addWidget(pushButton); nativeWidget->setLayout(layout); // Adjust Cocoa layouts NSView *nativeWidgetView = reinterpret_cast<NSView *>(nativeWidget->winId()); NSView *contentView = [window contentView]; [contentView setAutoresizesSubviews:YES]; [nativeWidgetView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; [nativeWidgetView setAutoresizesSubviews:YES]; NSView *pushButtonView = reinterpret_cast<NSView *>(pushButton->winId()); [pushButtonView setAutoresizingMask:NSViewWidthSizable]; // Add the nativeWidget to the window. [contentView addSubview:nativeWidgetView positioned:NSWindowAbove relativeTo:nil]; nativeWidget->show(); pushButton->show(); // Show the window. [window makeKeyAndOrderFront:window]; [pool release];
On Carbon, this would do the equivalent:
Rect contentRect; SetRect(&contentRect, 200, 200, 400, 400); HIWindowRef windowRef; CreateNewWindow(kDocumentWindowClass, kWindowStandardDocumentAttributes | kWindowCompositingAttribute | kWindowStandardHandlerAttribute | kWindowLiveResizeAttribute, &contentRect, &windowRef); HIViewRef contentView = 0; GetRootControl(windowRef, &contentView); QMacNativeWidget *nativeWidget = new QMacNativeWidget(); nativeWidget->move(0, 0); nativeWidget->setPalette(QPalette(Qt::red)); nativeWidget->setAutoFillBackground(true); QVBoxLayout *layout = new QVBoxLayout(); QPushButton *pushButton = new QPushButton("An Embedded Qt Button!", nativeWidget); pushButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // Don't use the layout rect calculated from QMacStyle. layout->addWidget(pushButton); nativeWidget->setLayout(layout); HIViewRef nativeWidgetView = reinterpret_cast<HIViewRef>(nativeWidget->winId()); // Add the nativeWidget to the window. HIViewAddSubview(contentView, nativeWidgetView); // Adjust Carbon layouts HILayoutInfo layoutInfo; layoutInfo.version = kHILayoutInfoVersionZero; HIViewGetLayoutInfo(nativeWidgetView, &layoutInfo); layoutInfo.binding.top.toView = contentView; layoutInfo.binding.top.kind = kHILayoutBindTop; layoutInfo.binding.left.toView = contentView; layoutInfo.binding.left.kind = kHILayoutBindLeft; layoutInfo.binding.right.toView = contentView; layoutInfo.binding.right.kind = kHILayoutBindRight; layoutInfo.binding.bottom.toView = contentView; layoutInfo.binding.bottom.kind = kHILayoutBindBottom; HIViewSetLayoutInfo(nativeWidgetView, &layoutInfo); HIViewApplyLayout(nativeWidgetView); pushButton->show(); nativeWidget->show(); // Show the window. ShowWindow(windowRef);
Note that QMacNativeWidget requires knowledge of Carbon or Cocoa. All it does is get the Qt hierarchy into a window not owned by Qt. It is then up to the programmer to ensure it is placed correctly in the window and responds correctly to events.
Create a QMacNativeWidget with parentView as its "superview" (i.e., parent). The parentView is either an HIViewRef if Qt is using Carbon or a NSView pointer if Qt is using Cocoa.
Destroy the QMacNativeWidget.
Reimplemented from QObject::event().
Reimplemented from QWidget::sizeHint().
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 4.6 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com