[Qt-interest] QMacNativeWidget and QApplication

Rush Manbert rush at manbert.com
Tue Mar 24 23:50:51 CET 2009


As I posted previously, I'm trying to write a Qt-based dylib that I  
can use to open a browser window from a native application. At the  
moment, I'm trying to do this on the Mac.

I read about QMacNativeWidget and used it to write my dylib. I used a  
modified version of the example code provided with the  
QMacNativeWidget documentation to actually create my stand-alone window.

The biggest problem that I encountered was that QMacNativeWidget would  
not construct itself, because I had not previously created a  
QApplication. I hacked around that by instantiating one in my dylib  
code right before I instantiated the QMacNativeWidget. This works in  
the sense that it lets me create the QMacNativeWidget. A window pops  
open and it contains my browser window.

At this point, I have a working demonstration. A native Carbon app  
that creates the Qt-based browser window embedded within a Carbon  
window.

But I am still wondering about this QApplication thing.  I am putting  
the QMacNativeWidget inside a Carbon view, and the Carbon app is  
running the event loop, etc., so why do I need a QApplication at all?  
Have I missed something here? Is hacking it the way I did the expected  
usage?

Here is the code that creates the browser window:

CarbonBrowserWindowImpl::CarbonBrowserWindowImpl ()
{
	// I had to dummy up a QApplication here. Otherwise the  
QMacNativeWidget would assert in its constructor.
	int argc = 0;
	char *argv[] = {0};
	QApplication *pDummyApp = new QApplication (argc, argv);
	(void)pDummyApp;
	
	Rect contentRect;
	SetRect(&contentRect, 100, 100, 900, 700);
	HIWindowRef windowRef;
	CreateNewWindow(kDocumentWindowClass,  
kWindowStandardDocumentAttributes | kWindowCompositingAttribute |  
kWindowStandardHandlerAttribute | kWindowLiveResizeAttribute,  
&contentRect, &windowRef);
	HIViewRef contentView = 0;
	GetRootControl(windowRef, &contentView);
	
	nativeWidget = new QMacNativeWidget();
	nativeWidget->move(0, 0);
	nativeWidget->setPalette(QPalette(Qt::red));
	nativeWidget->setAutoFillBackground(true);
	QGridLayout *layout = new QGridLayout();
	// My QWebKit-based Browser window. Pointer is called pushButton so I  
could
	// initially use the example code with no modifications.
	BrowserWindow *pushButton = new BrowserWindow(nativeWidget);
	pushButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // Don't use  
the layout rect calculated from QMacStyle.
	layout->setContentsMargins (0, 0, 0, 0);
	layout->addWidget(pushButton);
	nativeWidget->setLayout(layout);
	nativeWidget->adjustSize();
	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();

	// Resize/unresize the window to force the contents to be drawn.
	// Otherwise the window was displayed but blank, until you resized it  
with the mouse.
	Rect windowRect;
	GetWindowBounds (windowRef, kWindowStructureRgn, &windowRect);
	windowRect.top = windowRect.top + 5;
	SetWindowBounds (windowRef, kWindowStructureRgn, &windowRect);
	windowRect.top = windowRect.top - 5;
	SetWindowBounds (windowRef, kWindowStructureRgn, &windowRect);

	// Show the window.
	ShowWindow(windowRef);
	m_windowRef =  windowRef;
}

If one of you Nokia guys who knows all about this stuff wants to  
enlighten me, I'm all ears. :-)

Thanks,
Rush





More information about the Qt-interest-old mailing list