Introducing QWidget::createWindowContainer()

A QWidget that can embed a QWindow. This can be used
to embed a QWindow/QOpenGLContext based window or a
full QQuickView.

Change-Id: I8415b5ae38562fc00b46150fa70b56fd9b19a80c
Reviewed-by: Jørgen Lind <jorgen.lind@gmail.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
Gunnar Sletta 2012-12-03 12:31:18 +01:00 committed by The Qt Project
parent 9a5ab30d52
commit 64134181db
11 changed files with 712 additions and 3 deletions

View File

@ -17,7 +17,8 @@ SUBDIRS = \
statemachine \
tools \
tutorials \
widgets
widgets \
windowcontainer
contains(DEFINES, QT_NO_CURSOR): SUBDIRS -= mainwindows
contains(DEFINES, QT_NO_DRAGANDDROP): SUBDIRS -= draganddrop

View File

@ -0,0 +1,170 @@
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "openglwindow.h"
#include <QPainter>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QFocusEvent>
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QLineEdit>
// Making use of the class from the opengl example in gui.
class Window : public OpenGLWindow
{
Q_OBJECT
public:
Window()
: m_mouseDown(false)
, m_focus(false)
{
}
void render(QPainter *p) {
QLinearGradient g(0, 0, 0, height());
g.setColorAt(0, QColor("lightsteelblue"));
g.setColorAt(1, Qt::black);
p->fillRect(0, 0, width(), height(), g);
p->setPen(Qt::white);
p->drawText(20, 30, QLatin1String("This is an OpenGL based QWindow"));
if (m_key.trimmed().length() > 0) {
QRect bounds = p->boundingRect(QRect(0, 0, width(), height()), Qt::AlignTop | Qt::AlignLeft, m_key);
p->save();
p->translate(width() / 2.0, height() / 2.0);
p->scale(10, 10);
p->translate(-bounds.width() / 2.0, -bounds.height() / 2.0);
p->drawText(bounds, Qt::AlignCenter, m_key);
p->restore();
}
if (m_focus) {
p->drawText(20, height() - 20, QLatin1String("Window has focus!"));
}
p->setRenderHint(QPainter::Antialiasing);
p->drawPolyline(m_polygon);
}
void mousePressEvent(QMouseEvent *e) {
m_mouseDown = true;
m_polygon.clear();
m_polygon.append(e->pos());
renderLater();
}
void mouseMoveEvent(QMouseEvent *e) {
if (m_mouseDown) {
m_polygon.append(e->pos());
renderLater();
}
}
void mouseReleaseEvent(QMouseEvent *e) {
m_mouseDown = false;
m_polygon.append(e->pos());
renderLater();
}
void focusInEvent(QFocusEvent *) {
m_focus = true;
renderLater();
}
void focusOutEvent(QFocusEvent *) {
m_focus = false;
m_polygon.clear();
renderLater();
}
void keyPressEvent(QKeyEvent *e) {
m_key = e->text();
renderLater();
}
void keyReleaseEvent(QKeyEvent *) {
m_key = QString();
renderLater();
}
private:
QPolygon m_polygon;
bool m_mouseDown;
bool m_focus;
QString m_key;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *widget = new QWidget;
QHBoxLayout *layout = new QHBoxLayout(widget);
Window *window = new Window();
QWidget *container = QWidget::createWindowContainer(window);
container->setMinimumSize(300, 300);
container->setMaximumSize(600, 600);
container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
container->setFocusPolicy(Qt::StrongFocus);
window->setGeometry(100, 100, 300, 200);
layout->addWidget(new QLineEdit(QLatin1String("A QLineEdit")));
layout->addWidget(container);
layout->addWidget(new QLineEdit(QLatin1String("A QLabel")));
widget->show();
return app.exec();
}
#include "windowcontainer.moc"

View File

@ -0,0 +1,9 @@
SOURCES = windowcontainer.cpp
QT += widgets
# install
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/windowcontainer
INSTALLS += target
include(../../gui/openglwindow/openglwindow.pri)

View File

@ -34,7 +34,8 @@ HEADERS += \
kernel/qgesturerecognizer.h \
kernel/qgesturemanager_p.h \
kernel/qdesktopwidget_qpa_p.h \
kernel/qwidgetwindow_qpa_p.h
kernel/qwidgetwindow_qpa_p.h \
kernel/qwindowcontainer_p.h
SOURCES += \
kernel/qaction.cpp \
@ -62,7 +63,8 @@ SOURCES += \
kernel/qapplication_qpa.cpp \
kernel/qdesktopwidget_qpa.cpp \
kernel/qwidget_qpa.cpp \
kernel/qwidgetwindow.cpp
kernel/qwidgetwindow.cpp \
kernel/qwindowcontainer.cpp
# TODO

View File

@ -593,6 +593,8 @@ public:
QWindow *windowHandle() const;
static QWidget *createWindowContainer(QWindow *window, QWidget *parent=0, Qt::WindowFlags flags=0);
friend class QDesktopScreenWidget;
Q_SIGNALS:

View File

@ -0,0 +1,218 @@
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qwindowcontainer_p.h"
#include "qwidget_p.h"
#include <QtGui/qwindow.h>
QT_BEGIN_NAMESPACE
class QWindowContainerPrivate : public QWidgetPrivate
{
public:
Q_DECLARE_PUBLIC(QWindowContainer)
QWindowContainerPrivate() : window(0), oldFocusWindow(0) { }
~QWindowContainerPrivate() { }
QPointer<QWindow> window;
QWindow *oldFocusWindow;
};
/*!
\fn QWidget *QWidget::createWindowContainer(QWindow *window, QWidget *parent, Qt::WindowFlags flags);
Creates a QWidget that makes it possible to embed \a window into
a QWidget-based application.
The window container is created as a child of \a parent and with
window flags \a flags.
Once the window has been embedded into the container, the
container will control the window's geometry and
visibility. Explicit calls to QWindow::setGeometry(),
QWindow::show() or QWindow::hide() on an embedded window is not
recommended.
The container takes over ownership of \a window. The window can
be removed from the window container with a call to
QWindow::setParent().
The window container has a number of known limitations:
\list
\li Stacking order; The embedded window will stack on top of the
widget hierarchy as an opaque box. The stacking order of multiple
overlapping window container instances is undefined.
\li Window Handles; The window container will explicitly invoke
winId() which will force the use of native window handles
inside the application. See \l {Native Widgets vs Alien Widgets}
{QWidget documentation} for more details.
\li Rendering Integration; The window container does not interoperate
with QGraphicsProxyWidget, QWidget::render() or similar functionality.
\li Focus Handling; It is possible to let the window container
instance have any focus policy and it will delegate focus to the
window via a call to QWindow::requestActivate(). However,
returning to the normal focus chain from the QWindow instance will
be up to the QWindow instance implementation itself. For instance,
when entering a Qt Quick based window with tab focus, it is quite
likely that further tab presses will only cycle inside the QML
application. Also, whether QWindow::requestActivate() actually
gives the window focus, is platform dependent.
\li Using many window container instances in a QWidget-based
application can greatly hurt the overall performance of the
application.
\endlist
*/
QWidget *QWidget::createWindowContainer(QWindow *window, QWidget *parent, Qt::WindowFlags flags)
{
return new QWindowContainer(window, parent, flags);
}
/*!
\internal
*/
QWindowContainer::QWindowContainer(QWindow *embeddedWindow, QWidget *parent, Qt::WindowFlags flags)
: QWidget(*new QWindowContainerPrivate, parent, flags)
{
Q_D(QWindowContainer);
if (!embeddedWindow) {
qWarning("QWindowContainer: embedded window cannot be null");
return;
}
d->window = embeddedWindow;
// We force this window to become a native window and reparent the
// window directly to it. This is done so that the order in which
// the QWindowContainer is added to a QWidget tree and when it
// gets a window does not matter.
winId();
d->window->setParent(windowHandle());
connect(QGuiApplication::instance(), SIGNAL(focusWindowChanged(QWindow *)), this, SLOT(focusWindowChanged(QWindow *)));
}
/*!
\internal
*/
QWindowContainer::~QWindowContainer()
{
Q_D(QWindowContainer);
delete d->window;
}
/*!
\internal
*/
void QWindowContainer::focusWindowChanged(QWindow *focusWindow)
{
Q_D(QWindowContainer);
d->oldFocusWindow = focusWindow;
}
/*!
\internal
*/
bool QWindowContainer::event(QEvent *e)
{
Q_D(QWindowContainer);
if (!d->window)
return QWidget::event(e);
QEvent::Type type = e->type();
switch (type) {
case QEvent::ChildRemoved: {
QChildEvent *ce = static_cast<QChildEvent *>(e);
if (ce->child() == d->window)
d->window = 0;
break;
}
// The only thing we are interested in is making sure our sizes stay
// in sync, so do a catch-all case.
case QEvent::Resize:
case QEvent::Move:
case QEvent::PolishRequest:
d->window->setGeometry(0, 0, width(), height());
break;
case QEvent::Show:
d->window->show();
break;
case QEvent::Hide:
d->window->hide();
break;
case QEvent::FocusIn:
if (d->oldFocusWindow != d->window) {
d->window->requestActivate();
} else {
QWidget *next = nextInFocusChain();
next->setFocus();
}
break;
default:
break;
}
return QWidget::event(e);
}
QT_END_NAMESPACE

View File

@ -0,0 +1,73 @@
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QWINDOWCONTAINER_H
#define QWINDOWCONTAINER_H
#include <QtWidgets/qwidget.h>
QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
class QWindowContainerPrivate;
class QWindowContainer : public QWidget
{
Q_OBJECT
Q_DECLARE_PRIVATE(QWindowContainer)
public:
explicit QWindowContainer(QWindow *embeddedWindow, QWidget *parent = 0, Qt::WindowFlags f = 0);
~QWindowContainer();
protected:
bool event(QEvent *ev);
private slots:
void focusWindowChanged(QWindow *focusWindow);
};
QT_END_NAMESPACE
QT_END_HEADER
#endif // QWINDOWCONTAINER_H

View File

@ -15,6 +15,7 @@ SUBDIRS=\
qwidgetaction \
qwidgetmetatype \
qwidgetsvariant \
qwindowcontainer \
qshortcut \
qsizepolicy

View File

@ -0,0 +1 @@
tst_qwindowcontainer

View File

@ -0,0 +1,6 @@
CONFIG += testcase
mac:CONFIG -= app_bundle
# CONFIG += parallel_test // Cannot be parallel due to the activation test
TARGET = tst_qwindowcontainer
QT += widgets testlib
SOURCES += tst_qwindowcontainer.cpp

View File

@ -0,0 +1,226 @@
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <qapplication.h>
#include <qwindow.h>
#include <qwidget.h>
class Window : public QWindow
{
public:
Window()
: numberOfExposes(0)
, numberOfObscures(0)
{
}
void exposeEvent(QExposeEvent *) {
if (isExposed())
++numberOfExposes;
else
++numberOfObscures;
}
int numberOfExposes;
int numberOfObscures;
};
class tst_QWindowContainer: public QObject
{
Q_OBJECT
private slots:
void testShow();
void testPositionAndSize();
void testExposeObscure();
void testOwnership();
void testBehindTheScenesDeletion();
void testUnparenting();
void testActivation();
};
void tst_QWindowContainer::testShow()
{
QWidget root;
root.setGeometry(100, 100, 400, 400);
Window *window = new Window();
QWidget *container = QWidget::createWindowContainer(window, &root);
container->setGeometry(50, 50, 200, 200);
root.show();
QVERIFY(QTest::qWaitForWindowExposed(window));
}
void tst_QWindowContainer::testPositionAndSize()
{
QWindow *window = new QWindow();
window->setGeometry(300, 400, 500, 600);
QWidget *container = QWidget::createWindowContainer(window);
container->setGeometry(50, 50, 200, 200);
container->show();
QVERIFY(QTest::qWaitForWindowExposed(container));
QCOMPARE(window->x(), 0);
QCOMPARE(window->y(), 0);
QCOMPARE(window->width(), container->width());
QCOMPARE(window->height(), container->height());
}
void tst_QWindowContainer::testExposeObscure()
{
Window *window = new Window();
QWidget *container = QWidget::createWindowContainer(window);
container->setGeometry(50, 50, 200, 200);
container->show();
QVERIFY(QTest::qWaitForWindowExposed(container));
QVERIFY(QTest::qWaitForWindowExposed(window));
QVERIFY(window->numberOfExposes > 0);
container->hide();
QElapsedTimer timer;
timer.start();
while (window->numberOfObscures == 0 && timer.elapsed() < 5000) {
QTest::qWait(10);
}
QVERIFY(window->numberOfObscures > 0);
}
void tst_QWindowContainer::testOwnership()
{
QPointer<QWindow> window(new QWindow());
QWidget *container = QWidget::createWindowContainer(window);
delete container;
QCOMPARE(window.data(), (QWindow *) 0);
}
void tst_QWindowContainer::testBehindTheScenesDeletion()
{
QWindow *window = new QWindow();
QWidget *container = QWidget::createWindowContainer(window);
delete window;
// The child got removed, showing not should not have any side effects,
// such as for instance, crashing...
container->show();
QVERIFY(QTest::qWaitForWindowExposed(container));
delete container;
}
void tst_QWindowContainer::testActivation()
{
QWidget root;
QWindow *window = new QWindow();
QWidget *container = QWidget::createWindowContainer(window, &root);
container->setGeometry(100, 100, 200, 100);
root.setGeometry(100, 100, 400, 300);
root.show();
root.activateWindow();
QVERIFY(QTest::qWaitForWindowActive(root.windowHandle()));
QVERIFY(QGuiApplication::focusWindow() == root.windowHandle());
// Under KDE (ubuntu 12.10), we experience that doing two activateWindow in a row
// does not work. The second gets ignored by the window manager, even though the
// timestamp in the xcb connection is unique for both.
if (QGuiApplication::platformName() == "xcb")
QTest::qWait(100);
window->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
QVERIFY(QGuiApplication::focusWindow() == window);
}
void tst_QWindowContainer::testUnparenting()
{
QWindow *window = new QWindow();
QWidget *container = QWidget::createWindowContainer(window);
container->setGeometry(100, 100, 200, 100);
window->setParent(0);
container->show();
QVERIFY(QTest::qWaitForWindowExposed(container));
// Window should not be made visible by container..
QVERIFY(!window->isVisible());
}
QTEST_MAIN(tst_QWindowContainer)
#include "tst_qwindowcontainer.moc"