Getting the render window of an offscreen window
authorPaul Olav Tvete <paul.tvete@digia.com>
Wed, 23 Apr 2014 07:58:32 +0000 (09:58 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 24 Apr 2014 13:36:51 +0000 (15:36 +0200)
When using a QQuickWidget, the QQuickWindow is hidden and the contents
are displayed in another window. Some components need to query the actual
window, e.g. when positioning a popup.

Task-number: QTBUG-38116
Change-Id: I34452be2179ccc9e216e4d89264dc700e0cf42a0
Reviewed-by: Gunnar Sletta <gunnar.sletta@jollamobile.com>
src/quick/items/qquickrendercontrol.cpp
src/quick/items/qquickrendercontrol_p.h
src/quickwidgets/qquickwidget.cpp

index d919ae9..e2a2683 100644 (file)
@@ -245,4 +245,34 @@ QQuickWindow *QQuickRenderControl::window() const
     return d->window;
 }
 
+/*!
+  \fn QWindow *QQuickRenderControl::renderWindow(QPoint *offset)
+
+  Reimplemented in subclasses to return the real window this render control
+  is rendering into.
+
+  If \a offset in non-null, it is set to the offset of the control
+  inside the window.
+*/
+
+/*!
+  Returns the real window that \a win is being rendered to, if any.
+
+  If \a offset in non-null, it is set to the offset of the rendering
+  inside its window.
+
+ */
+
+QWindow *QQuickRenderControl::renderWindowFor(QQuickWindow *win, QPoint *offset)
+{
+    if (!win)
+        return 0;
+    QQuickRenderControl *rc = QQuickWindowPrivate::get(win)->renderControl;
+    if (rc)
+        return rc->renderWindow(offset);
+    return 0;
+}
+
+
+
 QT_END_NAMESPACE
index 98dc946..4cc2cf1 100644 (file)
@@ -63,6 +63,8 @@ public:
     ~QQuickRenderControl();
 
     QQuickWindow *window() const;
+    virtual QWindow *renderWindow(QPoint */*offset*/) { return 0; }
+    static QWindow *renderWindowFor(QQuickWindow *win, QPoint *offset = 0);
 
     void windowDestroyed();
 
index e71da7e..b6d72ac 100644 (file)
@@ -66,10 +66,29 @@ QT_BEGIN_NAMESPACE
 
 extern Q_GUI_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format, bool include_alpha);
 
+class QQuickWidgetRenderControl : public QQuickRenderControl
+{
+public:
+    QQuickWidgetRenderControl(QQuickWidget *quickwidget) : m_quickWidget(quickwidget) {}
+    QWindow *renderWindow(QPoint *offset) {
+        if (offset)
+            *offset = m_quickWidget->mapTo(m_quickWidget->window(), QPoint());
+        return m_quickWidget->window()->windowHandle();
+    }
+private:
+    QQuickWidget *m_quickWidget;
+};
+
 void QQuickWidgetPrivate::init(QQmlEngine* e)
 {
     Q_Q(QQuickWidget);
 
+    renderControl = new QQuickWidgetRenderControl(q);
+    offscreenWindow = new QQuickWindow(renderControl);
+    offscreenWindow->setTitle(QString::fromLatin1("Offscreen"));
+    // Do not call create() on offscreenWindow.
+    createOffscreenSurface();
+
     setRenderToTexture();
     engine = e;
 
@@ -108,11 +127,6 @@ QQuickWidgetPrivate::QQuickWidgetPrivate()
     , updatePending(false)
     , fakeHidden(false)
 {
-    renderControl = new QQuickRenderControl;
-    offscreenWindow = new QQuickWindow(renderControl);
-    offscreenWindow->setTitle(QString::fromLatin1("Offscreen"));
-    // Do not call create() on offscreenWindow.
-    createOffscreenSurface();
 }
 
 QQuickWidgetPrivate::~QQuickWidgetPrivate()