Skip to content

Instantly share code, notes, and snippets.

@jturcotte
Created January 21, 2014 16:00
Show Gist options
  • Save jturcotte/8542804 to your computer and use it in GitHub Desktop.
Save jturcotte/8542804 to your computer and use it in GitHub Desktop.
Simon's newWindowComponent example
#include <QtQml>
class NewWindowContext : public QQmlContext
{
Q_OBJECT
public:
NewWindowContext(QQmlContext *parentContext)
: QQmlContext(parentContext)
, handle(0)
{}
// Use QExplicitlySharedDataPointer<WebContentsAdapter> in real version :)
void *handle;
};
class WebView : public QObject, public QQmlParserStatus
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
Q_PROPERTY(QQmlComponent* newWindowComponent READ newWindowComponent WRITE setNewWindowComponent)
public:
WebView();
void setNewWindowComponent(QQmlComponent *comp) { m_newWindowComponent = comp; }
QQmlComponent *newWindowComponent() const { return m_newWindowComponent; }
Q_INVOKABLE void createNewWindow();
virtual void classBegin() {}
virtual void componentComplete();
private:
QQmlComponent *m_newWindowComponent;
};
WebView::WebView()
: m_newWindowComponent(0)
{
qDebug() << "WebView::WebView()";
}
void WebView::createNewWindow()
{
Q_ASSERT(m_newWindowComponent);
QQmlContext *parentContext = m_newWindowComponent->creationContext();
if (!parentContext)
parentContext = qmlContext(this);
// ### can this happen??
if (!parentContext) {
QQmlEngine *engine = qmlEngine(this);
Q_ASSERT(engine);
parentContext = engine->rootContext();
}
NewWindowContext *newContext = new NewWindowContext(parentContext);
newContext->handle = this; // Or anything, really :)
m_newWindowComponent->create(newContext);
}
void WebView::componentComplete()
{
QQmlContext *context = qmlContext(this);
void *handle = 0;
while (context) {
if (NewWindowContext *newWindowContext = qobject_cast<NewWindowContext*>(context)) {
qSwap(newWindowContext->handle, handle);
break;
}
context = context->parentContext();
}
if (handle)
qDebug() << "WebView created from another WebView!" << handle;
else
qDebug() << "Standalone WebView complete";
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QQmlEngine engine;
qmlRegisterType<WebView>("TestWebEngine", 1, 0, "WebView");
QQmlComponent doc(&engine);
doc.setData(""
"import QtQuick 2.0\n"
"import TestWebEngine 1.0\n"
"Item {\n"
" Component {\n"
" id: viewComponent\n"
" WebView { }\n"
" }\n"
" Component {\n"
" id: secondWindowComponent\n"
" Loader {\n"
// This doesn't work
" sourceComponent: viewComponent\n"
// But this does, weirdly.
// " WebView { }\n"
" }\n"
" }\n"
"\n"
" property QtObject initialWebView: WebView {\n"
" newWindowComponent: secondWindowComponent\n"
" }\n"
"}", QUrl());
QScopedPointer<QObject> root(doc.create());
if (!root) {
qDebug() << doc.errors().first().toString();
return 1;
}
QObject *initialView = qvariant_cast<QObject*>(root->property("initialWebView"));
QMetaObject::invokeMethod(initialView, "createNewWindow");
return 0;
}
#include "main.moc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment