2011-04-27 12:13:26 +00:00
/****************************************************************************
* *
2016-01-19 11:23:05 +00:00
* * Copyright ( C ) 2016 The Qt Company Ltd .
* * Contact : https : //www.qt.io/licensing/
2011-04-27 12:13:26 +00:00
* *
* * This file is part of the test suite of the Qt Toolkit .
* *
2016-01-19 11:23:05 +00:00
* * $ QT_BEGIN_LICENSE : GPL - EXCEPT $
2012-09-20 05:21:40 +00:00
* * 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
2015-01-28 11:55:39 +00:00
* * a written agreement between you and The Qt Company . For licensing terms
2016-01-19 11:23:05 +00:00
* * and conditions see https : //www.qt.io/terms-conditions. For further
* * information use the contact form at https : //www.qt.io/contact-us.
2012-09-20 05:21:40 +00:00
* *
2016-01-19 11:23:05 +00:00
* * GNU General Public License Usage
* * Alternatively , this file may be used under the terms of the GNU
* * General Public License version 3 as published by the Free Software
* * Foundation with exceptions as appearing in the file LICENSE . GPL3 - EXCEPT
* * included in the packaging of this file . Please review the following
* * information to ensure the GNU General Public License requirements will
* * be met : https : //www.gnu.org/licenses/gpl-3.0.html.
2011-04-27 12:13:26 +00:00
* *
* * $ QT_END_LICENSE $
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <qtest.h>
# include <QtTest/QSignalSpy>
2012-02-16 04:43:03 +00:00
# include <QtQml/qqmlengine.h>
# include <QtQml/qqmlcomponent.h>
# include <QtQml/qqmlcontext.h>
2014-05-10 10:12:18 +00:00
# include <QtQuick/qquickitemgrabresult.h>
Say hello to QtQuick module
This change moves the QtQuick 2 types and C++ API (including
SceneGraph) to a new module (AKA library), QtQuick.
99% of this change is moving files from src/declarative to
src/quick, and from tests/auto/declarative to
tests/auto/qtquick2.
The loading of QtQuick 2 ("import QtQuick 2.0") is now delegated to
a plugin, src/imports/qtquick2, just like it's done for QtQuick 1.
All tools, examples, and tests that use QtQuick C++ API have gotten
"QT += quick" or "QT += quick-private" added to their .pro file.
A few additional internal QtDeclarative classes had to be exported
(via Q_DECLARATIVE_PRIVATE_EXPORT) since they're needed by the
QtQuick 2 implementation.
The old header locations (e.g. QtDeclarative/qquickitem.h) will
still be supported for some time, but will produce compile-time
warnings. (To avoid the QtQuick implementation using the
compatibility headers (since QtDeclarative's includepath comes
first), a few include statements were modified, e.g. from
"#include <qsgnode.h>" to "#include <QtQuick/qsgnode.h>".)
There's a change in qtbase that automatically adds QtQuick to the
module list if QtDeclarative is used. Together with the compatibility
headers, this should help reduce the migration pain for existing
projects.
In theory, simply getting an existing QtDeclarative-based project
to compile and link shouldn't require any changes for now -- but
porting to the new scheme is of course recommended, and will
eventually become mandatory.
Task-number: QTBUG-22889
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
Change-Id: Ia52be9373172ba2f37e7623231ecb060316c96a7
Reviewed-by: Kent Hansen <kent.hansen@nokia.com>
Reviewed-by: Sergio Ahumada <sergio.ahumada@nokia.com>
2011-11-23 14:14:07 +00:00
# include <QtQuick/qquickview.h>
2012-02-15 03:20:56 +00:00
# include <QtGui/private/qinputmethod_p.h>
2021-04-16 12:16:37 +00:00
# include <QtQuick/private/qquickloader_p.h>
2021-06-11 12:19:17 +00:00
# include <QtQuick/private/qquickpalette_p.h>
Say hello to QtQuick module
This change moves the QtQuick 2 types and C++ API (including
SceneGraph) to a new module (AKA library), QtQuick.
99% of this change is moving files from src/declarative to
src/quick, and from tests/auto/declarative to
tests/auto/qtquick2.
The loading of QtQuick 2 ("import QtQuick 2.0") is now delegated to
a plugin, src/imports/qtquick2, just like it's done for QtQuick 1.
All tools, examples, and tests that use QtQuick C++ API have gotten
"QT += quick" or "QT += quick-private" added to their .pro file.
A few additional internal QtDeclarative classes had to be exported
(via Q_DECLARATIVE_PRIVATE_EXPORT) since they're needed by the
QtQuick 2 implementation.
The old header locations (e.g. QtDeclarative/qquickitem.h) will
still be supported for some time, but will produce compile-time
warnings. (To avoid the QtQuick implementation using the
compatibility headers (since QtDeclarative's includepath comes
first), a few include statements were modified, e.g. from
"#include <qsgnode.h>" to "#include <QtQuick/qsgnode.h>".)
There's a change in qtbase that automatically adds QtQuick to the
module list if QtDeclarative is used. Together with the compatibility
headers, this should help reduce the migration pain for existing
projects.
In theory, simply getting an existing QtDeclarative-based project
to compile and link shouldn't require any changes for now -- but
porting to the new scheme is of course recommended, and will
eventually become mandatory.
Task-number: QTBUG-22889
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
Change-Id: Ia52be9373172ba2f37e7623231ecb060316c96a7
Reviewed-by: Kent Hansen <kent.hansen@nokia.com>
Reviewed-by: Sergio Ahumada <sergio.ahumada@nokia.com>
2011-11-23 14:14:07 +00:00
# include <QtQuick/private/qquickrectangle_p.h>
2012-02-15 03:20:56 +00:00
# include <QtQuick/private/qquicktextinput_p.h>
2016-05-19 11:10:19 +00:00
# include <QtQuick/private/qquickitemchangelistener_p.h>
2015-02-04 09:10:12 +00:00
# include <QtGui/qstylehints.h>
2011-10-14 08:51:42 +00:00
# include <private/qquickitem_p.h>
Say hello to QtQuick module
This change moves the QtQuick 2 types and C++ API (including
SceneGraph) to a new module (AKA library), QtQuick.
99% of this change is moving files from src/declarative to
src/quick, and from tests/auto/declarative to
tests/auto/qtquick2.
The loading of QtQuick 2 ("import QtQuick 2.0") is now delegated to
a plugin, src/imports/qtquick2, just like it's done for QtQuick 1.
All tools, examples, and tests that use QtQuick C++ API have gotten
"QT += quick" or "QT += quick-private" added to their .pro file.
A few additional internal QtDeclarative classes had to be exported
(via Q_DECLARATIVE_PRIVATE_EXPORT) since they're needed by the
QtQuick 2 implementation.
The old header locations (e.g. QtDeclarative/qquickitem.h) will
still be supported for some time, but will produce compile-time
warnings. (To avoid the QtQuick implementation using the
compatibility headers (since QtDeclarative's includepath comes
first), a few include statements were modified, e.g. from
"#include <qsgnode.h>" to "#include <QtQuick/qsgnode.h>".)
There's a change in qtbase that automatically adds QtQuick to the
module list if QtDeclarative is used. Together with the compatibility
headers, this should help reduce the migration pain for existing
projects.
In theory, simply getting an existing QtDeclarative-based project
to compile and link shouldn't require any changes for now -- but
porting to the new scheme is of course recommended, and will
eventually become mandatory.
Task-number: QTBUG-22889
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
Change-Id: Ia52be9373172ba2f37e7623231ecb060316c96a7
Reviewed-by: Kent Hansen <kent.hansen@nokia.com>
Reviewed-by: Sergio Ahumada <sergio.ahumada@nokia.com>
2011-11-23 14:14:07 +00:00
# include "../../shared/util.h"
2012-01-31 07:15:00 +00:00
# include "../shared/visualtestutil.h"
2012-02-15 03:20:56 +00:00
# include "../../shared/platforminputcontext.h"
QQuickItem: Make x/y/width/height bindable
This ports the four properties properties to Q_OBJECT_COMPAT_PROPERTY.
It is not possible to use Q_OBJECT_BINDABLE_PROPERTY, not even for the
simpler x and y properties, as QQuickItem calls the virtual
geometryChange method in the setters, making them non-trivial.
For width/height, we use the new property system to improve the
width/heightValid check: We return not only valid if the property has
explictily been set (either via a direct setter call, or because the
setter has been called when a binding evaluated), but also when a
binding is set. This matches the fact that implicitWidth/Height should
only be used when width/height is not specified. In theory, this could
help in cases where one sets both implicit and explicit properties for
Text, skipping one initial layout calculation.
As the setters now remove the binding (this aligns with the behavior of
writing through the meta-object system), a few test cases had to be
adjusted, which relied on the setter not removing it.
Further test changes were necessary because the location of a few
warnings changed, due to differences in binding order evaluation and
error reporting.
[ChangeLog][QtQuick][QQuickItem] The x, y, width and height properties
of QQuickItem are now bindable. This enables modifying their bindings
from C++. One could for instance swap the width of two items,
preserving the bindings:
// QML
Item {
Rectangle { id: a; width: parent.width / 2 }
Rectangle { id: b; width: parent.width / 3; anchors.right: a.left }
}
// C++
auto rootCtxt = engine.rootContext();
auto a = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"a"_qs));
auto b = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"b"_qs));
auto aBinding = a->bindableWidth().takeBinding();
auto bBinding = b->bindableWidth().takeBinding();
a->bindableWidth()->setBinding(bBinding);
b->bindableWidth()->setBinding(aBinding);
Afterwards, if the root item gets resized, the width of the rectangles
will still be adjusted.
[ChangeLog][QtQuick][QQuickItem][Important Behavior Changes] Calling
the setWidth, setHeight, setX and setY properties now removes any
existing binding from the corresponding property.
Change-Id: I5e1553611cb92b033247ada715cea48c962395bc
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-09-24 09:06:06 +00:00
# include <QtTest/private/qpropertytesthelper_p.h>
2012-01-31 07:15:00 +00:00
using namespace QQuickVisualTestUtil ;
2011-04-27 12:13:26 +00:00
2012-02-16 04:43:03 +00:00
class tst_QQuickItem : public QQmlDataTest
2011-04-27 12:13:26 +00:00
{
Q_OBJECT
public :
2011-10-14 08:51:42 +00:00
tst_QQuickItem ( ) ;
2011-04-27 12:13:26 +00:00
private slots :
2021-02-23 08:46:23 +00:00
void initTestCase ( ) override ;
2012-02-15 03:20:56 +00:00
void cleanup ( ) ;
2013-02-22 12:44:06 +00:00
void activeFocusOnTab ( ) ;
void activeFocusOnTab2 ( ) ;
void activeFocusOnTab3 ( ) ;
2013-04-09 10:09:24 +00:00
void activeFocusOnTab4 ( ) ;
void activeFocusOnTab5 ( ) ;
2013-05-10 09:54:37 +00:00
void activeFocusOnTab6 ( ) ;
2013-05-27 08:19:35 +00:00
void activeFocusOnTab7 ( ) ;
void activeFocusOnTab8 ( ) ;
2014-04-01 08:05:28 +00:00
void activeFocusOnTab9 ( ) ;
void activeFocusOnTab10 ( ) ;
2020-02-26 13:14:50 +00:00
void activeFocusOnTab_infiniteLoop_data ( ) ;
2018-08-02 06:40:04 +00:00
void activeFocusOnTab_infiniteLoop ( ) ;
2013-02-22 12:44:06 +00:00
2013-04-12 09:52:48 +00:00
void nextItemInFocusChain ( ) ;
2013-05-10 09:54:37 +00:00
void nextItemInFocusChain2 ( ) ;
2013-09-19 11:13:49 +00:00
void nextItemInFocusChain3 ( ) ;
2013-04-12 09:52:48 +00:00
2015-07-02 16:20:41 +00:00
void tabFence ( ) ;
2016-01-20 11:21:21 +00:00
void qtbug_50516 ( ) ;
2016-01-21 12:19:24 +00:00
void qtbug_50516_2_data ( ) ;
void qtbug_50516_2 ( ) ;
2021-04-16 12:16:37 +00:00
void focusableItemReparentedToLoadedComponent ( ) ;
2015-07-02 16:20:41 +00:00
2011-04-27 12:13:26 +00:00
void keys ( ) ;
2019-10-18 09:00:53 +00:00
# if QT_CONFIG(shortcut)
2013-05-30 15:06:58 +00:00
void standardKeys_data ( ) ;
void standardKeys ( ) ;
2019-10-18 09:00:53 +00:00
# endif
2011-04-27 12:13:26 +00:00
void keysProcessingOrder ( ) ;
2012-02-15 03:20:56 +00:00
void keysim ( ) ;
2014-04-10 19:18:09 +00:00
void keysForward ( ) ;
2013-10-01 19:09:52 +00:00
void keyNavigation_data ( ) ;
2011-04-27 12:13:26 +00:00
void keyNavigation ( ) ;
void keyNavigation_RightToLeft ( ) ;
void keyNavigation_skipNotVisible ( ) ;
void keyNavigation_implicitSetting ( ) ;
2019-05-19 16:16:34 +00:00
void keyNavigation_implicitDestroy ( ) ;
2014-05-02 13:18:25 +00:00
void keyNavigation_focusReason ( ) ;
2015-10-12 08:24:38 +00:00
void keyNavigation_loop ( ) ;
2020-04-20 17:42:56 +00:00
void keyNavigation_repeater ( ) ;
2011-04-27 12:13:26 +00:00
void layoutMirroring ( ) ;
2016-08-26 08:58:32 +00:00
void layoutMirroringWindow ( ) ;
2011-04-27 12:13:26 +00:00
void layoutMirroringIllegalParent ( ) ;
void smooth ( ) ;
2012-07-24 03:40:55 +00:00
void antialiasing ( ) ;
2011-04-27 12:13:26 +00:00
void clip ( ) ;
void mapCoordinates ( ) ;
void mapCoordinates_data ( ) ;
2012-05-16 02:59:32 +00:00
void mapCoordinatesRect ( ) ;
void mapCoordinatesRect_data ( ) ;
2011-04-27 12:13:26 +00:00
void propertyChanges ( ) ;
2016-11-16 15:30:39 +00:00
void nonexistentPropertyConnection ( ) ;
2011-04-27 12:13:26 +00:00
void transforms ( ) ;
void transforms_data ( ) ;
void childrenRect ( ) ;
void childrenRectBug ( ) ;
void childrenRectBug2 ( ) ;
void childrenRectBug3 ( ) ;
2015-09-20 15:53:57 +00:00
void childrenRectBottomRightCorner ( ) ;
2011-04-27 12:13:26 +00:00
void childrenProperty ( ) ;
void resourcesProperty ( ) ;
QQuickItem: Make x/y/width/height bindable
This ports the four properties properties to Q_OBJECT_COMPAT_PROPERTY.
It is not possible to use Q_OBJECT_BINDABLE_PROPERTY, not even for the
simpler x and y properties, as QQuickItem calls the virtual
geometryChange method in the setters, making them non-trivial.
For width/height, we use the new property system to improve the
width/heightValid check: We return not only valid if the property has
explictily been set (either via a direct setter call, or because the
setter has been called when a binding evaluated), but also when a
binding is set. This matches the fact that implicitWidth/Height should
only be used when width/height is not specified. In theory, this could
help in cases where one sets both implicit and explicit properties for
Text, skipping one initial layout calculation.
As the setters now remove the binding (this aligns with the behavior of
writing through the meta-object system), a few test cases had to be
adjusted, which relied on the setter not removing it.
Further test changes were necessary because the location of a few
warnings changed, due to differences in binding order evaluation and
error reporting.
[ChangeLog][QtQuick][QQuickItem] The x, y, width and height properties
of QQuickItem are now bindable. This enables modifying their bindings
from C++. One could for instance swap the width of two items,
preserving the bindings:
// QML
Item {
Rectangle { id: a; width: parent.width / 2 }
Rectangle { id: b; width: parent.width / 3; anchors.right: a.left }
}
// C++
auto rootCtxt = engine.rootContext();
auto a = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"a"_qs));
auto b = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"b"_qs));
auto aBinding = a->bindableWidth().takeBinding();
auto bBinding = b->bindableWidth().takeBinding();
a->bindableWidth()->setBinding(bBinding);
b->bindableWidth()->setBinding(aBinding);
Afterwards, if the root item gets resized, the width of the rectangles
will still be adjusted.
[ChangeLog][QtQuick][QQuickItem][Important Behavior Changes] Calling
the setWidth, setHeight, setX and setY properties now removes any
existing binding from the corresponding property.
Change-Id: I5e1553611cb92b033247ada715cea48c962395bc
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-09-24 09:06:06 +00:00
void bindableProperties_data ( ) ;
void bindableProperties ( ) ;
2011-04-27 12:13:26 +00:00
2016-05-19 11:10:19 +00:00
void changeListener ( ) ;
2011-04-27 12:13:26 +00:00
void transformCrash ( ) ;
void implicitSize ( ) ;
void qtbug_16871 ( ) ;
2011-12-05 19:21:33 +00:00
void visibleChildren ( ) ;
2012-02-15 03:20:56 +00:00
void parentLoop ( ) ;
2012-02-26 16:26:53 +00:00
void contains_data ( ) ;
void contains ( ) ;
2012-07-24 03:40:55 +00:00
void childAt ( ) ;
2015-11-19 06:46:01 +00:00
void isAncestorOf ( ) ;
2012-02-26 16:26:53 +00:00
2014-04-28 11:30:04 +00:00
void grab ( ) ;
2021-06-11 12:19:17 +00:00
void colorGroup ( ) ;
2014-04-28 11:30:04 +00:00
2011-04-27 12:13:26 +00:00
private :
2012-02-16 04:43:03 +00:00
QQmlEngine engine ;
2013-05-10 09:54:37 +00:00
bool qt_tab_all_widgets ( ) {
2015-02-04 09:10:12 +00:00
return QGuiApplication : : styleHints ( ) - > tabFocusBehavior ( ) = = Qt : : TabFocusAllControls ;
2013-05-10 09:54:37 +00:00
}
2011-04-27 12:13:26 +00:00
} ;
class KeysTestObject : public QObject
{
Q_OBJECT
Q_PROPERTY ( bool processLast READ processLast NOTIFY processLastChanged )
public :
2012-07-03 01:04:02 +00:00
KeysTestObject ( ) : mKey ( 0 ) , mModifiers ( 0 ) , mForwardedKey ( 0 ) , mLast ( false ) , mNativeScanCode ( 0 ) { }
2011-04-27 12:13:26 +00:00
void reset ( ) {
mKey = 0 ;
mText = QString ( ) ;
mModifiers = 0 ;
mForwardedKey = 0 ;
2012-07-03 01:04:02 +00:00
mNativeScanCode = 0 ;
2011-04-27 12:13:26 +00:00
}
bool processLast ( ) const { return mLast ; }
void setProcessLast ( bool b ) {
if ( b ! = mLast ) {
mLast = b ;
emit processLastChanged ( ) ;
}
}
public slots :
void keyPress ( int key , QString text , int modifiers ) {
mKey = key ;
mText = text ;
mModifiers = modifiers ;
}
void keyRelease ( int key , QString text , int modifiers ) {
mKey = key ;
mText = text ;
mModifiers = modifiers ;
}
void forwardedKey ( int key ) {
mForwardedKey = key ;
}
2012-07-03 01:04:02 +00:00
void specialKey ( int key , QString text , quint32 nativeScanCode ) {
mKey = key ;
mText = text ;
mNativeScanCode = nativeScanCode ;
}
2011-04-27 12:13:26 +00:00
signals :
void processLastChanged ( ) ;
public :
int mKey ;
QString mText ;
int mModifiers ;
int mForwardedKey ;
bool mLast ;
2012-07-03 01:04:02 +00:00
quint32 mNativeScanCode ;
2011-04-27 12:13:26 +00:00
private :
} ;
2011-10-14 08:51:42 +00:00
class KeyTestItem : public QQuickItem
2011-04-27 12:13:26 +00:00
{
Q_OBJECT
public :
2018-02-21 09:41:54 +00:00
KeyTestItem ( QQuickItem * parent = nullptr ) : QQuickItem ( parent ) , mKey ( 0 ) { }
2011-04-27 12:13:26 +00:00
protected :
2021-03-25 13:44:04 +00:00
void keyPressEvent ( QKeyEvent * e ) override {
2011-04-27 12:13:26 +00:00
mKey = e - > key ( ) ;
if ( e - > key ( ) = = Qt : : Key_A )
e - > accept ( ) ;
else
e - > ignore ( ) ;
}
2021-03-25 13:44:04 +00:00
void keyReleaseEvent ( QKeyEvent * e ) override {
2011-04-27 12:13:26 +00:00
if ( e - > key ( ) = = Qt : : Key_B )
e - > accept ( ) ;
else
e - > ignore ( ) ;
}
public :
int mKey ;
} ;
2014-05-02 13:18:25 +00:00
class FocusEventFilter : public QObject
{
protected :
2021-03-25 13:44:04 +00:00
bool eventFilter ( QObject * watched , QEvent * event ) override {
2014-05-02 13:18:25 +00:00
if ( ( event - > type ( ) = = QEvent : : FocusIn ) | | ( event - > type ( ) = = QEvent : : FocusOut ) ) {
QFocusEvent * focusEvent = static_cast < QFocusEvent * > ( event ) ;
lastFocusReason = focusEvent - > reason ( ) ;
return false ;
} else
return QObject : : eventFilter ( watched , event ) ;
}
public :
Qt : : FocusReason lastFocusReason ;
} ;
2011-04-27 12:13:26 +00:00
QML_DECLARE_TYPE ( KeyTestItem ) ;
2012-02-26 16:26:53 +00:00
class HollowTestItem : public QQuickItem
{
Q_OBJECT
Q_PROPERTY ( bool circle READ isCircle WRITE setCircle )
Q_PROPERTY ( qreal holeRadius READ holeRadius WRITE setHoleRadius )
public :
2018-02-21 09:41:54 +00:00
HollowTestItem ( QQuickItem * parent = nullptr )
2012-02-26 16:26:53 +00:00
: QQuickItem ( parent ) ,
m_isPressed ( false ) ,
m_isHovered ( false ) ,
m_isCircle ( false ) ,
m_holeRadius ( 50 )
{
setAcceptHoverEvents ( true ) ;
setAcceptedMouseButtons ( Qt : : LeftButton ) ;
}
bool isPressed ( ) const { return m_isPressed ; }
bool isHovered ( ) const { return m_isHovered ; }
bool isCircle ( ) const { return m_isCircle ; }
void setCircle ( bool circle ) { m_isCircle = circle ; }
qreal holeRadius ( ) const { return m_holeRadius ; }
void setHoleRadius ( qreal radius ) { m_holeRadius = radius ; }
2021-03-25 13:44:04 +00:00
bool contains ( const QPointF & point ) const override {
2012-02-26 16:26:53 +00:00
const qreal w = width ( ) ;
const qreal h = height ( ) ;
const qreal r = m_holeRadius ;
// check boundaries
if ( ! QRectF ( 0 , 0 , w , h ) . contains ( point ) )
return false ;
// square shape
if ( ! m_isCircle )
return ! QRectF ( w / 2 - r , h / 2 - r , r * 2 , r * 2 ) . contains ( point ) ;
// circle shape
const qreal dx = point . x ( ) - ( w / 2 ) ;
const qreal dy = point . y ( ) - ( h / 2 ) ;
const qreal dd = ( dx * dx ) + ( dy * dy ) ;
const qreal outerRadius = qMin < qreal > ( w / 2 , h / 2 ) ;
return dd > ( r * r ) & & dd < = outerRadius * outerRadius ;
}
protected :
2021-03-25 13:44:04 +00:00
void hoverEnterEvent ( QHoverEvent * ) override { m_isHovered = true ; }
void hoverLeaveEvent ( QHoverEvent * ) override { m_isHovered = false ; }
void mousePressEvent ( QMouseEvent * ) override { m_isPressed = true ; }
void mouseReleaseEvent ( QMouseEvent * ) override { m_isPressed = false ; }
2012-02-26 16:26:53 +00:00
private :
bool m_isPressed ;
bool m_isHovered ;
bool m_isCircle ;
qreal m_holeRadius ;
} ;
QML_DECLARE_TYPE ( HollowTestItem ) ;
2015-07-02 16:20:41 +00:00
class TabFenceItem : public QQuickItem
{
Q_OBJECT
public :
2017-10-06 09:38:59 +00:00
TabFenceItem ( QQuickItem * parent = nullptr )
2015-07-02 16:20:41 +00:00
: QQuickItem ( parent )
{
QQuickItemPrivate * d = QQuickItemPrivate : : get ( this ) ;
d - > isTabFence = true ;
}
} ;
QML_DECLARE_TYPE ( TabFenceItem ) ;
2011-04-27 12:13:26 +00:00
2016-01-21 12:19:24 +00:00
class TabFenceItem2 : public QQuickItem
{
Q_OBJECT
public :
2017-10-06 09:38:59 +00:00
TabFenceItem2 ( QQuickItem * parent = nullptr )
2016-01-21 12:19:24 +00:00
: QQuickItem ( parent )
{
QQuickItemPrivate * d = QQuickItemPrivate : : get ( this ) ;
d - > isTabFence = true ;
setFlag ( ItemIsFocusScope ) ;
}
} ;
QML_DECLARE_TYPE ( TabFenceItem2 ) ;
2011-10-14 08:51:42 +00:00
tst_QQuickItem : : tst_QQuickItem ( )
2011-04-27 12:13:26 +00:00
{
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : initTestCase ( )
2011-04-27 12:13:26 +00:00
{
2012-02-16 04:43:03 +00:00
QQmlDataTest : : initTestCase ( ) ;
2011-04-27 12:13:26 +00:00
qmlRegisterType < KeyTestItem > ( " Test " , 1 , 0 , " KeyTestItem " ) ;
2012-02-26 16:26:53 +00:00
qmlRegisterType < HollowTestItem > ( " Test " , 1 , 0 , " HollowTestItem " ) ;
2015-07-02 16:20:41 +00:00
qmlRegisterType < TabFenceItem > ( " Test " , 1 , 0 , " TabFence " ) ;
2016-01-21 12:19:24 +00:00
qmlRegisterType < TabFenceItem2 > ( " Test " , 1 , 0 , " TabFence2 " ) ;
2011-04-27 12:13:26 +00:00
}
2012-02-15 03:20:56 +00:00
void tst_QQuickItem : : cleanup ( )
{
QInputMethodPrivate * inputMethodPrivate = QInputMethodPrivate : : get ( qApp - > inputMethod ( ) ) ;
2018-02-21 09:41:54 +00:00
inputMethodPrivate - > testContext = nullptr ;
2012-02-15 03:20:56 +00:00
}
2013-02-22 12:44:06 +00:00
void tst_QQuickItem : : activeFocusOnTab ( )
{
2013-05-10 09:54:37 +00:00
if ( ! qt_tab_all_widgets ( ) )
QSKIP ( " This function doesn't support NOT iterating all. " ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2013-02-22 12:44:06 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2013-02-22 12:44:06 +00:00
// original: button12
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " button12 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
// Tab: button12->sub2
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " sub2 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// Tab: sub2->button21
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button21 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// Tab: button21->button22
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button22 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// Tab: button22->edit
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " edit " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// BackTab: edit->button22
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button22 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// BackTab: button22->button21
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button21 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// BackTab: button21->sub2
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " sub2 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// BackTab: sub2->button12
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button12 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// BackTab: button12->button11
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button11 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// BackTab: button11->edit
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " edit " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
delete window ;
}
void tst_QQuickItem : : activeFocusOnTab2 ( )
{
2013-05-10 09:54:37 +00:00
if ( ! qt_tab_all_widgets ( ) )
QSKIP ( " This function doesn't support NOT iterating all. " ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2013-02-22 12:44:06 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2013-02-22 12:44:06 +00:00
// original: button12
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " button12 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
// BackTab: button12->button11
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button11 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// BackTab: button11->edit
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " edit " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
delete window ;
}
void tst_QQuickItem : : activeFocusOnTab3 ( )
{
2013-05-10 09:54:37 +00:00
if ( ! qt_tab_all_widgets ( ) )
QSKIP ( " This function doesn't support NOT iterating all. " ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2013-02-22 12:44:06 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab3.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2013-02-22 12:44:06 +00:00
// original: button1
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " button1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
// 4 Tabs: button1->button2, through a repeater
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ; ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button2 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 Tabs: button2->button3, through a row
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ; ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button3 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 Tabs: button3->button4, through a flow
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ; ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button4 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 Tabs: button4->button5, through a focusscope
// parent is activeFocusOnTab:false, one of children is focus:true
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ; ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button5 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 Tabs: button5->button6, through a focusscope
// parent is activeFocusOnTab:true, one of children is focus:true
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ; ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button6 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 5 Tabs: button6->button7, through a focusscope
// parent is activeFocusOnTab:true, none of children is focus:true
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ; ;
for ( int i = 0 ; i < 5 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button7 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 BackTabs: button7->button6, through a focusscope
// parent is activeFocusOnTab:true, one of children got focus:true in previous code
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button6 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 Tabs: button6->button7, through a focusscope
// parent is activeFocusOnTab:true, one of children got focus:true in previous code
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ; ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button7 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 BackTabs: button7->button6, through a focusscope
// parent is activeFocusOnTab:true, one of children got focus:true in previous code
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button6 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 BackTabs: button6->button5, through a focusscope(parent is activeFocusOnTab: false)
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button5 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 BackTabs: button5->button4, through a focusscope(parent is activeFocusOnTab: false)
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button4 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 BackTabs: button4->button3, through a flow
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button3 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 BackTabs: button3->button2, through a row
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button2 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
// 4 BackTabs: button2->button1, through a repeater
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
for ( int i = 0 ; i < 4 ; + + i ) {
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2013-02-22 12:44:06 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-02-22 12:44:06 +00:00
delete window ;
}
2013-04-09 10:09:24 +00:00
void tst_QQuickItem : : activeFocusOnTab4 ( )
{
2013-05-10 09:54:37 +00:00
if ( ! qt_tab_all_widgets ( ) )
QSKIP ( " This function doesn't support NOT iterating all. " ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2013-04-09 10:09:24 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab4.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2013-04-09 10:09:24 +00:00
// original: button11
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " button11 " ) ;
item - > setActiveFocusOnTab ( true ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
// Tab: button11->button21
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button21 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
delete window ;
}
void tst_QQuickItem : : activeFocusOnTab5 ( )
{
2013-05-10 09:54:37 +00:00
if ( ! qt_tab_all_widgets ( ) )
QSKIP ( " This function doesn't support NOT iterating all. " ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2013-04-09 10:09:24 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab4.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2013-04-09 10:09:24 +00:00
// original: button11 in sub1
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " button11 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
QQuickItem * item2 = findItem < QQuickItem > ( window - > rootObject ( ) , " sub1 " ) ;
item2 - > setActiveFocusOnTab ( true ) ;
// Tab: button11->button21
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button21 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
delete window ;
}
2013-05-10 09:54:37 +00:00
void tst_QQuickItem : : activeFocusOnTab6 ( )
{
if ( qt_tab_all_widgets ( ) )
QSKIP ( " This function doesn't support iterating all. " ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2013-05-10 09:54:37 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab6.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2013-05-10 09:54:37 +00:00
// original: button12
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " button12 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
// Tab: button12->edit
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-05-10 09:54:37 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " edit " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-05-10 09:54:37 +00:00
// BackTab: edit->button12
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-05-10 09:54:37 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button12 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-05-10 09:54:37 +00:00
// BackTab: button12->button11
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-05-10 09:54:37 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " button11 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-05-10 09:54:37 +00:00
// BackTab: button11->edit
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-05-10 09:54:37 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " edit " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-05-10 09:54:37 +00:00
delete window ;
}
2013-05-27 08:19:35 +00:00
void tst_QQuickItem : : activeFocusOnTab7 ( )
{
if ( qt_tab_all_widgets ( ) )
QSKIP ( " This function doesn't support iterating all. " ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2013-05-27 08:19:35 +00:00
window - > setBaseSize ( QSize ( 300 , 300 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab7.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2013-05-27 08:19:35 +00:00
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " button1 " ) ;
QVERIFY ( item ) ;
item - > forceActiveFocus ( ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
// Tab: button1->button1
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( ! key . isAccepted ( ) ) ;
2013-05-27 08:19:35 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-05-27 08:19:35 +00:00
// BackTab: button1->button1
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( ! key . isAccepted ( ) ) ;
2013-05-27 08:19:35 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2013-05-27 08:19:35 +00:00
delete window ;
}
void tst_QQuickItem : : activeFocusOnTab8 ( )
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2013-05-27 08:19:35 +00:00
window - > setBaseSize ( QSize ( 300 , 300 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab8.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2013-05-27 08:19:35 +00:00
QQuickItem * content = window - > contentItem ( ) ;
QVERIFY ( content ) ;
QVERIFY ( content - > hasActiveFocus ( ) ) ;
QQuickItem * button1 = findItem < QQuickItem > ( window - > rootObject ( ) , " button1 " ) ;
QVERIFY ( button1 ) ;
QVERIFY ( ! button1 - > hasActiveFocus ( ) ) ;
QQuickItem * button2 = findItem < QQuickItem > ( window - > rootObject ( ) , " button2 " ) ;
QVERIFY ( button2 ) ;
QVERIFY ( ! button2 - > hasActiveFocus ( ) ) ;
// Tab: contentItem->button1
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-05-27 08:19:35 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( button1 - > hasActiveFocus ( ) ) ;
}
2013-05-27 08:19:35 +00:00
// Tab: button1->button2
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-05-27 08:19:35 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( button2 - > hasActiveFocus ( ) ) ;
QVERIFY ( ! button1 - > hasActiveFocus ( ) ) ;
}
2013-05-27 08:19:35 +00:00
// BackTab: button2->button1
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2013-05-27 08:19:35 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( button1 - > hasActiveFocus ( ) ) ;
QVERIFY ( ! button2 - > hasActiveFocus ( ) ) ;
}
2013-05-27 08:19:35 +00:00
delete window ;
}
2014-04-01 08:05:28 +00:00
void tst_QQuickItem : : activeFocusOnTab9 ( )
{
if ( qt_tab_all_widgets ( ) )
QSKIP ( " This function doesn't support iterating all. " ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2014-04-01 08:05:28 +00:00
window - > setBaseSize ( QSize ( 300 , 300 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab9.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2014-04-01 08:05:28 +00:00
QQuickItem * content = window - > contentItem ( ) ;
QVERIFY ( content ) ;
QVERIFY ( content - > hasActiveFocus ( ) ) ;
QQuickItem * textinput1 = findItem < QQuickItem > ( window - > rootObject ( ) , " textinput1 " ) ;
QVERIFY ( textinput1 ) ;
QQuickItem * textedit1 = findItem < QQuickItem > ( window - > rootObject ( ) , " textedit1 " ) ;
QVERIFY ( textedit1 ) ;
QVERIFY ( ! textinput1 - > hasActiveFocus ( ) ) ;
textinput1 - > forceActiveFocus ( ) ;
QVERIFY ( textinput1 - > hasActiveFocus ( ) ) ;
// Tab: textinput1->textedit1
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2014-04-01 08:05:28 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( textedit1 - > hasActiveFocus ( ) ) ;
}
2014-04-01 08:05:28 +00:00
// BackTab: textedit1->textinput1
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2014-04-01 08:05:28 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( textinput1 - > hasActiveFocus ( ) ) ;
}
2014-04-01 08:05:28 +00:00
// BackTab: textinput1->textedit1
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2014-04-01 08:05:28 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( textedit1 - > hasActiveFocus ( ) ) ;
}
2014-04-01 08:05:28 +00:00
delete window ;
}
void tst_QQuickItem : : activeFocusOnTab10 ( )
{
if ( ! qt_tab_all_widgets ( ) )
QSKIP ( " This function doesn't support NOT iterating all. " ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2014-04-01 08:05:28 +00:00
window - > setBaseSize ( QSize ( 300 , 300 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab9.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2014-04-01 08:05:28 +00:00
QQuickItem * content = window - > contentItem ( ) ;
QVERIFY ( content ) ;
QVERIFY ( content - > hasActiveFocus ( ) ) ;
QQuickItem * textinput1 = findItem < QQuickItem > ( window - > rootObject ( ) , " textinput1 " ) ;
QVERIFY ( textinput1 ) ;
QQuickItem * textedit1 = findItem < QQuickItem > ( window - > rootObject ( ) , " textedit1 " ) ;
QVERIFY ( textedit1 ) ;
QQuickItem * textinput2 = findItem < QQuickItem > ( window - > rootObject ( ) , " textinput2 " ) ;
QVERIFY ( textinput2 ) ;
QQuickItem * textedit2 = findItem < QQuickItem > ( window - > rootObject ( ) , " textedit2 " ) ;
QVERIFY ( textedit2 ) ;
QVERIFY ( ! textinput1 - > hasActiveFocus ( ) ) ;
textinput1 - > forceActiveFocus ( ) ;
QVERIFY ( textinput1 - > hasActiveFocus ( ) ) ;
// Tab: textinput1->textinput2
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2014-04-01 08:05:28 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( textinput2 - > hasActiveFocus ( ) ) ;
}
2014-04-01 08:05:28 +00:00
// Tab: textinput2->textedit1
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2014-04-01 08:05:28 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( textedit1 - > hasActiveFocus ( ) ) ;
}
2014-04-01 08:05:28 +00:00
// BackTab: textedit1->textinput2
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2014-04-01 08:05:28 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( textinput2 - > hasActiveFocus ( ) ) ;
}
2014-04-01 08:05:28 +00:00
// BackTab: textinput2->textinput1
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2014-04-01 08:05:28 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( textinput1 - > hasActiveFocus ( ) ) ;
}
2014-04-01 08:05:28 +00:00
// BackTab: textinput1->textedit2
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2014-04-01 08:05:28 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( textedit2 - > hasActiveFocus ( ) ) ;
}
2014-04-01 08:05:28 +00:00
// BackTab: textedit2->textedit1
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2014-04-01 08:05:28 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( textedit1 - > hasActiveFocus ( ) ) ;
}
2014-04-01 08:05:28 +00:00
// BackTab: textedit1->textinput2
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : ShiftModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2014-04-01 08:05:28 +00:00
2020-11-11 12:51:35 +00:00
QVERIFY ( textinput2 - > hasActiveFocus ( ) ) ;
}
2014-04-01 08:05:28 +00:00
delete window ;
}
2020-02-26 13:14:50 +00:00
void tst_QQuickItem : : activeFocusOnTab_infiniteLoop_data ( )
{
QTest : : addColumn < QUrl > ( " source " ) ;
QTest : : newRow ( " infiniteLoop " ) < < testFileUrl ( " activeFocusOnTab_infiniteLoop.qml " ) ; // QTBUG-68271
QTest : : newRow ( " infiniteLoop2 " ) < < testFileUrl ( " activeFocusOnTab_infiniteLoop2.qml " ) ; // QTBUG-81510
}
2018-08-02 06:40:04 +00:00
void tst_QQuickItem : : activeFocusOnTab_infiniteLoop ( )
{
2020-02-26 13:14:50 +00:00
QFETCH ( QUrl , source ) ;
2018-08-02 06:40:04 +00:00
// create a window where the currently focused item is not visible
QScopedPointer < QQuickView > window ( new QQuickView ( ) ) ;
2020-02-26 13:14:50 +00:00
window - > setSource ( source ) ;
2018-08-02 06:40:04 +00:00
window - > show ( ) ;
auto * hiddenChild = findItem < QQuickItem > ( window - > rootObject ( ) , " hiddenChild " ) ;
QVERIFY ( hiddenChild ) ;
// move the focus - this used to result in an infinite loop
auto * item = hiddenChild - > nextItemInFocusChain ( ) ;
// focus is moved to the root object since there is no other candidate
QCOMPARE ( item , window - > rootObject ( ) ) ;
2020-02-26 13:14:50 +00:00
item = hiddenChild - > nextItemInFocusChain ( false ) ;
QCOMPARE ( item , window - > rootObject ( ) ) ;
2018-08-02 06:40:04 +00:00
}
2013-04-12 09:52:48 +00:00
void tst_QQuickItem : : nextItemInFocusChain ( )
{
2013-05-10 09:54:37 +00:00
if ( ! qt_tab_all_widgets ( ) )
QSKIP ( " This function doesn't support NOT iterating all. " ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2013-04-12 09:52:48 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2013-04-12 09:52:48 +00:00
QQuickItem * button11 = findItem < QQuickItem > ( window - > rootObject ( ) , " button11 " ) ;
QVERIFY ( button11 ) ;
QQuickItem * button12 = findItem < QQuickItem > ( window - > rootObject ( ) , " button12 " ) ;
QVERIFY ( button12 ) ;
QQuickItem * sub2 = findItem < QQuickItem > ( window - > rootObject ( ) , " sub2 " ) ;
QVERIFY ( sub2 ) ;
QQuickItem * button21 = findItem < QQuickItem > ( window - > rootObject ( ) , " button21 " ) ;
QVERIFY ( button21 ) ;
QQuickItem * button22 = findItem < QQuickItem > ( window - > rootObject ( ) , " button22 " ) ;
QVERIFY ( button22 ) ;
QQuickItem * edit = findItem < QQuickItem > ( window - > rootObject ( ) , " edit " ) ;
QVERIFY ( edit ) ;
QQuickItem * next , * prev ;
next = button11 - > nextItemInFocusChain ( true ) ;
QVERIFY ( next ) ;
QCOMPARE ( next , button12 ) ;
prev = button11 - > nextItemInFocusChain ( false ) ;
QVERIFY ( prev ) ;
QCOMPARE ( prev , edit ) ;
next = button12 - > nextItemInFocusChain ( ) ;
QVERIFY ( next ) ;
QCOMPARE ( next , sub2 ) ;
prev = button12 - > nextItemInFocusChain ( false ) ;
QVERIFY ( prev ) ;
QCOMPARE ( prev , button11 ) ;
next = sub2 - > nextItemInFocusChain ( true ) ;
QVERIFY ( next ) ;
QCOMPARE ( next , button21 ) ;
prev = sub2 - > nextItemInFocusChain ( false ) ;
QVERIFY ( prev ) ;
QCOMPARE ( prev , button12 ) ;
next = button21 - > nextItemInFocusChain ( ) ;
QVERIFY ( next ) ;
QCOMPARE ( next , button22 ) ;
prev = button21 - > nextItemInFocusChain ( false ) ;
QVERIFY ( prev ) ;
QCOMPARE ( prev , sub2 ) ;
next = button22 - > nextItemInFocusChain ( true ) ;
QVERIFY ( next ) ;
QCOMPARE ( next , edit ) ;
prev = button22 - > nextItemInFocusChain ( false ) ;
QVERIFY ( prev ) ;
QCOMPARE ( prev , button21 ) ;
next = edit - > nextItemInFocusChain ( ) ;
QVERIFY ( next ) ;
QCOMPARE ( next , button11 ) ;
prev = edit - > nextItemInFocusChain ( false ) ;
QVERIFY ( prev ) ;
QCOMPARE ( prev , button22 ) ;
delete window ;
}
2013-05-10 09:54:37 +00:00
void tst_QQuickItem : : nextItemInFocusChain2 ( )
{
if ( qt_tab_all_widgets ( ) )
QSKIP ( " This function doesn't support iterating all. " ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2013-05-10 09:54:37 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( " activeFocusOnTab6.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2013-05-10 09:54:37 +00:00
QQuickItem * button11 = findItem < QQuickItem > ( window - > rootObject ( ) , " button11 " ) ;
QVERIFY ( button11 ) ;
QQuickItem * button12 = findItem < QQuickItem > ( window - > rootObject ( ) , " button12 " ) ;
QVERIFY ( button12 ) ;
QQuickItem * edit = findItem < QQuickItem > ( window - > rootObject ( ) , " edit " ) ;
QVERIFY ( edit ) ;
QQuickItem * next , * prev ;
next = button11 - > nextItemInFocusChain ( true ) ;
QVERIFY ( next ) ;
QCOMPARE ( next , button12 ) ;
prev = button11 - > nextItemInFocusChain ( false ) ;
QVERIFY ( prev ) ;
QCOMPARE ( prev , edit ) ;
next = button12 - > nextItemInFocusChain ( ) ;
QVERIFY ( next ) ;
QCOMPARE ( next , edit ) ;
prev = button12 - > nextItemInFocusChain ( false ) ;
QVERIFY ( prev ) ;
QCOMPARE ( prev , button11 ) ;
next = edit - > nextItemInFocusChain ( ) ;
QVERIFY ( next ) ;
QCOMPARE ( next , button11 ) ;
prev = edit - > nextItemInFocusChain ( false ) ;
QVERIFY ( prev ) ;
QCOMPARE ( prev , button12 ) ;
delete window ;
}
2013-09-19 11:13:49 +00:00
void tst_QQuickItem : : nextItemInFocusChain3 ( )
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2013-09-19 11:13:49 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( " nextItemInFocusChain3.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2013-09-19 11:13:49 +00:00
}
2015-07-02 16:20:41 +00:00
void verifyTabFocusChain ( QQuickView * window , const char * * focusChain , bool forward )
{
int idx = 0 ;
for ( const char * * objectName = focusChain ; * objectName ; + + objectName , + + idx ) {
const QString & descrStr = QString ( " idx=%1 objectName= \" %2 \" " ) . arg ( idx ) . arg ( * objectName ) ;
const char * descr = descrStr . toLocal8Bit ( ) . data ( ) ;
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Tab , forward ? Qt : : NoModifier : Qt : : ShiftModifier ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY2 ( key . isAccepted ( ) , descr ) ;
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , * objectName ) ;
QVERIFY2 ( item , descr ) ;
QVERIFY2 ( item - > hasActiveFocus ( ) , descr ) ;
}
}
void tst_QQuickItem : : tabFence ( )
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2015-07-02 16:20:41 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( " tabFence.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
QVERIFY ( QGuiApplication : : focusWindow ( ) = = window ) ;
QVERIFY ( window - > rootObject ( ) - > hasActiveFocus ( ) ) ;
const char * rootTabFocusChain [ ] = {
2017-10-06 09:38:59 +00:00
" input1 " , " input2 " , " input3 " , " input1 " , nullptr
2015-07-02 16:20:41 +00:00
} ;
verifyTabFocusChain ( window , rootTabFocusChain , true /* forward */ ) ;
const char * rootBacktabFocusChain [ ] = {
2017-10-06 09:38:59 +00:00
" input3 " , " input2 " , " input1 " , " input3 " , nullptr
2015-07-02 16:20:41 +00:00
} ;
verifyTabFocusChain ( window , rootBacktabFocusChain , false /* forward */ ) ;
// Give focus to input11 in fence1
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " input11 " ) ;
item - > setFocus ( true ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
const char * fence1TabFocusChain [ ] = {
2017-10-06 09:38:59 +00:00
" input12 " , " input13 " , " input11 " , " input12 " , nullptr
2015-07-02 16:20:41 +00:00
} ;
verifyTabFocusChain ( window , fence1TabFocusChain , true /* forward */ ) ;
const char * fence1BacktabFocusChain [ ] = {
2017-10-06 09:38:59 +00:00
" input11 " , " input13 " , " input12 " , " input11 " , nullptr
2015-07-02 16:20:41 +00:00
} ;
verifyTabFocusChain ( window , fence1BacktabFocusChain , false /* forward */ ) ;
}
2016-01-20 11:21:21 +00:00
void tst_QQuickItem : : qtbug_50516 ( )
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2016-01-20 11:21:21 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( " qtbug_50516.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
QVERIFY ( QGuiApplication : : focusWindow ( ) = = window ) ;
QVERIFY ( window - > rootObject ( ) - > hasActiveFocus ( ) ) ;
QQuickItem * contentItem = window - > rootObject ( ) ;
QQuickItem * next = contentItem - > nextItemInFocusChain ( true ) ;
QCOMPARE ( next , contentItem ) ;
next = contentItem - > nextItemInFocusChain ( false ) ;
QCOMPARE ( next , contentItem ) ;
delete window ;
}
2016-01-21 12:19:24 +00:00
void tst_QQuickItem : : qtbug_50516_2_data ( )
{
QTest : : addColumn < QString > ( " filename " ) ;
QTest : : addColumn < QString > ( " item1 " ) ;
QTest : : addColumn < QString > ( " item2 " ) ;
QTest : : newRow ( " FocusScope TabFence with one Item(focused) " )
< < QStringLiteral ( " qtbug_50516_2_1.qml " ) < < QStringLiteral ( " root " ) < < QStringLiteral ( " root " ) ;
QTest : : newRow ( " FocusScope TabFence with one Item(unfocused) " )
< < QStringLiteral ( " qtbug_50516_2_2.qml " ) < < QStringLiteral ( " root " ) < < QStringLiteral ( " root " ) ;
QTest : : newRow ( " FocusScope TabFence with two Items(focused) " )
< < QStringLiteral ( " qtbug_50516_2_3.qml " ) < < QStringLiteral ( " root " ) < < QStringLiteral ( " root " ) ;
QTest : : newRow ( " FocusScope TabFence with two Items(unfocused) " )
< < QStringLiteral ( " qtbug_50516_2_4.qml " ) < < QStringLiteral ( " root " ) < < QStringLiteral ( " root " ) ;
QTest : : newRow ( " FocusScope TabFence with one Item and one TextInput(unfocused) " )
< < QStringLiteral ( " qtbug_50516_2_5.qml " ) < < QStringLiteral ( " item1 " ) < < QStringLiteral ( " item1 " ) ;
QTest : : newRow ( " FocusScope TabFence with two TextInputs(unfocused) " )
< < QStringLiteral ( " qtbug_50516_2_6.qml " ) < < QStringLiteral ( " item1 " ) < < QStringLiteral ( " item2 " ) ;
}
void tst_QQuickItem : : qtbug_50516_2 ( )
{
QFETCH ( QString , filename ) ;
QFETCH ( QString , item1 ) ;
QFETCH ( QString , item2 ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2016-01-21 12:19:24 +00:00
window - > setBaseSize ( QSize ( 800 , 600 ) ) ;
window - > setSource ( testFileUrl ( filename ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
QVERIFY ( QGuiApplication : : focusWindow ( ) = = window ) ;
QVERIFY ( window - > rootObject ( ) - > hasActiveFocus ( ) ) ;
QQuickItem * contentItem = window - > rootObject ( ) ;
QQuickItem * next = contentItem - > nextItemInFocusChain ( true ) ;
QCOMPARE ( next - > objectName ( ) , item1 ) ;
next = contentItem - > nextItemInFocusChain ( false ) ;
QCOMPARE ( next - > objectName ( ) , item2 ) ;
delete window ;
}
2021-04-16 12:16:37 +00:00
void tst_QQuickItem : : focusableItemReparentedToLoadedComponent ( ) // QTBUG-89736
{
QQuickView window ;
window . setSource ( testFileUrl ( " focusableItemReparentedToLoadedComponent.qml " ) ) ;
window . show ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( & window ) ) ;
QCOMPARE ( QGuiApplication : : focusWindow ( ) , & window ) ;
QQuickLoader * loader = window . rootObject ( ) - > findChild < QQuickLoader * > ( ) ;
QVERIFY ( loader ) ;
QTRY_VERIFY ( loader - > status ( ) = = QQuickLoader : : Ready ) ;
QQuickTextInput * textInput = window . rootObject ( ) - > findChild < QQuickTextInput * > ( ) ;
QVERIFY ( textInput ) ;
// click to focus
QTest : : mouseClick ( & window , Qt : : LeftButton , Qt : : NoModifier , { 10 , 10 } ) ;
QTRY_VERIFY ( textInput - > hasActiveFocus ( ) ) ;
// unload and reload
auto component = loader - > sourceComponent ( ) ;
loader - > resetSourceComponent ( ) ;
QTRY_VERIFY ( loader - > status ( ) = = QQuickLoader : : Null ) ;
loader - > setSourceComponent ( component ) ;
QTRY_VERIFY ( loader - > status ( ) = = QQuickLoader : : Ready ) ;
// click to focus again
QTest : : mouseClick ( & window , Qt : : LeftButton , Qt : : NoModifier , { 10 , 10 } ) ;
QTRY_VERIFY ( textInput - > hasActiveFocus ( ) ) ;
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : keys ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setBaseSize ( QSize ( 240 , 320 ) ) ;
2011-04-27 12:13:26 +00:00
KeysTestObject * testObject = new KeysTestObject ;
2012-07-11 07:32:16 +00:00
window - > rootContext ( ) - > setContextProperty ( " keysTestObject " , testObject ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
window - > rootContext ( ) - > setContextProperty ( " enableKeyHanding " , QVariant ( true ) ) ;
window - > rootContext ( ) - > setContextProperty ( " forwardeeVisible " , QVariant ( true ) ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " keystest.qml " ) ) ;
window - > show ( ) ;
2012-10-19 16:05:01 +00:00
window - > requestActivate ( ) ;
2012-07-19 12:13:14 +00:00
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QVERIFY ( window - > rootObject ( ) ) ;
QCOMPARE ( window - > rootObject ( ) - > property ( " isEnabled " ) . toBool ( ) , true ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_A , Qt : : NoModifier , " A " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_A ) ) ;
QCOMPARE ( testObject - > mForwardedKey , int ( Qt : : Key_A ) ) ;
QCOMPARE ( testObject - > mText , QLatin1String ( " A " ) ) ;
QCOMPARE ( testObject - > mModifiers , int ( Qt : : NoModifier ) ) ;
QVERIFY ( ! key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
testObject - > reset ( ) ;
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyRelease , Qt : : Key_A , Qt : : ShiftModifier , " A " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_A ) ) ;
QCOMPARE ( testObject - > mForwardedKey , int ( Qt : : Key_A ) ) ;
QCOMPARE ( testObject - > mText , QLatin1String ( " A " ) ) ;
QCOMPARE ( testObject - > mModifiers , int ( Qt : : ShiftModifier ) ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
testObject - > reset ( ) ;
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Return , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_Return ) ) ;
QCOMPARE ( testObject - > mForwardedKey , int ( Qt : : Key_Return ) ) ;
QCOMPARE ( testObject - > mText , QLatin1String ( " Return " ) ) ;
QCOMPARE ( testObject - > mModifiers , int ( Qt : : NoModifier ) ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
testObject - > reset ( ) ;
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_0 , Qt : : NoModifier , " 0 " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_0 ) ) ;
QCOMPARE ( testObject - > mForwardedKey , int ( Qt : : Key_0 ) ) ;
QCOMPARE ( testObject - > mText , QLatin1String ( " 0 " ) ) ;
QCOMPARE ( testObject - > mModifiers , int ( Qt : : NoModifier ) ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
testObject - > reset ( ) ;
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_9 , Qt : : NoModifier , " 9 " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_9 ) ) ;
QCOMPARE ( testObject - > mForwardedKey , int ( Qt : : Key_9 ) ) ;
QCOMPARE ( testObject - > mText , QLatin1String ( " 9 " ) ) ;
QCOMPARE ( testObject - > mModifiers , int ( Qt : : NoModifier ) ) ;
QVERIFY ( ! key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
testObject - > reset ( ) ;
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_Tab ) ) ;
QCOMPARE ( testObject - > mForwardedKey , int ( Qt : : Key_Tab ) ) ;
QCOMPARE ( testObject - > mText , QLatin1String ( " Tab " ) ) ;
QCOMPARE ( testObject - > mModifiers , int ( Qt : : NoModifier ) ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
testObject - > reset ( ) ;
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Backtab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_Backtab ) ) ;
QCOMPARE ( testObject - > mForwardedKey , int ( Qt : : Key_Backtab ) ) ;
QCOMPARE ( testObject - > mText , QLatin1String ( " Backtab " ) ) ;
QCOMPARE ( testObject - > mModifiers , int ( Qt : : NoModifier ) ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
testObject - > reset ( ) ;
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_VolumeUp , Qt : : NoModifier , 1234 , 0 , 0 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_VolumeUp ) ) ;
QCOMPARE ( testObject - > mForwardedKey , int ( Qt : : Key_VolumeUp ) ) ;
QCOMPARE ( testObject - > mModifiers , int ( Qt : : NoModifier ) ) ;
QCOMPARE ( testObject - > mNativeScanCode , quint32 ( 1234 ) ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2012-07-03 01:04:02 +00:00
testObject - > reset ( ) ;
2020-11-11 12:51:35 +00:00
{
window - > rootContext ( ) - > setContextProperty ( " forwardeeVisible " , QVariant ( false ) ) ;
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_A , Qt : : NoModifier , " A " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_A ) ) ;
QCOMPARE ( testObject - > mForwardedKey , 0 ) ;
QCOMPARE ( testObject - > mText , QLatin1String ( " A " ) ) ;
QCOMPARE ( testObject - > mModifiers , int ( Qt : : NoModifier ) ) ;
QVERIFY ( ! key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
testObject - > reset ( ) ;
2020-11-11 12:51:35 +00:00
{
window - > rootContext ( ) - > setContextProperty ( " enableKeyHanding " , QVariant ( false ) ) ;
QCOMPARE ( window - > rootObject ( ) - > property ( " isEnabled " ) . toBool ( ) , false ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Return , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , 0 ) ;
QVERIFY ( ! key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
{
window - > rootContext ( ) - > setContextProperty ( " enableKeyHanding " , QVariant ( true ) ) ;
QCOMPARE ( window - > rootObject ( ) - > property ( " isEnabled " ) . toBool ( ) , true ) ;
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Return , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_Return ) ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
delete testObject ;
}
2019-10-18 09:00:53 +00:00
# if QT_CONFIG(shortcut)
2013-05-30 15:06:58 +00:00
Q_DECLARE_METATYPE ( QEvent : : Type ) ;
Q_DECLARE_METATYPE ( QKeySequence : : StandardKey ) ;
void tst_QQuickItem : : standardKeys_data ( )
{
QTest : : addColumn < QKeySequence : : StandardKey > ( " standardKey " ) ;
QTest : : addColumn < QKeySequence : : StandardKey > ( " contextProperty " ) ;
QTest : : addColumn < QEvent : : Type > ( " eventType " ) ;
QTest : : addColumn < bool > ( " pressed " ) ;
QTest : : addColumn < bool > ( " released " ) ;
QTest : : newRow ( " Press: Open " ) < < QKeySequence : : Open < < QKeySequence : : Open < < QEvent : : KeyPress < < true < < false ;
QTest : : newRow ( " Press: Close " ) < < QKeySequence : : Close < < QKeySequence : : Close < < QEvent : : KeyPress < < true < < false ;
QTest : : newRow ( " Press: Save " ) < < QKeySequence : : Save < < QKeySequence : : Save < < QEvent : : KeyPress < < true < < false ;
QTest : : newRow ( " Press: Quit " ) < < QKeySequence : : Quit < < QKeySequence : : Quit < < QEvent : : KeyPress < < true < < false ;
QTest : : newRow ( " Release: New " ) < < QKeySequence : : New < < QKeySequence : : New < < QEvent : : KeyRelease < < false < < true ;
QTest : : newRow ( " Release: Delete " ) < < QKeySequence : : Delete < < QKeySequence : : Delete < < QEvent : : KeyRelease < < false < < true ;
QTest : : newRow ( " Release: Undo " ) < < QKeySequence : : Undo < < QKeySequence : : Undo < < QEvent : : KeyRelease < < false < < true ;
QTest : : newRow ( " Release: Redo " ) < < QKeySequence : : Redo < < QKeySequence : : Redo < < QEvent : : KeyRelease < < false < < true ;
QTest : : newRow ( " Mismatch: Cut " ) < < QKeySequence : : Cut < < QKeySequence : : Copy < < QEvent : : KeyPress < < false < < false ;
QTest : : newRow ( " Mismatch: Copy " ) < < QKeySequence : : Copy < < QKeySequence : : Paste < < QEvent : : KeyPress < < false < < false ;
QTest : : newRow ( " Mismatch: Paste " ) < < QKeySequence : : Paste < < QKeySequence : : Cut < < QEvent : : KeyRelease < < false < < false ;
QTest : : newRow ( " Mismatch: Quit " ) < < QKeySequence : : Quit < < QKeySequence : : New < < QEvent : : KeyRelease < < false < < false ;
}
void tst_QQuickItem : : standardKeys ( )
{
QFETCH ( QKeySequence : : StandardKey , standardKey ) ;
QFETCH ( QKeySequence : : StandardKey , contextProperty ) ;
QFETCH ( QEvent : : Type , eventType ) ;
QFETCH ( bool , pressed ) ;
QFETCH ( bool , released ) ;
QKeySequence keySequence ( standardKey ) ;
if ( keySequence . isEmpty ( ) )
QSKIP ( " Undefined key sequence. " ) ;
QQuickView view ;
view . rootContext ( ) - > setContextProperty ( " standardKey " , contextProperty ) ;
view . setSource ( testFileUrl ( " standardkeys.qml " ) ) ;
view . show ( ) ;
view . requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( & view ) ) ;
QQuickItem * item = qobject_cast < QQuickItem * > ( view . rootObject ( ) ) ;
QVERIFY ( item ) ;
2020-10-21 07:25:01 +00:00
const int key = keySequence [ 0 ] . key ( ) ;
Qt : : KeyboardModifiers modifiers = keySequence [ 0 ] . keyboardModifiers ( ) ;
QKeyEvent keyEvent ( eventType , key , modifiers ) ;
2013-05-30 15:06:58 +00:00
QGuiApplication : : sendEvent ( & view , & keyEvent ) ;
QCOMPARE ( item - > property ( " pressed " ) . toBool ( ) , pressed ) ;
QCOMPARE ( item - > property ( " released " ) . toBool ( ) , released ) ;
}
2019-10-18 09:00:53 +00:00
# endif // QT_CONFIG(shortcut)
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : keysProcessingOrder ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setBaseSize ( QSize ( 240 , 320 ) ) ;
2011-04-27 12:13:26 +00:00
KeysTestObject * testObject = new KeysTestObject ;
2012-07-11 07:32:16 +00:00
window - > rootContext ( ) - > setContextProperty ( " keysTestObject " , testObject ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " keyspriority.qml " ) ) ;
window - > show ( ) ;
2012-10-19 16:05:01 +00:00
window - > requestActivate ( ) ;
2012-07-19 12:13:14 +00:00
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
KeyTestItem * testItem = qobject_cast < KeyTestItem * > ( window - > rootObject ( ) ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( testItem ) ;
2012-02-15 03:20:56 +00:00
QCOMPARE ( testItem - > property ( " priorityTest " ) . toInt ( ) , 0 ) ;
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_A , Qt : : NoModifier , " A " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_A ) ) ;
QCOMPARE ( testObject - > mText , QLatin1String ( " A " ) ) ;
QCOMPARE ( testObject - > mModifiers , int ( Qt : : NoModifier ) ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
testObject - > reset ( ) ;
testObject - > setProcessLast ( true ) ;
2012-02-15 03:20:56 +00:00
QCOMPARE ( testItem - > property ( " priorityTest " ) . toInt ( ) , 1 ) ;
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_A , Qt : : NoModifier , " A " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , 0 ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
testObject - > reset ( ) ;
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_B , Qt : : NoModifier , " B " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , int ( Qt : : Key_B ) ) ;
QCOMPARE ( testObject - > mText , QLatin1String ( " B " ) ) ;
QCOMPARE ( testObject - > mModifiers , int ( Qt : : NoModifier ) ) ;
QVERIFY ( ! key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
testObject - > reset ( ) ;
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyRelease , Qt : : Key_B , Qt : : NoModifier , " B " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QCOMPARE ( testObject - > mKey , 0 ) ;
QVERIFY ( key . isAccepted ( ) ) ;
}
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
delete testObject ;
}
2012-02-15 03:20:56 +00:00
void tst_QQuickItem : : keysim ( )
{
PlatformInputContext platformInputContext ;
QInputMethodPrivate * inputMethodPrivate = QInputMethodPrivate : : get ( qApp - > inputMethod ( ) ) ;
inputMethodPrivate - > testContext = & platformInputContext ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setBaseSize ( QSize ( 240 , 320 ) ) ;
2012-02-15 03:20:56 +00:00
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " keysim.qml " ) ) ;
window - > show ( ) ;
2012-10-19 16:05:01 +00:00
window - > requestActivate ( ) ;
2012-07-19 12:13:14 +00:00
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2012-02-15 03:20:56 +00:00
2012-07-11 07:32:16 +00:00
QVERIFY ( window - > rootObject ( ) ) ;
QVERIFY ( window - > rootObject ( ) - > hasFocus ( ) & & window - > rootObject ( ) - > hasActiveFocus ( ) ) ;
2012-02-15 03:20:56 +00:00
2012-07-11 07:32:16 +00:00
QQuickTextInput * input = window - > rootObject ( ) - > findChild < QQuickTextInput * > ( ) ;
2012-02-15 03:20:56 +00:00
QVERIFY ( input ) ;
QInputMethodEvent ev ( " Hello world! " , QList < QInputMethodEvent : : Attribute > ( ) ) ;
QGuiApplication : : sendEvent ( qGuiApp - > focusObject ( ) , & ev ) ;
QEXPECT_FAIL ( " " , " QTBUG-24280 " , Continue ) ;
QCOMPARE ( input - > text ( ) , QLatin1String ( " Hello world! " ) ) ;
2012-07-11 07:32:16 +00:00
delete window ;
2012-02-15 03:20:56 +00:00
}
2014-04-10 19:18:09 +00:00
void tst_QQuickItem : : keysForward ( )
{
QQuickView window ;
window . setBaseSize ( QSize ( 240 , 320 ) ) ;
window . setSource ( testFileUrl ( " keysforward.qml " ) ) ;
window . show ( ) ;
window . requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( & window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , & window ) ;
2014-04-10 19:18:09 +00:00
QQuickItem * rootItem = qobject_cast < QQuickItem * > ( window . rootObject ( ) ) ;
QVERIFY ( rootItem ) ;
QQuickItem * sourceItem = rootItem - > property ( " source " ) . value < QQuickItem * > ( ) ;
QVERIFY ( sourceItem ) ;
QQuickItem * primaryTarget = rootItem - > property ( " primaryTarget " ) . value < QQuickItem * > ( ) ;
QVERIFY ( primaryTarget ) ;
QQuickItem * secondaryTarget = rootItem - > property ( " secondaryTarget " ) . value < QQuickItem * > ( ) ;
QVERIFY ( secondaryTarget ) ;
// primary target accepts/consumes Key_P
QKeyEvent pressKeyP ( QEvent : : KeyPress , Qt : : Key_P , Qt : : NoModifier , " P " ) ;
QCoreApplication : : sendEvent ( sourceItem , & pressKeyP ) ;
QCOMPARE ( rootItem - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) ) ;
QCOMPARE ( sourceItem - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) ) ;
QCOMPARE ( primaryTarget - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P ) ;
QCOMPARE ( secondaryTarget - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P ) ;
QVERIFY ( pressKeyP . isAccepted ( ) ) ;
QKeyEvent releaseKeyP ( QEvent : : KeyRelease , Qt : : Key_P , Qt : : NoModifier , " P " ) ;
QCoreApplication : : sendEvent ( sourceItem , & releaseKeyP ) ;
QCOMPARE ( rootItem - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) ) ;
QCOMPARE ( sourceItem - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) ) ;
QCOMPARE ( primaryTarget - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P ) ;
QCOMPARE ( secondaryTarget - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P ) ;
QVERIFY ( releaseKeyP . isAccepted ( ) ) ;
// secondary target accepts/consumes Key_S
QKeyEvent pressKeyS ( QEvent : : KeyPress , Qt : : Key_S , Qt : : NoModifier , " S " ) ;
QCoreApplication : : sendEvent ( sourceItem , & pressKeyS ) ;
QCOMPARE ( rootItem - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) ) ;
QCOMPARE ( sourceItem - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) ) ;
QCOMPARE ( primaryTarget - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P ) ;
QCOMPARE ( secondaryTarget - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P < < Qt : : Key_S ) ;
QVERIFY ( pressKeyS . isAccepted ( ) ) ;
QKeyEvent releaseKeyS ( QEvent : : KeyRelease , Qt : : Key_S , Qt : : NoModifier , " S " ) ;
QCoreApplication : : sendEvent ( sourceItem , & releaseKeyS ) ;
QCOMPARE ( rootItem - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) ) ;
QCOMPARE ( sourceItem - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) ) ;
QCOMPARE ( primaryTarget - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P ) ;
QCOMPARE ( secondaryTarget - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P < < Qt : : Key_S ) ;
QVERIFY ( releaseKeyS . isAccepted ( ) ) ;
// neither target accepts/consumes Key_Q
QKeyEvent pressKeyQ ( QEvent : : KeyPress , Qt : : Key_Q , Qt : : NoModifier , " Q " ) ;
QCoreApplication : : sendEvent ( sourceItem , & pressKeyQ ) ;
QCOMPARE ( rootItem - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) ) ;
QCOMPARE ( sourceItem - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_Q ) ;
QCOMPARE ( primaryTarget - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P < < Qt : : Key_Q ) ;
QCOMPARE ( secondaryTarget - > property ( " pressedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P < < Qt : : Key_S < < Qt : : Key_Q ) ;
QVERIFY ( ! pressKeyQ . isAccepted ( ) ) ;
QKeyEvent releaseKeyQ ( QEvent : : KeyRelease , Qt : : Key_Q , Qt : : NoModifier , " Q " ) ;
QCoreApplication : : sendEvent ( sourceItem , & releaseKeyQ ) ;
QCOMPARE ( rootItem - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) ) ;
QCOMPARE ( sourceItem - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_Q ) ;
QCOMPARE ( primaryTarget - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P < < Qt : : Key_Q ) ;
QCOMPARE ( secondaryTarget - > property ( " releasedKeys " ) . toList ( ) , QVariantList ( ) < < Qt : : Key_P < < Qt : : Key_S < < Qt : : Key_Q ) ;
QVERIFY ( ! releaseKeyQ . isAccepted ( ) ) ;
}
2011-10-14 08:51:42 +00:00
QQuickItemPrivate * childPrivate ( QQuickItem * rootItem , const char * itemString )
2011-04-27 12:13:26 +00:00
{
2011-10-14 08:51:42 +00:00
QQuickItem * item = findItem < QQuickItem > ( rootItem , QString ( QLatin1String ( itemString ) ) ) ;
QQuickItemPrivate * itemPrivate = QQuickItemPrivate : : get ( item ) ;
2011-04-27 12:13:26 +00:00
return itemPrivate ;
}
2011-10-14 08:51:42 +00:00
QVariant childProperty ( QQuickItem * rootItem , const char * itemString , const char * property )
2011-04-27 12:13:26 +00:00
{
2011-10-14 08:51:42 +00:00
QQuickItem * item = findItem < QQuickItem > ( rootItem , QString ( QLatin1String ( itemString ) ) ) ;
2011-04-27 12:13:26 +00:00
return item - > property ( property ) ;
}
2011-10-14 08:51:42 +00:00
bool anchorsMirrored ( QQuickItem * rootItem , const char * itemString )
2011-04-27 12:13:26 +00:00
{
2011-10-14 08:51:42 +00:00
QQuickItem * item = findItem < QQuickItem > ( rootItem , QString ( QLatin1String ( itemString ) ) ) ;
QQuickItemPrivate * itemPrivate = QQuickItemPrivate : : get ( item ) ;
2011-04-27 12:13:26 +00:00
return itemPrivate - > anchors ( ) - > mirrored ( ) ;
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : layoutMirroring ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " layoutmirroring.qml " ) ) ;
window - > show ( ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * rootItem = qobject_cast < QQuickItem * > ( window - > rootObject ( ) ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( rootItem ) ;
2011-10-14 08:51:42 +00:00
QQuickItemPrivate * rootPrivate = QQuickItemPrivate : : get ( rootItem ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( rootPrivate ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored1 " ) - > effectiveLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored2 " ) - > effectiveLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored1 " ) - > effectiveLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored2 " ) - > effectiveLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror1 " ) - > effectiveLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror2 " ) - > effectiveLayoutMirror , true ) ;
QCOMPARE ( anchorsMirrored ( rootItem , " mirrored1 " ) , true ) ;
QCOMPARE ( anchorsMirrored ( rootItem , " mirrored2 " ) , true ) ;
QCOMPARE ( anchorsMirrored ( rootItem , " notMirrored1 " ) , false ) ;
QCOMPARE ( anchorsMirrored ( rootItem , " notMirrored2 " ) , false ) ;
QCOMPARE ( anchorsMirrored ( rootItem , " inheritedMirror1 " ) , true ) ;
QCOMPARE ( anchorsMirrored ( rootItem , " inheritedMirror2 " ) , true ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored1 " ) - > inheritedLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored2 " ) - > inheritedLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored1 " ) - > inheritedLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored2 " ) - > inheritedLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror1 " ) - > inheritedLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror2 " ) - > inheritedLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored1 " ) - > isMirrorImplicit , false ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored2 " ) - > isMirrorImplicit , false ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored1 " ) - > isMirrorImplicit , false ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored2 " ) - > isMirrorImplicit , true ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror1 " ) - > isMirrorImplicit , true ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror2 " ) - > isMirrorImplicit , true ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored1 " ) - > inheritMirrorFromParent , true ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored2 " ) - > inheritMirrorFromParent , false ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored1 " ) - > inheritMirrorFromParent , true ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored2 " ) - > inheritMirrorFromParent , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror1 " ) - > inheritMirrorFromParent , true ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror2 " ) - > inheritMirrorFromParent , true ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored1 " ) - > inheritMirrorFromItem , true ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored2 " ) - > inheritMirrorFromItem , false ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored1 " ) - > inheritMirrorFromItem , false ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored2 " ) - > inheritMirrorFromItem , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror1 " ) - > inheritMirrorFromItem , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror2 " ) - > inheritMirrorFromItem , false ) ;
// load dynamic content using Loader that needs to inherit mirroring
rootItem - > setProperty ( " state " , " newContent " ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored3 " ) - > effectiveLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror3 " ) - > effectiveLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored3 " ) - > inheritedLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror3 " ) - > inheritedLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored3 " ) - > isMirrorImplicit , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror3 " ) - > isMirrorImplicit , true ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored3 " ) - > inheritMirrorFromParent , true ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror3 " ) - > inheritMirrorFromParent , true ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored3 " ) - > inheritMirrorFromItem , false ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored3 " ) - > inheritMirrorFromItem , false ) ;
// disable inheritance
rootItem - > setProperty ( " childrenInherit " , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror1 " ) - > effectiveLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror2 " ) - > effectiveLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored1 " ) - > effectiveLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored1 " ) - > effectiveLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror1 " ) - > inheritedLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror2 " ) - > inheritedLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored1 " ) - > inheritedLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored1 " ) - > inheritedLayoutMirror , false ) ;
// re-enable inheritance
rootItem - > setProperty ( " childrenInherit " , true ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror1 " ) - > effectiveLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror2 " ) - > effectiveLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored1 " ) - > effectiveLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored1 " ) - > effectiveLayoutMirror , false ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror1 " ) - > inheritedLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " inheritedMirror2 " ) - > inheritedLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " mirrored1 " ) - > inheritedLayoutMirror , true ) ;
QCOMPARE ( childPrivate ( rootItem , " notMirrored1 " ) - > inheritedLayoutMirror , true ) ;
2011-10-17 11:03:58 +00:00
2011-04-27 12:13:26 +00:00
//
// dynamic parenting
//
2011-10-14 08:51:42 +00:00
QQuickItem * parentItem1 = new QQuickItem ( ) ;
QQuickItemPrivate : : get ( parentItem1 ) - > effectiveLayoutMirror = true ; // LayoutMirroring.enabled: true
QQuickItemPrivate : : get ( parentItem1 ) - > isMirrorImplicit = false ;
QQuickItemPrivate : : get ( parentItem1 ) - > inheritMirrorFromItem = true ; // LayoutMirroring.childrenInherit: true
QQuickItemPrivate : : get ( parentItem1 ) - > resolveLayoutMirror ( ) ;
2011-04-27 12:13:26 +00:00
// inherit in constructor
2011-10-14 08:51:42 +00:00
QQuickItem * childItem1 = new QQuickItem ( parentItem1 ) ;
QCOMPARE ( QQuickItemPrivate : : get ( childItem1 ) - > effectiveLayoutMirror , true ) ;
QCOMPARE ( QQuickItemPrivate : : get ( childItem1 ) - > inheritMirrorFromParent , true ) ;
2011-04-27 12:13:26 +00:00
// inherit through a parent change
2011-10-14 08:51:42 +00:00
QQuickItem * childItem2 = new QQuickItem ( ) ;
QCOMPARE ( QQuickItemPrivate : : get ( childItem2 ) - > effectiveLayoutMirror , false ) ;
QCOMPARE ( QQuickItemPrivate : : get ( childItem2 ) - > inheritMirrorFromParent , false ) ;
2011-04-27 12:13:26 +00:00
childItem2 - > setParentItem ( parentItem1 ) ;
2011-10-14 08:51:42 +00:00
QCOMPARE ( QQuickItemPrivate : : get ( childItem2 ) - > effectiveLayoutMirror , true ) ;
QCOMPARE ( QQuickItemPrivate : : get ( childItem2 ) - > inheritMirrorFromParent , true ) ;
2011-04-27 12:13:26 +00:00
// stop inherting through a parent change
2011-10-14 08:51:42 +00:00
QQuickItem * parentItem2 = new QQuickItem ( ) ;
QQuickItemPrivate : : get ( parentItem2 ) - > effectiveLayoutMirror = true ; // LayoutMirroring.enabled: true
QQuickItemPrivate : : get ( parentItem2 ) - > resolveLayoutMirror ( ) ;
2011-04-27 12:13:26 +00:00
childItem2 - > setParentItem ( parentItem2 ) ;
2011-10-14 08:51:42 +00:00
QCOMPARE ( QQuickItemPrivate : : get ( childItem2 ) - > effectiveLayoutMirror , false ) ;
QCOMPARE ( QQuickItemPrivate : : get ( childItem2 ) - > inheritMirrorFromParent , false ) ;
2011-10-17 11:03:58 +00:00
2011-04-27 12:13:26 +00:00
delete parentItem1 ;
delete parentItem2 ;
}
2016-08-26 08:58:32 +00:00
void tst_QQuickItem : : layoutMirroringWindow ( )
{
QQmlComponent component ( & engine ) ;
component . loadUrl ( testFileUrl ( " layoutmirroring_window.qml " ) ) ;
QScopedPointer < QObject > object ( component . create ( ) ) ;
QQuickWindow * window = qobject_cast < QQuickWindow * > ( object . data ( ) ) ;
QVERIFY ( window ) ;
window - > show ( ) ;
QQuickItemPrivate * content = QQuickItemPrivate : : get ( window - > contentItem ( ) ) ;
QCOMPARE ( content - > effectiveLayoutMirror , true ) ;
QCOMPARE ( content - > inheritedLayoutMirror , true ) ;
QCOMPARE ( content - > isMirrorImplicit , false ) ;
QCOMPARE ( content - > inheritMirrorFromParent , true ) ;
QCOMPARE ( content - > inheritMirrorFromItem , true ) ;
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : layoutMirroringIllegalParent ( )
2011-04-27 12:13:26 +00:00
{
2012-02-16 04:43:03 +00:00
QQmlComponent component ( & engine ) ;
2011-04-27 12:13:26 +00:00
component . setData ( " import QtQuick 2.0; QtObject { LayoutMirroring.enabled: true; LayoutMirroring.childrenInherit: true } " , QUrl : : fromLocalFile ( " " ) ) ;
2016-08-26 08:58:32 +00:00
QTest : : ignoreMessage ( QtWarningMsg , " <Unknown File>:1:21: QML QtObject: LayoutDirection attached property only works with Items and Windows " ) ;
2011-04-27 12:13:26 +00:00
QObject * object = component . create ( ) ;
2018-02-21 09:41:54 +00:00
QVERIFY ( object ! = nullptr ) ;
2011-04-27 12:13:26 +00:00
}
2013-10-01 19:09:52 +00:00
void tst_QQuickItem : : keyNavigation_data ( )
{
QTest : : addColumn < QString > ( " source " ) ;
QTest : : newRow ( " KeyNavigation " ) < < QStringLiteral ( " keynavigationtest.qml " ) ;
QTest : : newRow ( " KeyNavigation_FocusScope " ) < < QStringLiteral ( " keynavigationtest_focusscope.qml " ) ;
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : keyNavigation ( )
2011-04-27 12:13:26 +00:00
{
2013-10-01 19:09:52 +00:00
QFETCH ( QString , source ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setBaseSize ( QSize ( 240 , 320 ) ) ;
2011-04-27 12:13:26 +00:00
2013-10-01 19:09:52 +00:00
window - > setSource ( testFileUrl ( source ) ) ;
2012-07-11 07:32:16 +00:00
window - > show ( ) ;
2012-10-19 16:05:01 +00:00
window - > requestActivate ( ) ;
2012-07-19 12:13:14 +00:00
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
QVariant result ;
2012-07-11 07:32:16 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( window - > rootObject ( ) , " verify " ,
2011-04-27 12:13:26 +00:00
Q_RETURN_ARG ( QVariant , result ) ) ) ;
QVERIFY ( result . toBool ( ) ) ;
// right
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Right , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item2 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// down
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Down , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item4 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// left
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Left , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item3 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// up
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Up , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// tab
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item2 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// backtab
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Backtab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : keyNavigation_RightToLeft ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setBaseSize ( QSize ( 240 , 320 ) ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " keynavigationtest.qml " ) ) ;
window - > show ( ) ;
2012-10-19 16:05:01 +00:00
window - > requestActivate ( ) ;
2012-07-19 12:13:14 +00:00
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * rootItem = qobject_cast < QQuickItem * > ( window - > rootObject ( ) ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( rootItem ) ;
2011-10-14 08:51:42 +00:00
QQuickItemPrivate * rootItemPrivate = QQuickItemPrivate : : get ( rootItem ) ;
2011-04-27 12:13:26 +00:00
rootItemPrivate - > effectiveLayoutMirror = true ; // LayoutMirroring.mirror: true
rootItemPrivate - > isMirrorImplicit = false ;
rootItemPrivate - > inheritMirrorFromItem = true ; // LayoutMirroring.inherit: true
rootItemPrivate - > resolveLayoutMirror ( ) ;
QEvent wa ( QEvent : : WindowActivate ) ;
2012-07-11 07:32:16 +00:00
QGuiApplication : : sendEvent ( window , & wa ) ;
2011-04-27 12:13:26 +00:00
QFocusEvent fe ( QEvent : : FocusIn ) ;
2012-07-11 07:32:16 +00:00
QGuiApplication : : sendEvent ( window , & fe ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
QVariant result ;
2012-07-11 07:32:16 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( window - > rootObject ( ) , " verify " ,
2011-04-27 12:13:26 +00:00
Q_RETURN_ARG ( QVariant , result ) ) ) ;
QVERIFY ( result . toBool ( ) ) ;
// right
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Left , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item2 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// left
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Right , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : keyNavigation_skipNotVisible ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setBaseSize ( QSize ( 240 , 320 ) ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " keynavigationtest.qml " ) ) ;
window - > show ( ) ;
2012-10-19 16:05:01 +00:00
window - > requestActivate ( ) ;
2012-07-19 12:13:14 +00:00
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
// Set item 2 to not visible
2012-07-11 07:32:16 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item2 " ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( item ) ;
item - > setVisible ( false ) ;
QVERIFY ( ! item - > isVisible ( ) ) ;
// right
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Right , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// tab
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item3 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// backtab
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Backtab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
//Set item 3 to not visible
2012-07-11 07:32:16 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item3 " ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( item ) ;
item - > setVisible ( false ) ;
QVERIFY ( ! item - > isVisible ( ) ) ;
// tab
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item4 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// backtab
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Backtab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : keyNavigation_implicitSetting ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setBaseSize ( QSize ( 240 , 320 ) ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " keynavigationtest_implicit.qml " ) ) ;
window - > show ( ) ;
2012-10-19 16:05:01 +00:00
window - > requestActivate ( ) ;
2012-07-19 12:13:14 +00:00
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2011-04-27 12:13:26 +00:00
QEvent wa ( QEvent : : WindowActivate ) ;
2012-07-11 07:32:16 +00:00
QGuiApplication : : sendEvent ( window , & wa ) ;
2011-04-27 12:13:26 +00:00
QFocusEvent fe ( QEvent : : FocusIn ) ;
2012-07-11 07:32:16 +00:00
QGuiApplication : : sendEvent ( window , & fe ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
QVariant result ;
2012-07-11 07:32:16 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( window - > rootObject ( ) , " verify " ,
2011-04-27 12:13:26 +00:00
Q_RETURN_ARG ( QVariant , result ) ) ) ;
QVERIFY ( result . toBool ( ) ) ;
// right
2020-11-11 12:51:35 +00:00
{
QKeyEvent key ( QEvent : : KeyPress , Qt : : Key_Right , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item2 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// back to item1
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Left , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// down
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Down , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item3 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// move to item4
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Right , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item4 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// left
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Left , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item3 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// back to item4
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Right , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item4 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// up
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Up , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item2 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// back to item4
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Down , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item4 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// tab
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// back to item4
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Backtab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item4 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
// backtab
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Backtab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
2011-04-27 12:13:26 +00:00
2020-11-11 12:51:35 +00:00
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item3 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
}
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
2019-05-19 16:16:34 +00:00
// QTBUG-75399
void tst_QQuickItem : : keyNavigation_implicitDestroy ( )
{
QQuickView view ;
view . setSource ( testFileUrl ( " keynavigationtest_implicitDestroy.qml " ) ) ;
view . show ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( & view ) ) ;
QQuickItem * root = view . rootObject ( ) ;
QVERIFY ( QMetaObject : : invokeMethod ( root , " createImplicitKeyNavigation " ) ) ;
// process events is necessary to trigger upcoming memory access violation
QTest : : qWait ( 0 ) ;
QVERIFY ( root - > hasActiveFocus ( ) ) ;
QKeyEvent keyPress = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Down , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( & view , & keyPress ) ; // <-- access violation happens here
// this should fail the test, even if the access violation does not occur
QVERIFY ( ! keyPress . isAccepted ( ) ) ;
}
2014-05-02 13:18:25 +00:00
void tst_QQuickItem : : keyNavigation_focusReason ( )
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2014-05-02 13:18:25 +00:00
window - > setBaseSize ( QSize ( 240 , 320 ) ) ;
FocusEventFilter focusEventFilter ;
window - > setSource ( testFileUrl ( " keynavigationtest.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2014-05-02 13:18:25 +00:00
// install event filter on first item
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
item - > installEventFilter ( & focusEventFilter ) ;
//install event filter on second item
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item2 " ) ;
QVERIFY ( item ) ;
item - > installEventFilter ( & focusEventFilter ) ;
//install event filter on third item
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item3 " ) ;
QVERIFY ( item ) ;
item - > installEventFilter ( & focusEventFilter ) ;
//install event filter on last item
item = findItem < QQuickItem > ( window - > rootObject ( ) , " item4 " ) ;
QVERIFY ( item ) ;
item - > installEventFilter ( & focusEventFilter ) ;
// tab
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Tab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
QCOMPARE ( focusEventFilter . lastFocusReason , Qt : : TabFocusReason ) ;
}
2014-05-02 13:18:25 +00:00
// backtab
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Backtab , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
QCOMPARE ( focusEventFilter . lastFocusReason , Qt : : BacktabFocusReason ) ;
}
2014-05-02 13:18:25 +00:00
2015-10-14 09:00:35 +00:00
// right - it's also one kind of key navigation
2020-11-11 12:51:35 +00:00
{
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Right , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
QCOMPARE ( focusEventFilter . lastFocusReason , Qt : : TabFocusReason ) ;
2015-10-14 09:00:35 +00:00
2020-11-11 12:51:35 +00:00
item - > setFocus ( true , Qt : : OtherFocusReason ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
QCOMPARE ( focusEventFilter . lastFocusReason , Qt : : OtherFocusReason ) ;
}
2014-05-02 13:18:25 +00:00
delete window ;
}
2015-10-12 08:24:38 +00:00
void tst_QQuickItem : : keyNavigation_loop ( )
{
// QTBUG-47229
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2015-10-12 08:24:38 +00:00
window - > setBaseSize ( QSize ( 240 , 320 ) ) ;
window - > setSource ( testFileUrl ( " keynavigationtest_loop.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " item1 " ) ;
QVERIFY ( item ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
QKeyEvent key = QKeyEvent ( QEvent : : KeyPress , Qt : : Key_Down , Qt : : NoModifier , " " , false , 1 ) ;
QGuiApplication : : sendEvent ( window , & key ) ;
QVERIFY ( key . isAccepted ( ) ) ;
QVERIFY ( item - > hasActiveFocus ( ) ) ;
delete window ;
}
2020-04-20 17:42:56 +00:00
void tst_QQuickItem : : keyNavigation_repeater ( )
{
// QTBUG-83356
QScopedPointer < QQuickView > window ( new QQuickView ( ) ) ;
window - > setBaseSize ( QSize ( 240 , 320 ) ) ;
window - > setSource ( testFileUrl ( " keynavigationtest_repeater.qml " ) ) ;
window - > show ( ) ;
window - > requestActivate ( ) ;
QVariant result ;
QVERIFY ( QMetaObject : : invokeMethod ( window - > rootObject ( ) , " verify " ,
Q_RETURN_ARG ( QVariant , result ) ) ) ;
QVERIFY ( result . toBool ( ) ) ;
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : smooth ( )
2011-04-27 12:13:26 +00:00
{
2012-02-16 04:43:03 +00:00
QQmlComponent component ( & engine ) ;
2011-04-27 12:13:26 +00:00
component . setData ( " import QtQuick 2.0; Item { smooth: false; } " , QUrl : : fromLocalFile ( " " ) ) ;
2011-10-14 08:51:42 +00:00
QQuickItem * item = qobject_cast < QQuickItem * > ( component . create ( ) ) ;
2011-04-27 12:13:26 +00:00
QSignalSpy spy ( item , SIGNAL ( smoothChanged ( bool ) ) ) ;
QVERIFY ( item ) ;
QVERIFY ( ! item - > smooth ( ) ) ;
item - > setSmooth ( true ) ;
QVERIFY ( item - > smooth ( ) ) ;
QCOMPARE ( spy . count ( ) , 1 ) ;
QList < QVariant > arguments = spy . first ( ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( arguments . count ( ) , 1 ) ;
2015-07-24 11:39:05 +00:00
QVERIFY ( arguments . at ( 0 ) . toBool ( ) ) ;
2011-04-27 12:13:26 +00:00
item - > setSmooth ( true ) ;
QCOMPARE ( spy . count ( ) , 1 ) ;
item - > setSmooth ( false ) ;
QVERIFY ( ! item - > smooth ( ) ) ;
QCOMPARE ( spy . count ( ) , 2 ) ;
item - > setSmooth ( false ) ;
QCOMPARE ( spy . count ( ) , 2 ) ;
delete item ;
}
2012-07-24 03:40:55 +00:00
void tst_QQuickItem : : antialiasing ( )
{
QQmlComponent component ( & engine ) ;
component . setData ( " import QtQuick 2.0; Item { antialiasing: false; } " , QUrl : : fromLocalFile ( " " ) ) ;
QQuickItem * item = qobject_cast < QQuickItem * > ( component . create ( ) ) ;
QSignalSpy spy ( item , SIGNAL ( antialiasingChanged ( bool ) ) ) ;
QVERIFY ( item ) ;
QVERIFY ( ! item - > antialiasing ( ) ) ;
item - > setAntialiasing ( true ) ;
QVERIFY ( item - > antialiasing ( ) ) ;
QCOMPARE ( spy . count ( ) , 1 ) ;
QList < QVariant > arguments = spy . first ( ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( arguments . count ( ) , 1 ) ;
2015-07-24 11:39:05 +00:00
QVERIFY ( arguments . at ( 0 ) . toBool ( ) ) ;
2012-07-24 03:40:55 +00:00
item - > setAntialiasing ( true ) ;
QCOMPARE ( spy . count ( ) , 1 ) ;
item - > setAntialiasing ( false ) ;
QVERIFY ( ! item - > antialiasing ( ) ) ;
QCOMPARE ( spy . count ( ) , 2 ) ;
item - > setAntialiasing ( false ) ;
QCOMPARE ( spy . count ( ) , 2 ) ;
delete item ;
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : clip ( )
2011-04-27 12:13:26 +00:00
{
2012-02-16 04:43:03 +00:00
QQmlComponent component ( & engine ) ;
2011-04-27 12:13:26 +00:00
component . setData ( " import QtQuick 2.0 \n Item { clip: false \n } " , QUrl : : fromLocalFile ( " " ) ) ;
2011-10-14 08:51:42 +00:00
QQuickItem * item = qobject_cast < QQuickItem * > ( component . create ( ) ) ;
2011-04-27 12:13:26 +00:00
QSignalSpy spy ( item , SIGNAL ( clipChanged ( bool ) ) ) ;
QVERIFY ( item ) ;
QVERIFY ( ! item - > clip ( ) ) ;
item - > setClip ( true ) ;
QVERIFY ( item - > clip ( ) ) ;
QList < QVariant > arguments = spy . first ( ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( arguments . count ( ) , 1 ) ;
2015-07-24 11:39:05 +00:00
QVERIFY ( arguments . at ( 0 ) . toBool ( ) ) ;
2011-04-27 12:13:26 +00:00
QCOMPARE ( spy . count ( ) , 1 ) ;
item - > setClip ( true ) ;
QCOMPARE ( spy . count ( ) , 1 ) ;
item - > setClip ( false ) ;
QVERIFY ( ! item - > clip ( ) ) ;
QCOMPARE ( spy . count ( ) , 2 ) ;
item - > setClip ( false ) ;
QCOMPARE ( spy . count ( ) , 2 ) ;
delete item ;
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : mapCoordinates ( )
2011-04-27 12:13:26 +00:00
{
QFETCH ( int , x ) ;
QFETCH ( int , y ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setBaseSize ( QSize ( 300 , 300 ) ) ;
window - > setSource ( testFileUrl ( " mapCoordinates.qml " ) ) ;
window - > show ( ) ;
2011-04-27 12:13:26 +00:00
qApp - > processEvents ( ) ;
2012-07-11 07:32:16 +00:00
QQuickItem * root = qobject_cast < QQuickItem * > ( window - > rootObject ( ) ) ;
2018-02-21 09:41:54 +00:00
QVERIFY ( root ! = nullptr ) ;
2012-07-11 07:32:16 +00:00
QQuickItem * a = findItem < QQuickItem > ( window - > rootObject ( ) , " itemA " ) ;
2018-02-21 09:41:54 +00:00
QVERIFY ( a ! = nullptr ) ;
2012-07-11 07:32:16 +00:00
QQuickItem * b = findItem < QQuickItem > ( window - > rootObject ( ) , " itemB " ) ;
2018-02-21 09:41:54 +00:00
QVERIFY ( b ! = nullptr ) ;
2011-04-27 12:13:26 +00:00
QVariant result ;
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAToB " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
2011-10-14 08:51:42 +00:00
QCOMPARE ( result . value < QPointF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapToItem ( b , QPointF ( x , y ) ) ) ;
2011-04-27 12:13:26 +00:00
2020-01-15 09:45:30 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAToBPoint " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
QCOMPARE ( result . value < QPointF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapToItem ( b , QPointF ( x , y ) ) ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAFromB " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
2011-10-14 08:51:42 +00:00
QCOMPARE ( result . value < QPointF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapFromItem ( b , QPointF ( x , y ) ) ) ;
2011-04-27 12:13:26 +00:00
2020-01-15 09:45:30 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAFromBPoint " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
QCOMPARE ( result . value < QPointF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapFromItem ( b , QPointF ( x , y ) ) ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAToNull " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
2011-10-14 08:51:42 +00:00
QCOMPARE ( result . value < QPointF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapToScene ( QPointF ( x , y ) ) ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAFromNull " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
2011-10-14 08:51:42 +00:00
QCOMPARE ( result . value < QPointF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapFromScene ( QPointF ( x , y ) ) ) ;
2011-04-27 12:13:26 +00:00
Item: sort out mapFromGlobal() and mapToGlobal()
The commit message of 08327da, and the change log of Qt 5.7.0 promised
that mapFromGlobal() and mapToGlobal() were available in QML. But since
the revision 7 of QQuickItem was not registered, this was not entirely
true.
Due to a little quirk in the QML engine's handling of revisioned
methods, mapFromGlobal() and mapToGlobal() were only accessible via
an identifier or property, but not directly:
// works
MouseArea { id: ma; onClicked: console.log(ma.mapToGlobal(Qt.point(mouse.x, mouse.y))) }
// ReferenceError: mapToGlobal is not defined
MouseArea { onClicked: console.log(mapToGlobal(Qt.point(mouse.x, mouse.y))) }
Furhermore, this is inconsistent with how mapFromItem() and mapToItem()
are exposed to QML. Even though the C++ versions of these methods take
QPointF and QRectF, the QML versions take 2-4 number specifying x, y,
width and height:
object mapFromItem(Item item, real x, real y)
object mapFromItem(Item item, real x, real y, real width, real height)
object mapToItem(Item item, real x, real y)
object mapToItem(Item item, real x, real y, real width, real height)
Therefore the signature of mapFromGlobal() and mapToGlobal() should be:
object mapFromGlobal(real x, real y)
object mapToGlobal(real x, real y)
This change implements the QML versions of these methods using
QQmlV4Function, and adds the missing documentation for the QML API.
NOTE: This is QML-only API.
Change-Id: I2ced4836d274c7d1e644ea29fc25dbdd2045001b
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
2016-09-28 09:00:40 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAToGlobal " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
QCOMPARE ( result . value < QPointF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapToGlobal ( QPointF ( x , y ) ) ) ;
2020-01-16 10:13:36 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAToGlobalPoint " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
QCOMPARE ( result . value < QPointF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapToGlobal ( QPointF ( x , y ) ) ) ;
Item: sort out mapFromGlobal() and mapToGlobal()
The commit message of 08327da, and the change log of Qt 5.7.0 promised
that mapFromGlobal() and mapToGlobal() were available in QML. But since
the revision 7 of QQuickItem was not registered, this was not entirely
true.
Due to a little quirk in the QML engine's handling of revisioned
methods, mapFromGlobal() and mapToGlobal() were only accessible via
an identifier or property, but not directly:
// works
MouseArea { id: ma; onClicked: console.log(ma.mapToGlobal(Qt.point(mouse.x, mouse.y))) }
// ReferenceError: mapToGlobal is not defined
MouseArea { onClicked: console.log(mapToGlobal(Qt.point(mouse.x, mouse.y))) }
Furhermore, this is inconsistent with how mapFromItem() and mapToItem()
are exposed to QML. Even though the C++ versions of these methods take
QPointF and QRectF, the QML versions take 2-4 number specifying x, y,
width and height:
object mapFromItem(Item item, real x, real y)
object mapFromItem(Item item, real x, real y, real width, real height)
object mapToItem(Item item, real x, real y)
object mapToItem(Item item, real x, real y, real width, real height)
Therefore the signature of mapFromGlobal() and mapToGlobal() should be:
object mapFromGlobal(real x, real y)
object mapToGlobal(real x, real y)
This change implements the QML versions of these methods using
QQmlV4Function, and adds the missing documentation for the QML API.
NOTE: This is QML-only API.
Change-Id: I2ced4836d274c7d1e644ea29fc25dbdd2045001b
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
2016-09-28 09:00:40 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAFromGlobal " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
QCOMPARE ( result . value < QPointF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapFromGlobal ( QPointF ( x , y ) ) ) ;
2020-01-16 10:13:36 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAFromGlobalPoint " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
QCOMPARE ( result . value < QPointF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapFromGlobal ( QPointF ( x , y ) ) ) ;
2018-04-09 13:41:02 +00:00
// for orphans we are primarily testing that we don't crash.
// when orphaned the final position is the original position of the item translated by x,y
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapOrphanToGlobal " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
QCOMPARE ( result . value < QPointF > ( ) , QPointF ( 150 , 150 ) + QPointF ( x , y ) ) ;
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapOrphanFromGlobal " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
QCOMPARE ( result . value < QPointF > ( ) , - QPointF ( 150 , 150 ) + QPointF ( x , y ) ) ;
2016-01-20 07:44:43 +00:00
QString warning1 = testFileUrl ( " mapCoordinates.qml " ) . toString ( ) + " :35:5: QML Item: mapToItem() given argument \" 1122 \" which is neither null nor an Item " ;
QString warning2 = testFileUrl ( " mapCoordinates.qml " ) . toString ( ) + " :35:5: QML Item: mapFromItem() given argument \" 1122 \" which is neither null nor an Item " ;
2011-04-27 12:13:26 +00:00
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning1 ) ) ;
QVERIFY ( QMetaObject : : invokeMethod ( root , " checkMapAToInvalid " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
QVERIFY ( result . toBool ( ) ) ;
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning2 ) ) ;
QVERIFY ( QMetaObject : : invokeMethod ( root , " checkMapAFromInvalid " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) ) ) ;
QVERIFY ( result . toBool ( ) ) ;
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : mapCoordinates_data ( )
2011-04-27 12:13:26 +00:00
{
QTest : : addColumn < int > ( " x " ) ;
QTest : : addColumn < int > ( " y " ) ;
for ( int i = - 20 ; i < = 20 ; i + = 10 )
QTest : : newRow ( QTest : : toString ( i ) ) < < i < < i ;
}
2012-05-16 02:59:32 +00:00
void tst_QQuickItem : : mapCoordinatesRect ( )
{
QFETCH ( int , x ) ;
QFETCH ( int , y ) ;
QFETCH ( int , width ) ;
QFETCH ( int , height ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setBaseSize ( QSize ( 300 , 300 ) ) ;
window - > setSource ( testFileUrl ( " mapCoordinatesRect.qml " ) ) ;
window - > show ( ) ;
2012-05-16 02:59:32 +00:00
qApp - > processEvents ( ) ;
2012-07-11 07:32:16 +00:00
QQuickItem * root = qobject_cast < QQuickItem * > ( window - > rootObject ( ) ) ;
2018-02-21 09:41:54 +00:00
QVERIFY ( root ! = nullptr ) ;
2012-07-11 07:32:16 +00:00
QQuickItem * a = findItem < QQuickItem > ( window - > rootObject ( ) , " itemA " ) ;
2018-02-21 09:41:54 +00:00
QVERIFY ( a ! = nullptr ) ;
2012-07-11 07:32:16 +00:00
QQuickItem * b = findItem < QQuickItem > ( window - > rootObject ( ) , " itemB " ) ;
2018-02-21 09:41:54 +00:00
QVERIFY ( b ! = nullptr ) ;
2012-05-16 02:59:32 +00:00
QVariant result ;
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAToB " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) , Q_ARG ( QVariant , width ) , Q_ARG ( QVariant , height ) ) ) ;
QCOMPARE ( result . value < QRectF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapRectToItem ( b , QRectF ( x , y , width , height ) ) ) ;
2020-01-15 09:45:30 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAToBRect " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) , Q_ARG ( QVariant , width ) , Q_ARG ( QVariant , height ) ) ) ;
QCOMPARE ( result . value < QRectF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapRectToItem ( b , QRectF ( x , y , width , height ) ) ) ;
2012-05-16 02:59:32 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAFromB " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) , Q_ARG ( QVariant , width ) , Q_ARG ( QVariant , height ) ) ) ;
QCOMPARE ( result . value < QRectF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapRectFromItem ( b , QRectF ( x , y , width , height ) ) ) ;
2020-01-15 09:45:30 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAFromBRect " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) , Q_ARG ( QVariant , width ) , Q_ARG ( QVariant , height ) ) ) ;
QCOMPARE ( result . value < QRectF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapRectFromItem ( b , QRectF ( x , y , width , height ) ) ) ;
2012-05-16 02:59:32 +00:00
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAToNull " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) , Q_ARG ( QVariant , width ) , Q_ARG ( QVariant , height ) ) ) ;
QCOMPARE ( result . value < QRectF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapRectToScene ( QRectF ( x , y , width , height ) ) ) ;
QVERIFY ( QMetaObject : : invokeMethod ( root , " mapAFromNull " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) , Q_ARG ( QVariant , width ) , Q_ARG ( QVariant , height ) ) ) ;
QCOMPARE ( result . value < QRectF > ( ) , qobject_cast < QQuickItem * > ( a ) - > mapRectFromScene ( QRectF ( x , y , width , height ) ) ) ;
2016-01-20 07:44:43 +00:00
QString warning1 = testFileUrl ( " mapCoordinatesRect.qml " ) . toString ( ) + " :35:5: QML Item: mapToItem() given argument \" 1122 \" which is neither null nor an Item " ;
QString warning2 = testFileUrl ( " mapCoordinatesRect.qml " ) . toString ( ) + " :35:5: QML Item: mapFromItem() given argument \" 1122 \" which is neither null nor an Item " ;
2012-05-16 02:59:32 +00:00
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning1 ) ) ;
QVERIFY ( QMetaObject : : invokeMethod ( root , " checkMapAToInvalid " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) , Q_ARG ( QVariant , width ) , Q_ARG ( QVariant , height ) ) ) ;
QVERIFY ( result . toBool ( ) ) ;
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning2 ) ) ;
QVERIFY ( QMetaObject : : invokeMethod ( root , " checkMapAFromInvalid " ,
Q_RETURN_ARG ( QVariant , result ) , Q_ARG ( QVariant , x ) , Q_ARG ( QVariant , y ) , Q_ARG ( QVariant , width ) , Q_ARG ( QVariant , height ) ) ) ;
QVERIFY ( result . toBool ( ) ) ;
2012-07-11 07:32:16 +00:00
delete window ;
2012-05-16 02:59:32 +00:00
}
void tst_QQuickItem : : mapCoordinatesRect_data ( )
{
QTest : : addColumn < int > ( " x " ) ;
QTest : : addColumn < int > ( " y " ) ;
QTest : : addColumn < int > ( " width " ) ;
QTest : : addColumn < int > ( " height " ) ;
for ( int i = - 20 ; i < = 20 ; i + = 5 )
QTest : : newRow ( QTest : : toString ( i ) ) < < i < < i < < i < < i ;
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : transforms_data ( )
2011-04-27 12:13:26 +00:00
{
QTest : : addColumn < QByteArray > ( " qml " ) ;
2011-07-18 22:57:41 +00:00
QTest : : addColumn < QTransform > ( " transform " ) ;
2011-04-27 12:13:26 +00:00
QTest : : newRow ( " translate " ) < < QByteArray ( " Translate { x: 10; y: 20 } " )
2011-07-18 22:57:41 +00:00
< < QTransform ( 1 , 0 , 0 , 0 , 1 , 0 , 10 , 20 , 1 ) ;
2013-12-11 06:22:49 +00:00
QTest : : newRow ( " matrix4x4 " ) < < QByteArray ( " Matrix4x4 { matrix: Qt.matrix4x4(1,0,0,10, 0,1,0,15, 0,0,1,0, 0,0,0,1) } " )
< < QTransform ( 1 , 0 , 0 , 0 , 1 , 0 , 10 , 15 , 1 ) ;
2011-04-27 12:13:26 +00:00
QTest : : newRow ( " rotation " ) < < QByteArray ( " Rotation { angle: 90 } " )
2011-07-18 22:57:41 +00:00
< < QTransform ( 0 , 1 , 0 , - 1 , 0 , 0 , 0 , 0 , 1 ) ;
2011-04-27 12:13:26 +00:00
QTest : : newRow ( " scale " ) < < QByteArray ( " Scale { xScale: 1.5; yScale: -2 } " )
2011-07-18 22:57:41 +00:00
< < QTransform ( 1.5 , 0 , 0 , 0 , - 2 , 0 , 0 , 0 , 1 ) ;
2011-04-27 12:13:26 +00:00
QTest : : newRow ( " sequence " ) < < QByteArray ( " [ Translate { x: 10; y: 20 }, Scale { xScale: 1.5; yScale: -2 } ] " )
2011-07-18 22:57:41 +00:00
< < QTransform ( 1 , 0 , 0 , 0 , 1 , 0 , 10 , 20 , 1 ) * QTransform ( 1.5 , 0 , 0 , 0 , - 2 , 0 , 0 , 0 , 1 ) ;
2011-04-27 12:13:26 +00:00
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : transforms ( )
2011-04-27 12:13:26 +00:00
{
2011-07-18 22:57:41 +00:00
QFETCH ( QByteArray , qml ) ;
QFETCH ( QTransform , transform ) ;
2012-02-16 04:43:03 +00:00
QQmlComponent component ( & engine ) ;
2014-05-01 21:08:42 +00:00
component . setData ( " import QtQuick 2.3 \n Item { transform: " + qml + " } " , QUrl : : fromLocalFile ( " " ) ) ;
2011-10-14 08:51:42 +00:00
QQuickItem * item = qobject_cast < QQuickItem * > ( component . create ( ) ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( item ) ;
2018-02-21 09:41:54 +00:00
QCOMPARE ( item - > itemTransform ( nullptr , nullptr ) , transform ) ;
2011-04-27 12:13:26 +00:00
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : childrenProperty ( )
2011-04-27 12:13:26 +00:00
{
2012-02-16 04:43:03 +00:00
QQmlComponent component ( & engine , testFileUrl ( " childrenProperty.qml " ) ) ;
2011-10-17 11:03:58 +00:00
2011-04-27 12:13:26 +00:00
QObject * o = component . create ( ) ;
2018-02-21 09:41:54 +00:00
QVERIFY ( o ! = nullptr ) ;
2011-04-27 12:13:26 +00:00
QCOMPARE ( o - > property ( " test1 " ) . toBool ( ) , true ) ;
QCOMPARE ( o - > property ( " test2 " ) . toBool ( ) , true ) ;
QCOMPARE ( o - > property ( " test3 " ) . toBool ( ) , true ) ;
QCOMPARE ( o - > property ( " test4 " ) . toBool ( ) , true ) ;
QCOMPARE ( o - > property ( " test5 " ) . toBool ( ) , true ) ;
delete o ;
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : resourcesProperty ( )
2011-04-27 12:13:26 +00:00
{
2012-02-16 04:43:03 +00:00
QQmlComponent component ( & engine , testFileUrl ( " resourcesProperty.qml " ) ) ;
2011-10-17 11:03:58 +00:00
2013-05-08 09:18:04 +00:00
QObject * object = component . create ( ) ;
2018-02-21 09:41:54 +00:00
QVERIFY ( object ! = nullptr ) ;
2011-04-27 12:13:26 +00:00
2013-05-08 09:18:04 +00:00
QQmlProperty property ( object , " resources " , component . creationContext ( ) ) ;
QVERIFY ( property . isValid ( ) ) ;
QQmlListReference list = qvariant_cast < QQmlListReference > ( property . read ( ) ) ;
QVERIFY ( list . isValid ( ) ) ;
QCOMPARE ( list . count ( ) , 4 ) ;
QCOMPARE ( object - > property ( " test1 " ) . toBool ( ) , true ) ;
QCOMPARE ( object - > property ( " test2 " ) . toBool ( ) , true ) ;
QCOMPARE ( object - > property ( " test3 " ) . toBool ( ) , true ) ;
QCOMPARE ( object - > property ( " test4 " ) . toBool ( ) , true ) ;
QCOMPARE ( object - > property ( " test5 " ) . toBool ( ) , true ) ;
QCOMPARE ( object - > property ( " test6 " ) . toBool ( ) , true ) ;
QObject * subObject = object - > findChild < QObject * > ( " subObject " ) ;
QVERIFY ( subObject ) ;
QCOMPARE ( object , subObject - > parent ( ) ) ;
delete subObject ;
QCOMPARE ( list . count ( ) , 3 ) ;
delete object ;
2011-04-27 12:13:26 +00:00
}
QQuickItem: Make x/y/width/height bindable
This ports the four properties properties to Q_OBJECT_COMPAT_PROPERTY.
It is not possible to use Q_OBJECT_BINDABLE_PROPERTY, not even for the
simpler x and y properties, as QQuickItem calls the virtual
geometryChange method in the setters, making them non-trivial.
For width/height, we use the new property system to improve the
width/heightValid check: We return not only valid if the property has
explictily been set (either via a direct setter call, or because the
setter has been called when a binding evaluated), but also when a
binding is set. This matches the fact that implicitWidth/Height should
only be used when width/height is not specified. In theory, this could
help in cases where one sets both implicit and explicit properties for
Text, skipping one initial layout calculation.
As the setters now remove the binding (this aligns with the behavior of
writing through the meta-object system), a few test cases had to be
adjusted, which relied on the setter not removing it.
Further test changes were necessary because the location of a few
warnings changed, due to differences in binding order evaluation and
error reporting.
[ChangeLog][QtQuick][QQuickItem] The x, y, width and height properties
of QQuickItem are now bindable. This enables modifying their bindings
from C++. One could for instance swap the width of two items,
preserving the bindings:
// QML
Item {
Rectangle { id: a; width: parent.width / 2 }
Rectangle { id: b; width: parent.width / 3; anchors.right: a.left }
}
// C++
auto rootCtxt = engine.rootContext();
auto a = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"a"_qs));
auto b = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"b"_qs));
auto aBinding = a->bindableWidth().takeBinding();
auto bBinding = b->bindableWidth().takeBinding();
a->bindableWidth()->setBinding(bBinding);
b->bindableWidth()->setBinding(aBinding);
Afterwards, if the root item gets resized, the width of the rectangles
will still be adjusted.
[ChangeLog][QtQuick][QQuickItem][Important Behavior Changes] Calling
the setWidth, setHeight, setX and setY properties now removes any
existing binding from the corresponding property.
Change-Id: I5e1553611cb92b033247ada715cea48c962395bc
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-09-24 09:06:06 +00:00
void tst_QQuickItem : : bindableProperties_data ( )
{
QTest : : addColumn < qreal > ( " initialValue " ) ;
QTest : : addColumn < qreal > ( " newValue " ) ;
QTest : : addColumn < QString > ( " property " ) ;
// can't simply use 3. or 3.0 for the numbers as qreal might
// be float instead of double...
QTest : : addRow ( " x " ) < < qreal ( 3 ) < < qreal ( 14 ) < < " x " ;
QTest : : addRow ( " y " ) < < qreal ( 10 ) < < qreal ( 20 ) < < " y " ;
QTest : : addRow ( " width " ) < < qreal ( 100 ) < < qreal ( 200 ) < < " width " ;
QTest : : addRow ( " height " ) < < qreal ( 50 ) < < qreal ( 40 ) < < " height " ;
}
void tst_QQuickItem : : bindableProperties ( )
{
QQuickItem item ;
QFETCH ( qreal , initialValue ) ;
QFETCH ( qreal , newValue ) ;
QFETCH ( QString , property ) ;
QTestPrivate : : testReadWritePropertyBasics ( item , initialValue , newValue , property . toUtf8 ( ) . constData ( ) ) ;
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : propertyChanges ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setBaseSize ( QSize ( 300 , 300 ) ) ;
window - > setSource ( testFileUrl ( " propertychanges.qml " ) ) ;
window - > show ( ) ;
2012-10-19 16:05:01 +00:00
window - > requestActivate ( ) ;
2012-07-19 12:13:14 +00:00
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * item = findItem < QQuickItem > ( window - > rootObject ( ) , " item " ) ;
QQuickItem * parentItem = findItem < QQuickItem > ( window - > rootObject ( ) , " parentItem " ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( item ) ;
QVERIFY ( parentItem ) ;
2014-03-02 10:32:29 +00:00
QSignalSpy parentSpy ( item , SIGNAL ( parentChanged ( QQuickItem * ) ) ) ;
2011-04-27 12:13:26 +00:00
QSignalSpy widthSpy ( item , SIGNAL ( widthChanged ( ) ) ) ;
QSignalSpy heightSpy ( item , SIGNAL ( heightChanged ( ) ) ) ;
QSignalSpy baselineOffsetSpy ( item , SIGNAL ( baselineOffsetChanged ( qreal ) ) ) ;
QSignalSpy childrenRectSpy ( parentItem , SIGNAL ( childrenRectChanged ( QRectF ) ) ) ;
QSignalSpy focusSpy ( item , SIGNAL ( focusChanged ( bool ) ) ) ;
QSignalSpy wantsFocusSpy ( parentItem , SIGNAL ( activeFocusChanged ( bool ) ) ) ;
QSignalSpy childrenChangedSpy ( parentItem , SIGNAL ( childrenChanged ( ) ) ) ;
QSignalSpy xSpy ( item , SIGNAL ( xChanged ( ) ) ) ;
QSignalSpy ySpy ( item , SIGNAL ( yChanged ( ) ) ) ;
item - > setParentItem ( parentItem ) ;
item - > setWidth ( 100.0 ) ;
item - > setHeight ( 200.0 ) ;
item - > setFocus ( true ) ;
item - > setBaselineOffset ( 10.0 ) ;
QCOMPARE ( item - > parentItem ( ) , parentItem ) ;
QCOMPARE ( parentSpy . count ( ) , 1 ) ;
QList < QVariant > parentArguments = parentSpy . first ( ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( parentArguments . count ( ) , 1 ) ;
2011-10-14 08:51:42 +00:00
QCOMPARE ( item - > parentItem ( ) , qvariant_cast < QQuickItem * > ( parentArguments . at ( 0 ) ) ) ;
2011-04-27 12:13:26 +00:00
QCOMPARE ( childrenChangedSpy . count ( ) , 1 ) ;
item - > setParentItem ( parentItem ) ;
QCOMPARE ( childrenChangedSpy . count ( ) , 1 ) ;
QCOMPARE ( item - > width ( ) , 100.0 ) ;
QCOMPARE ( widthSpy . count ( ) , 1 ) ;
QCOMPARE ( item - > height ( ) , 200.0 ) ;
QCOMPARE ( heightSpy . count ( ) , 1 ) ;
QCOMPARE ( item - > baselineOffset ( ) , 10.0 ) ;
QCOMPARE ( baselineOffsetSpy . count ( ) , 1 ) ;
QList < QVariant > baselineOffsetArguments = baselineOffsetSpy . first ( ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( baselineOffsetArguments . count ( ) , 1 ) ;
2011-04-27 12:13:26 +00:00
QCOMPARE ( item - > baselineOffset ( ) , baselineOffsetArguments . at ( 0 ) . toReal ( ) ) ;
QCOMPARE ( parentItem - > childrenRect ( ) , QRectF ( 0.0 , 0.0 , 100.0 , 200.0 ) ) ;
2011-11-30 04:58:12 +00:00
QCOMPARE ( childrenRectSpy . count ( ) , 1 ) ;
QList < QVariant > childrenRectArguments = childrenRectSpy . at ( 0 ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( childrenRectArguments . count ( ) , 1 ) ;
2011-04-27 12:13:26 +00:00
QCOMPARE ( parentItem - > childrenRect ( ) , childrenRectArguments . at ( 0 ) . toRectF ( ) ) ;
QCOMPARE ( item - > hasActiveFocus ( ) , true ) ;
QCOMPARE ( focusSpy . count ( ) , 1 ) ;
QList < QVariant > focusArguments = focusSpy . first ( ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( focusArguments . count ( ) , 1 ) ;
2011-04-27 12:13:26 +00:00
QCOMPARE ( focusArguments . at ( 0 ) . toBool ( ) , true ) ;
QCOMPARE ( parentItem - > hasActiveFocus ( ) , false ) ;
QCOMPARE ( parentItem - > hasFocus ( ) , false ) ;
QCOMPARE ( wantsFocusSpy . count ( ) , 0 ) ;
item - > setX ( 10.0 ) ;
QCOMPARE ( item - > x ( ) , 10.0 ) ;
QCOMPARE ( xSpy . count ( ) , 1 ) ;
item - > setY ( 10.0 ) ;
QCOMPARE ( item - > y ( ) , 10.0 ) ;
QCOMPARE ( ySpy . count ( ) , 1 ) ;
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
2016-11-16 15:30:39 +00:00
void tst_QQuickItem : : nonexistentPropertyConnection ( )
{
// QTBUG-56551: don't crash
QQmlComponent component ( & engine , testFileUrl ( " nonexistentPropertyConnection.qml " ) ) ;
QObject * o = component . create ( ) ;
QVERIFY ( o ) ;
delete o ;
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : childrenRect ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " childrenRect.qml " ) ) ;
window - > setBaseSize ( QSize ( 240 , 320 ) ) ;
window - > show ( ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * o = window - > rootObject ( ) ;
2011-10-14 08:51:42 +00:00
QQuickItem * item = o - > findChild < QQuickItem * > ( " testItem " ) ;
2011-04-27 12:13:26 +00:00
QCOMPARE ( item - > width ( ) , qreal ( 0 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 0 ) ) ;
o - > setProperty ( " childCount " , 1 ) ;
QCOMPARE ( item - > width ( ) , qreal ( 10 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 20 ) ) ;
o - > setProperty ( " childCount " , 5 ) ;
QCOMPARE ( item - > width ( ) , qreal ( 50 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 100 ) ) ;
o - > setProperty ( " childCount " , 0 ) ;
QCOMPARE ( item - > width ( ) , qreal ( 0 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 0 ) ) ;
delete o ;
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
// QTBUG-11383
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : childrenRectBug ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2011-12-06 02:29:09 +00:00
QQuickItem: Make x/y/width/height bindable
This ports the four properties properties to Q_OBJECT_COMPAT_PROPERTY.
It is not possible to use Q_OBJECT_BINDABLE_PROPERTY, not even for the
simpler x and y properties, as QQuickItem calls the virtual
geometryChange method in the setters, making them non-trivial.
For width/height, we use the new property system to improve the
width/heightValid check: We return not only valid if the property has
explictily been set (either via a direct setter call, or because the
setter has been called when a binding evaluated), but also when a
binding is set. This matches the fact that implicitWidth/Height should
only be used when width/height is not specified. In theory, this could
help in cases where one sets both implicit and explicit properties for
Text, skipping one initial layout calculation.
As the setters now remove the binding (this aligns with the behavior of
writing through the meta-object system), a few test cases had to be
adjusted, which relied on the setter not removing it.
Further test changes were necessary because the location of a few
warnings changed, due to differences in binding order evaluation and
error reporting.
[ChangeLog][QtQuick][QQuickItem] The x, y, width and height properties
of QQuickItem are now bindable. This enables modifying their bindings
from C++. One could for instance swap the width of two items,
preserving the bindings:
// QML
Item {
Rectangle { id: a; width: parent.width / 2 }
Rectangle { id: b; width: parent.width / 3; anchors.right: a.left }
}
// C++
auto rootCtxt = engine.rootContext();
auto a = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"a"_qs));
auto b = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"b"_qs));
auto aBinding = a->bindableWidth().takeBinding();
auto bBinding = b->bindableWidth().takeBinding();
a->bindableWidth()->setBinding(bBinding);
b->bindableWidth()->setBinding(aBinding);
Afterwards, if the root item gets resized, the width of the rectangles
will still be adjusted.
[ChangeLog][QtQuick][QQuickItem][Important Behavior Changes] Calling
the setWidth, setHeight, setX and setY properties now removes any
existing binding from the corresponding property.
Change-Id: I5e1553611cb92b033247ada715cea48c962395bc
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-09-24 09:06:06 +00:00
QString warning = testFileUrl ( " childrenRectBug.qml " ) . toString ( ) + " :11:9: QML Item: Binding loop detected for property \" height \" " ;
2011-12-06 02:29:09 +00:00
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning ) ) ;
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning ) ) ;
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " childrenRectBug.qml " ) ) ;
window - > show ( ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * o = window - > rootObject ( ) ;
2011-10-14 08:51:42 +00:00
QQuickItem * item = o - > findChild < QQuickItem * > ( " theItem " ) ;
2011-04-27 12:13:26 +00:00
QCOMPARE ( item - > width ( ) , qreal ( 200 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 100 ) ) ;
QCOMPARE ( item - > x ( ) , qreal ( 100 ) ) ;
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
// QTBUG-11465
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : childrenRectBug2 ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2011-12-06 02:29:09 +00:00
QQuickItem: Make x/y/width/height bindable
This ports the four properties properties to Q_OBJECT_COMPAT_PROPERTY.
It is not possible to use Q_OBJECT_BINDABLE_PROPERTY, not even for the
simpler x and y properties, as QQuickItem calls the virtual
geometryChange method in the setters, making them non-trivial.
For width/height, we use the new property system to improve the
width/heightValid check: We return not only valid if the property has
explictily been set (either via a direct setter call, or because the
setter has been called when a binding evaluated), but also when a
binding is set. This matches the fact that implicitWidth/Height should
only be used when width/height is not specified. In theory, this could
help in cases where one sets both implicit and explicit properties for
Text, skipping one initial layout calculation.
As the setters now remove the binding (this aligns with the behavior of
writing through the meta-object system), a few test cases had to be
adjusted, which relied on the setter not removing it.
Further test changes were necessary because the location of a few
warnings changed, due to differences in binding order evaluation and
error reporting.
[ChangeLog][QtQuick][QQuickItem] The x, y, width and height properties
of QQuickItem are now bindable. This enables modifying their bindings
from C++. One could for instance swap the width of two items,
preserving the bindings:
// QML
Item {
Rectangle { id: a; width: parent.width / 2 }
Rectangle { id: b; width: parent.width / 3; anchors.right: a.left }
}
// C++
auto rootCtxt = engine.rootContext();
auto a = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"a"_qs));
auto b = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"b"_qs));
auto aBinding = a->bindableWidth().takeBinding();
auto bBinding = b->bindableWidth().takeBinding();
a->bindableWidth()->setBinding(bBinding);
b->bindableWidth()->setBinding(aBinding);
Afterwards, if the root item gets resized, the width of the rectangles
will still be adjusted.
[ChangeLog][QtQuick][QQuickItem][Important Behavior Changes] Calling
the setWidth, setHeight, setX and setY properties now removes any
existing binding from the corresponding property.
Change-Id: I5e1553611cb92b033247ada715cea48c962395bc
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-09-24 09:06:06 +00:00
QString warning1 = testFileUrl ( " childrenRectBug2.qml " ) . toString ( ) + " :10:9: QML Item: Binding loop detected for property \" width \" " ;
2011-12-06 02:29:09 +00:00
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning1 ) ) ;
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning1 ) ) ;
QQuickItem: Make x/y/width/height bindable
This ports the four properties properties to Q_OBJECT_COMPAT_PROPERTY.
It is not possible to use Q_OBJECT_BINDABLE_PROPERTY, not even for the
simpler x and y properties, as QQuickItem calls the virtual
geometryChange method in the setters, making them non-trivial.
For width/height, we use the new property system to improve the
width/heightValid check: We return not only valid if the property has
explictily been set (either via a direct setter call, or because the
setter has been called when a binding evaluated), but also when a
binding is set. This matches the fact that implicitWidth/Height should
only be used when width/height is not specified. In theory, this could
help in cases where one sets both implicit and explicit properties for
Text, skipping one initial layout calculation.
As the setters now remove the binding (this aligns with the behavior of
writing through the meta-object system), a few test cases had to be
adjusted, which relied on the setter not removing it.
Further test changes were necessary because the location of a few
warnings changed, due to differences in binding order evaluation and
error reporting.
[ChangeLog][QtQuick][QQuickItem] The x, y, width and height properties
of QQuickItem are now bindable. This enables modifying their bindings
from C++. One could for instance swap the width of two items,
preserving the bindings:
// QML
Item {
Rectangle { id: a; width: parent.width / 2 }
Rectangle { id: b; width: parent.width / 3; anchors.right: a.left }
}
// C++
auto rootCtxt = engine.rootContext();
auto a = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"a"_qs));
auto b = qobject_cast<QQuickItem*>(rootCtxt->objectForName(u"b"_qs));
auto aBinding = a->bindableWidth().takeBinding();
auto bBinding = b->bindableWidth().takeBinding();
a->bindableWidth()->setBinding(bBinding);
b->bindableWidth()->setBinding(aBinding);
Afterwards, if the root item gets resized, the width of the rectangles
will still be adjusted.
[ChangeLog][QtQuick][QQuickItem][Important Behavior Changes] Calling
the setWidth, setHeight, setX and setY properties now removes any
existing binding from the corresponding property.
Change-Id: I5e1553611cb92b033247ada715cea48c962395bc
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-09-24 09:06:06 +00:00
QString warning2 = testFileUrl ( " childrenRectBug2.qml " ) . toString ( ) + " :11:9: QML Item: Binding loop detected for property \" height \" " ;
2011-12-06 02:29:09 +00:00
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning2 ) ) ;
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning2 ) ) ;
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning2 ) ) ;
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning2 ) ) ;
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " childrenRectBug2.qml " ) ) ;
window - > show ( ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QQuickRectangle * rect = qobject_cast < QQuickRectangle * > ( window - > rootObject ( ) ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( rect ) ;
2011-10-14 08:51:42 +00:00
QQuickItem * item = rect - > findChild < QQuickItem * > ( " theItem " ) ;
2011-04-27 12:13:26 +00:00
QCOMPARE ( item - > width ( ) , qreal ( 100 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 110 ) ) ;
QCOMPARE ( item - > x ( ) , qreal ( 130 ) ) ;
2011-10-14 08:51:42 +00:00
QQuickItemPrivate * rectPrivate = QQuickItemPrivate : : get ( rect ) ;
2011-04-27 12:13:26 +00:00
rectPrivate - > setState ( " row " ) ;
QCOMPARE ( item - > width ( ) , qreal ( 210 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 50 ) ) ;
QCOMPARE ( item - > x ( ) , qreal ( 75 ) ) ;
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
// QTBUG-12722
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : childrenRectBug3 ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " childrenRectBug3.qml " ) ) ;
window - > show ( ) ;
2011-04-27 12:13:26 +00:00
//don't crash on delete
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
2015-09-20 15:53:57 +00:00
// QTBUG-38732
void tst_QQuickItem : : childrenRectBottomRightCorner ( )
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2015-09-20 15:53:57 +00:00
window - > setSource ( testFileUrl ( " childrenRectBottomRightCorner.qml " ) ) ;
window - > show ( ) ;
QQuickItem * rect = window - > rootObject ( ) - > findChild < QQuickItem * > ( " childrenRectProxy " ) ;
QCOMPARE ( rect - > x ( ) , qreal ( - 100 ) ) ;
QCOMPARE ( rect - > y ( ) , qreal ( - 100 ) ) ;
QCOMPARE ( rect - > width ( ) , qreal ( 50 ) ) ;
QCOMPARE ( rect - > height ( ) , qreal ( 50 ) ) ;
delete window ;
}
2016-05-19 11:10:19 +00:00
struct TestListener : public QQuickItemChangeListener
{
2016-07-13 09:22:11 +00:00
TestListener ( bool remove = false ) : remove ( remove ) { }
2016-05-19 11:10:19 +00:00
2016-11-03 13:34:43 +00:00
void itemGeometryChanged ( QQuickItem * item , QQuickGeometryChange , const QRectF & oldGeometry ) override
2016-04-11 10:44:00 +00:00
{
2016-11-03 13:34:43 +00:00
record ( item , QQuickItemPrivate : : Geometry , oldGeometry ) ;
2016-07-13 09:22:11 +00:00
}
void itemSiblingOrderChanged ( QQuickItem * item ) override
{
record ( item , QQuickItemPrivate : : SiblingOrder ) ;
}
void itemVisibilityChanged ( QQuickItem * item ) override
{
record ( item , QQuickItemPrivate : : Visibility ) ;
}
void itemOpacityChanged ( QQuickItem * item ) override
{
record ( item , QQuickItemPrivate : : Opacity ) ;
}
void itemRotationChanged ( QQuickItem * item ) override
{
record ( item , QQuickItemPrivate : : Rotation ) ;
}
void itemImplicitWidthChanged ( QQuickItem * item ) override
{
record ( item , QQuickItemPrivate : : ImplicitWidth ) ;
}
void itemImplicitHeightChanged ( QQuickItem * item ) override
{
record ( item , QQuickItemPrivate : : ImplicitHeight ) ;
2016-04-11 10:44:00 +00:00
}
2016-05-19 11:10:19 +00:00
void itemDestroyed ( QQuickItem * item ) override
{
2016-07-13 09:22:11 +00:00
record ( item , QQuickItemPrivate : : Destroyed ) ;
2016-05-19 11:10:19 +00:00
}
void itemChildAdded ( QQuickItem * item , QQuickItem * child ) override
{
2016-07-13 09:22:11 +00:00
record ( item , QQuickItemPrivate : : Children , QVariant : : fromValue ( child ) ) ;
2016-05-19 11:10:19 +00:00
}
void itemChildRemoved ( QQuickItem * item , QQuickItem * child ) override
{
2016-07-13 09:22:11 +00:00
record ( item , QQuickItemPrivate : : Children , QVariant : : fromValue ( child ) ) ;
2016-05-19 11:10:19 +00:00
}
void itemParentChanged ( QQuickItem * item , QQuickItem * parent ) override
{
2016-07-13 09:22:11 +00:00
record ( item , QQuickItemPrivate : : Parent , QVariant : : fromValue ( parent ) ) ;
2016-05-19 11:10:19 +00:00
}
QQuickAnchorsPrivate * anchorPrivate ( ) override { return nullptr ; }
2016-07-13 09:22:11 +00:00
void record ( QQuickItem * item , QQuickItemPrivate : : ChangeType change , const QVariant & value = QVariant ( ) )
{
changes + = change ;
values [ change ] = value ;
// QTBUG-54732
if ( remove )
QQuickItemPrivate : : get ( item ) - > removeItemChangeListener ( this , change ) ;
}
int count ( QQuickItemPrivate : : ChangeType change ) const
2016-05-19 11:10:19 +00:00
{
2016-07-13 09:22:11 +00:00
return changes . count ( change ) ;
}
QVariant value ( QQuickItemPrivate : : ChangeType change ) const
{
return values . value ( change ) ;
2016-05-19 11:10:19 +00:00
}
bool remove ;
2016-07-13 09:22:11 +00:00
QList < QQuickItemPrivate : : ChangeType > changes ;
QHash < QQuickItemPrivate : : ChangeType , QVariant > values ;
2016-05-19 11:10:19 +00:00
} ;
void tst_QQuickItem : : changeListener ( )
{
2016-07-13 09:22:11 +00:00
QQuickWindow window ;
window . show ( ) ;
2018-02-21 08:16:14 +00:00
QVERIFY ( QTest : : qWaitForWindowExposed ( & window ) ) ;
2016-07-13 09:22:11 +00:00
QQuickItem * item = new QQuickItem ;
2016-05-19 11:10:19 +00:00
TestListener itemListener ;
2016-07-13 09:22:11 +00:00
QQuickItemPrivate : : get ( item ) - > addItemChangeListener ( & itemListener , QQuickItemPrivate : : ChangeTypes ( 0xffff ) ) ;
item - > setImplicitWidth ( 10 ) ;
QCOMPARE ( itemListener . count ( QQuickItemPrivate : : ImplicitWidth ) , 1 ) ;
QCOMPARE ( itemListener . count ( QQuickItemPrivate : : Geometry ) , 1 ) ;
2016-11-03 13:34:43 +00:00
QCOMPARE ( itemListener . value ( QQuickItemPrivate : : Geometry ) , QVariant ( QRectF ( 0 , 0 , 0 , 0 ) ) ) ;
2016-05-19 11:10:19 +00:00
2016-07-13 09:22:11 +00:00
item - > setImplicitHeight ( 20 ) ;
QCOMPARE ( itemListener . count ( QQuickItemPrivate : : ImplicitHeight ) , 1 ) ;
QCOMPARE ( itemListener . count ( QQuickItemPrivate : : Geometry ) , 2 ) ;
2016-11-03 13:34:43 +00:00
QCOMPARE ( itemListener . value ( QQuickItemPrivate : : Geometry ) , QVariant ( QRectF ( 0 , 0 , 10 , 0 ) ) ) ;
2016-05-19 11:10:19 +00:00
2016-07-13 09:22:11 +00:00
item - > setWidth ( item - > width ( ) + 30 ) ;
QCOMPARE ( itemListener . count ( QQuickItemPrivate : : Geometry ) , 3 ) ;
2016-11-03 13:34:43 +00:00
QCOMPARE ( itemListener . value ( QQuickItemPrivate : : Geometry ) , QVariant ( QRectF ( 0 , 0 , 10 , 20 ) ) ) ;
2016-05-19 11:10:19 +00:00
2016-07-13 09:22:11 +00:00
item - > setHeight ( item - > height ( ) + 40 ) ;
QCOMPARE ( itemListener . count ( QQuickItemPrivate : : Geometry ) , 4 ) ;
2016-11-03 13:34:43 +00:00
QCOMPARE ( itemListener . value ( QQuickItemPrivate : : Geometry ) , QVariant ( QRectF ( 0 , 0 , 40 , 20 ) ) ) ;
2016-05-19 11:10:19 +00:00
2016-07-13 09:22:11 +00:00
item - > setOpacity ( 0.5 ) ;
QCOMPARE ( itemListener . count ( QQuickItemPrivate : : Opacity ) , 1 ) ;
2016-05-19 11:10:19 +00:00
2016-07-13 09:22:11 +00:00
item - > setRotation ( 90 ) ;
QCOMPARE ( itemListener . count ( QQuickItemPrivate : : Rotation ) , 1 ) ;
item - > setParentItem ( window . contentItem ( ) ) ;
QCOMPARE ( itemListener . count ( QQuickItemPrivate : : Parent ) , 1 ) ;
item - > setVisible ( false ) ;
QCOMPARE ( itemListener . count ( QQuickItemPrivate : : Visibility ) , 1 ) ;
2016-05-19 11:10:19 +00:00
2016-07-13 09:22:11 +00:00
QQuickItemPrivate : : get ( item ) - > removeItemChangeListener ( & itemListener , QQuickItemPrivate : : ChangeTypes ( 0xffff ) ) ;
2016-05-19 11:10:19 +00:00
2016-07-13 09:22:11 +00:00
QQuickItem * parent = new QQuickItem ( window . contentItem ( ) ) ;
2016-05-19 11:10:19 +00:00
TestListener parentListener ;
QQuickItemPrivate : : get ( parent ) - > addItemChangeListener ( & parentListener , QQuickItemPrivate : : Children ) ;
QQuickItem * child1 = new QQuickItem ;
QQuickItem * child2 = new QQuickItem ;
TestListener child1Listener ;
TestListener child2Listener ;
QQuickItemPrivate : : get ( child1 ) - > addItemChangeListener ( & child1Listener , QQuickItemPrivate : : Parent | QQuickItemPrivate : : SiblingOrder | QQuickItemPrivate : : Destroyed ) ;
QQuickItemPrivate : : get ( child2 ) - > addItemChangeListener ( & child2Listener , QQuickItemPrivate : : Parent | QQuickItemPrivate : : SiblingOrder | QQuickItemPrivate : : Destroyed ) ;
child1 - > setParentItem ( parent ) ;
2016-07-13 09:22:11 +00:00
QCOMPARE ( parentListener . count ( QQuickItemPrivate : : Children ) , 1 ) ;
QCOMPARE ( parentListener . value ( QQuickItemPrivate : : Children ) , QVariant : : fromValue ( child1 ) ) ;
QCOMPARE ( child1Listener . count ( QQuickItemPrivate : : Parent ) , 1 ) ;
QCOMPARE ( child1Listener . value ( QQuickItemPrivate : : Parent ) , QVariant : : fromValue ( parent ) ) ;
2016-05-19 11:10:19 +00:00
child2 - > setParentItem ( parent ) ;
2016-07-13 09:22:11 +00:00
QCOMPARE ( parentListener . count ( QQuickItemPrivate : : Children ) , 2 ) ;
QCOMPARE ( parentListener . value ( QQuickItemPrivate : : Children ) , QVariant : : fromValue ( child2 ) ) ;
QCOMPARE ( child2Listener . count ( QQuickItemPrivate : : Parent ) , 1 ) ;
QCOMPARE ( child2Listener . value ( QQuickItemPrivate : : Parent ) , QVariant : : fromValue ( parent ) ) ;
2016-05-19 11:10:19 +00:00
child2 - > stackBefore ( child1 ) ;
2016-07-13 09:22:11 +00:00
QCOMPARE ( child1Listener . count ( QQuickItemPrivate : : SiblingOrder ) , 1 ) ;
QCOMPARE ( child2Listener . count ( QQuickItemPrivate : : SiblingOrder ) , 1 ) ;
2016-05-19 11:10:19 +00:00
child1 - > setParentItem ( nullptr ) ;
2016-07-13 09:22:11 +00:00
QCOMPARE ( parentListener . count ( QQuickItemPrivate : : Children ) , 3 ) ;
QCOMPARE ( parentListener . value ( QQuickItemPrivate : : Children ) , QVariant : : fromValue ( child1 ) ) ;
QCOMPARE ( child1Listener . count ( QQuickItemPrivate : : Parent ) , 2 ) ;
QCOMPARE ( child1Listener . value ( QQuickItemPrivate : : Parent ) , QVariant : : fromValue < QQuickItem * > ( nullptr ) ) ;
2016-05-19 11:10:19 +00:00
delete child1 ;
2016-07-13 09:22:11 +00:00
QCOMPARE ( child1Listener . count ( QQuickItemPrivate : : Destroyed ) , 1 ) ;
2016-05-19 11:10:19 +00:00
delete child2 ;
2016-07-13 09:22:11 +00:00
QCOMPARE ( parentListener . count ( QQuickItemPrivate : : Children ) , 4 ) ;
QCOMPARE ( parentListener . value ( QQuickItemPrivate : : Children ) , QVariant : : fromValue ( child2 ) ) ;
QCOMPARE ( child2Listener . count ( QQuickItemPrivate : : Parent ) , 2 ) ;
QCOMPARE ( child2Listener . value ( QQuickItemPrivate : : Parent ) , QVariant : : fromValue < QQuickItem * > ( nullptr ) ) ;
QCOMPARE ( child2Listener . count ( QQuickItemPrivate : : Destroyed ) , 1 ) ;
2016-05-19 11:10:19 +00:00
QQuickItemPrivate : : get ( parent ) - > removeItemChangeListener ( & parentListener , QQuickItemPrivate : : Children ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , 0 ) ;
2016-07-13 09:22:11 +00:00
// QTBUG-54732: all listeners should get invoked even if they remove themselves while iterating the listeners
2016-05-19 11:10:19 +00:00
QList < TestListener * > listeners ;
for ( int i = 0 ; i < 5 ; + + i )
listeners < < new TestListener ( true ) ;
2016-07-13 09:22:11 +00:00
// itemVisibilityChanged x 5
foreach ( TestListener * listener , listeners )
QQuickItemPrivate : : get ( parent ) - > addItemChangeListener ( listener , QQuickItemPrivate : : Visibility ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , listeners . count ( ) ) ;
parent - > setVisible ( false ) ;
foreach ( TestListener * listener , listeners )
QCOMPARE ( listener - > count ( QQuickItemPrivate : : Visibility ) , 1 ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , 0 ) ;
// itemRotationChanged x 5
foreach ( TestListener * listener , listeners )
QQuickItemPrivate : : get ( parent ) - > addItemChangeListener ( listener , QQuickItemPrivate : : Rotation ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , listeners . count ( ) ) ;
parent - > setRotation ( 90 ) ;
foreach ( TestListener * listener , listeners )
QCOMPARE ( listener - > count ( QQuickItemPrivate : : Rotation ) , 1 ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , 0 ) ;
// itemOpacityChanged x 5
foreach ( TestListener * listener , listeners )
QQuickItemPrivate : : get ( parent ) - > addItemChangeListener ( listener , QQuickItemPrivate : : Opacity ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , listeners . count ( ) ) ;
parent - > setOpacity ( 0.5 ) ;
foreach ( TestListener * listener , listeners )
QCOMPARE ( listener - > count ( QQuickItemPrivate : : Opacity ) , 1 ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , 0 ) ;
2016-05-19 11:10:19 +00:00
// itemChildAdded() x 5
foreach ( TestListener * listener , listeners )
QQuickItemPrivate : : get ( parent ) - > addItemChangeListener ( listener , QQuickItemPrivate : : Children ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , listeners . count ( ) ) ;
child1 = new QQuickItem ( parent ) ;
foreach ( TestListener * listener , listeners )
2016-07-13 09:22:11 +00:00
QCOMPARE ( listener - > count ( QQuickItemPrivate : : Children ) , 1 ) ;
2016-05-19 11:10:19 +00:00
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , 0 ) ;
// itemParentChanged() x 5
foreach ( TestListener * listener , listeners )
QQuickItemPrivate : : get ( child1 ) - > addItemChangeListener ( listener , QQuickItemPrivate : : Parent ) ;
QCOMPARE ( QQuickItemPrivate : : get ( child1 ) - > changeListeners . count ( ) , listeners . count ( ) ) ;
child1 - > setParentItem ( nullptr ) ;
foreach ( TestListener * listener , listeners )
2016-07-13 09:22:11 +00:00
QCOMPARE ( listener - > count ( QQuickItemPrivate : : Parent ) , 1 ) ;
2016-05-19 11:10:19 +00:00
QCOMPARE ( QQuickItemPrivate : : get ( child1 ) - > changeListeners . count ( ) , 0 ) ;
2016-07-13 09:22:11 +00:00
// itemImplicitWidthChanged() x 5
foreach ( TestListener * listener , listeners )
QQuickItemPrivate : : get ( parent ) - > addItemChangeListener ( listener , QQuickItemPrivate : : ImplicitWidth ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , listeners . count ( ) ) ;
parent - > setImplicitWidth ( parent - > implicitWidth ( ) + 1 ) ;
foreach ( TestListener * listener , listeners )
QCOMPARE ( listener - > count ( QQuickItemPrivate : : ImplicitWidth ) , 1 ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , 0 ) ;
// itemImplicitHeightChanged() x 5
foreach ( TestListener * listener , listeners )
QQuickItemPrivate : : get ( parent ) - > addItemChangeListener ( listener , QQuickItemPrivate : : ImplicitHeight ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , listeners . count ( ) ) ;
parent - > setImplicitHeight ( parent - > implicitHeight ( ) + 1 ) ;
foreach ( TestListener * listener , listeners )
QCOMPARE ( listener - > count ( QQuickItemPrivate : : ImplicitHeight ) , 1 ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , 0 ) ;
// itemGeometryChanged() x 5
foreach ( TestListener * listener , listeners )
QQuickItemPrivate : : get ( parent ) - > addItemChangeListener ( listener , QQuickItemPrivate : : Geometry ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , listeners . count ( ) ) ;
parent - > setWidth ( parent - > width ( ) + 1 ) ;
foreach ( TestListener * listener , listeners )
QCOMPARE ( listener - > count ( QQuickItemPrivate : : Geometry ) , 1 ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , 0 ) ;
2016-05-19 11:10:19 +00:00
// itemChildRemoved() x 5
child1 - > setParentItem ( parent ) ;
foreach ( TestListener * listener , listeners )
QQuickItemPrivate : : get ( parent ) - > addItemChangeListener ( listener , QQuickItemPrivate : : Children ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , listeners . count ( ) ) ;
delete child1 ;
foreach ( TestListener * listener , listeners )
2016-07-13 09:22:11 +00:00
QCOMPARE ( listener - > count ( QQuickItemPrivate : : Children ) , 2 ) ;
2016-05-19 11:10:19 +00:00
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , 0 ) ;
// itemDestroyed() x 5
foreach ( TestListener * listener , listeners )
QQuickItemPrivate : : get ( parent ) - > addItemChangeListener ( listener , QQuickItemPrivate : : Destroyed ) ;
QCOMPARE ( QQuickItemPrivate : : get ( parent ) - > changeListeners . count ( ) , listeners . count ( ) ) ;
delete parent ;
foreach ( TestListener * listener , listeners )
2016-07-13 09:22:11 +00:00
QCOMPARE ( listener - > count ( QQuickItemPrivate : : Destroyed ) , 1 ) ;
2016-05-19 11:10:19 +00:00
}
2011-04-27 12:13:26 +00:00
// QTBUG-13893
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : transformCrash ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " transformCrash.qml " ) ) ;
window - > show ( ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : implicitSize ( )
2011-04-27 12:13:26 +00:00
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " implicitsize.qml " ) ) ;
window - > show ( ) ;
2011-04-27 12:13:26 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * item = qobject_cast < QQuickItem * > ( window - > rootObject ( ) ) ;
2011-04-27 12:13:26 +00:00
QVERIFY ( item ) ;
QCOMPARE ( item - > width ( ) , qreal ( 80 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 60 ) ) ;
QCOMPARE ( item - > implicitWidth ( ) , qreal ( 200 ) ) ;
QCOMPARE ( item - > implicitHeight ( ) , qreal ( 100 ) ) ;
QMetaObject : : invokeMethod ( item , " resetSize " ) ;
QCOMPARE ( item - > width ( ) , qreal ( 200 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 100 ) ) ;
QMetaObject : : invokeMethod ( item , " changeImplicit " ) ;
QCOMPARE ( item - > implicitWidth ( ) , qreal ( 150 ) ) ;
QCOMPARE ( item - > implicitHeight ( ) , qreal ( 80 ) ) ;
QCOMPARE ( item - > width ( ) , qreal ( 150 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 80 ) ) ;
2012-04-20 01:40:40 +00:00
QMetaObject : : invokeMethod ( item , " assignImplicitBinding " ) ;
QCOMPARE ( item - > implicitWidth ( ) , qreal ( 150 ) ) ;
QCOMPARE ( item - > implicitHeight ( ) , qreal ( 80 ) ) ;
QCOMPARE ( item - > width ( ) , qreal ( 150 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 80 ) ) ;
QMetaObject : : invokeMethod ( item , " increaseImplicit " ) ;
QCOMPARE ( item - > implicitWidth ( ) , qreal ( 200 ) ) ;
QCOMPARE ( item - > implicitHeight ( ) , qreal ( 100 ) ) ;
QCOMPARE ( item - > width ( ) , qreal ( 175 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 90 ) ) ;
QMetaObject : : invokeMethod ( item , " changeImplicit " ) ;
QCOMPARE ( item - > implicitWidth ( ) , qreal ( 150 ) ) ;
QCOMPARE ( item - > implicitHeight ( ) , qreal ( 80 ) ) ;
QCOMPARE ( item - > width ( ) , qreal ( 150 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 80 ) ) ;
QMetaObject : : invokeMethod ( item , " assignUndefinedBinding " ) ;
QCOMPARE ( item - > implicitWidth ( ) , qreal ( 150 ) ) ;
QCOMPARE ( item - > implicitHeight ( ) , qreal ( 80 ) ) ;
QCOMPARE ( item - > width ( ) , qreal ( 150 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 80 ) ) ;
QMetaObject : : invokeMethod ( item , " increaseImplicit " ) ;
QCOMPARE ( item - > implicitWidth ( ) , qreal ( 200 ) ) ;
QCOMPARE ( item - > implicitHeight ( ) , qreal ( 100 ) ) ;
QCOMPARE ( item - > width ( ) , qreal ( 175 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 90 ) ) ;
QMetaObject : : invokeMethod ( item , " changeImplicit " ) ;
QCOMPARE ( item - > implicitWidth ( ) , qreal ( 150 ) ) ;
QCOMPARE ( item - > implicitHeight ( ) , qreal ( 80 ) ) ;
QCOMPARE ( item - > width ( ) , qreal ( 150 ) ) ;
QCOMPARE ( item - > height ( ) , qreal ( 80 ) ) ;
2012-07-11 07:32:16 +00:00
delete window ;
2011-04-27 12:13:26 +00:00
}
2011-10-14 08:51:42 +00:00
void tst_QQuickItem : : qtbug_16871 ( )
2011-04-27 12:13:26 +00:00
{
2012-02-16 04:43:03 +00:00
QQmlComponent component ( & engine , testFileUrl ( " qtbug_16871.qml " ) ) ;
2011-04-27 12:13:26 +00:00
QObject * o = component . create ( ) ;
2018-02-21 09:41:54 +00:00
QVERIFY ( o ! = nullptr ) ;
2011-04-27 12:13:26 +00:00
delete o ;
}
2011-12-05 19:21:33 +00:00
void tst_QQuickItem : : visibleChildren ( )
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " visiblechildren.qml " ) ) ;
window - > show ( ) ;
2011-12-05 19:21:33 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * root = qobject_cast < QQuickItem * > ( window - > rootObject ( ) ) ;
2011-12-05 19:21:33 +00:00
QVERIFY ( root ) ;
QCOMPARE ( root - > property ( " test1_1 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test1_2 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test1_3 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test1_4 " ) . toBool ( ) , true ) ;
QMetaObject : : invokeMethod ( root , " hideFirstAndLastRowChild " ) ;
QCOMPARE ( root - > property ( " test2_1 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test2_2 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test2_3 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test2_4 " ) . toBool ( ) , true ) ;
QMetaObject : : invokeMethod ( root , " showLastRowChildsLastChild " ) ;
QCOMPARE ( root - > property ( " test3_1 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test3_2 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test3_3 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test3_4 " ) . toBool ( ) , true ) ;
QMetaObject : : invokeMethod ( root , " showLastRowChild " ) ;
QCOMPARE ( root - > property ( " test4_1 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test4_2 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test4_3 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test4_4 " ) . toBool ( ) , true ) ;
2012-10-22 15:57:05 +00:00
QString warning1 = testFileUrl ( " visiblechildren.qml " ) . toString ( ) + " :87: TypeError: Cannot read property 'visibleChildren' of null " ;
2011-12-05 19:21:33 +00:00
QTest : : ignoreMessage ( QtWarningMsg , qPrintable ( warning1 ) ) ;
QMetaObject : : invokeMethod ( root , " tryWriteToReadonlyVisibleChildren " ) ;
QMetaObject : : invokeMethod ( root , " reparentVisibleItem3 " ) ;
QCOMPARE ( root - > property ( " test6_1 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test6_2 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test6_3 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test6_4 " ) . toBool ( ) , true ) ;
QMetaObject : : invokeMethod ( root , " reparentImlicitlyInvisibleItem4_1 " ) ;
QCOMPARE ( root - > property ( " test7_1 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test7_2 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test7_3 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test7_4 " ) . toBool ( ) , true ) ;
// FINALLY TEST THAT EVERYTHING IS AS EXPECTED
QCOMPARE ( root - > property ( " test8_1 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test8_2 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test8_3 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test8_4 " ) . toBool ( ) , true ) ;
QCOMPARE ( root - > property ( " test8_5 " ) . toBool ( ) , true ) ;
2012-07-11 07:32:16 +00:00
delete window ;
2011-12-05 19:21:33 +00:00
}
2012-02-15 03:20:56 +00:00
void tst_QQuickItem : : parentLoop ( )
{
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-02-15 03:20:56 +00:00
2016-11-16 13:22:36 +00:00
# if QT_CONFIG(regularexpression)
2015-10-19 11:01:13 +00:00
QRegularExpression msgRegexp = QRegularExpression ( " QQuickItem::setParentItem: Parent QQuickItem \\ (.* \\ ) is already part of the subtree of QQuickItem \\ (.* \\ ) " ) ;
QTest : : ignoreMessage ( QtWarningMsg , msgRegexp ) ;
# endif
2012-07-11 07:32:16 +00:00
window - > setSource ( testFileUrl ( " parentLoop.qml " ) ) ;
2012-02-15 03:20:56 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * root = qobject_cast < QQuickItem * > ( window - > rootObject ( ) ) ;
2012-02-15 03:20:56 +00:00
QVERIFY ( root ) ;
QQuickItem * item1 = root - > findChild < QQuickItem * > ( " item1 " ) ;
QVERIFY ( item1 ) ;
QCOMPARE ( item1 - > parentItem ( ) , root ) ;
QQuickItem * item2 = root - > findChild < QQuickItem * > ( " item2 " ) ;
QVERIFY ( item2 ) ;
QCOMPARE ( item2 - > parentItem ( ) , item1 ) ;
2012-07-11 07:32:16 +00:00
delete window ;
2012-02-15 03:20:56 +00:00
}
2012-02-26 16:26:53 +00:00
void tst_QQuickItem : : contains_data ( )
{
QTest : : addColumn < bool > ( " circleTest " ) ;
QTest : : addColumn < bool > ( " insideTarget " ) ;
QTest : : addColumn < QList < QPoint > > ( " points " ) ;
QList < QPoint > points ;
points < < QPoint ( 176 , 176 )
< < QPoint ( 176 , 226 )
< < QPoint ( 226 , 176 )
< < QPoint ( 226 , 226 )
< < QPoint ( 150 , 200 )
< < QPoint ( 200 , 150 )
< < QPoint ( 200 , 250 )
< < QPoint ( 250 , 200 ) ;
QTest : : newRow ( " hollow square: testing points inside " ) < < false < < true < < points ;
points . clear ( ) ;
points < < QPoint ( 162 , 162 )
< < QPoint ( 162 , 242 )
< < QPoint ( 242 , 162 )
< < QPoint ( 242 , 242 )
< < QPoint ( 200 , 200 )
< < QPoint ( 175 , 200 )
< < QPoint ( 200 , 175 )
< < QPoint ( 200 , 228 )
< < QPoint ( 228 , 200 )
< < QPoint ( 200 , 122 )
< < QPoint ( 122 , 200 )
< < QPoint ( 200 , 280 )
< < QPoint ( 280 , 200 ) ;
QTest : : newRow ( " hollow square: testing points outside " ) < < false < < false < < points ;
points . clear ( ) ;
points < < QPoint ( 174 , 174 )
< < QPoint ( 174 , 225 )
< < QPoint ( 225 , 174 )
< < QPoint ( 225 , 225 )
< < QPoint ( 165 , 200 )
< < QPoint ( 200 , 165 )
< < QPoint ( 200 , 235 )
< < QPoint ( 235 , 200 ) ;
QTest : : newRow ( " hollow circle: testing points inside " ) < < true < < true < < points ;
points . clear ( ) ;
points < < QPoint ( 160 , 160 )
< < QPoint ( 160 , 240 )
< < QPoint ( 240 , 160 )
< < QPoint ( 240 , 240 )
< < QPoint ( 200 , 200 )
< < QPoint ( 185 , 185 )
< < QPoint ( 185 , 216 )
< < QPoint ( 216 , 185 )
< < QPoint ( 216 , 216 )
< < QPoint ( 145 , 200 )
< < QPoint ( 200 , 145 )
< < QPoint ( 255 , 200 )
< < QPoint ( 200 , 255 ) ;
QTest : : newRow ( " hollow circle: testing points outside " ) < < true < < false < < points ;
}
void tst_QQuickItem : : contains ( )
{
QFETCH ( bool , circleTest ) ;
QFETCH ( bool , insideTarget ) ;
QFETCH ( QList < QPoint > , points ) ;
2018-02-21 09:41:54 +00:00
QQuickView * window = new QQuickView ( nullptr ) ;
2012-07-11 07:32:16 +00:00
window - > rootContext ( ) - > setContextProperty ( " circleShapeTest " , circleTest ) ;
window - > setBaseSize ( QSize ( 400 , 400 ) ) ;
window - > setSource ( testFileUrl ( " hollowTestItem.qml " ) ) ;
window - > show ( ) ;
2012-10-19 16:05:01 +00:00
window - > requestActivate ( ) ;
2012-07-19 12:13:14 +00:00
QVERIFY ( QTest : : qWaitForWindowActive ( window ) ) ;
2015-07-24 13:32:22 +00:00
QCOMPARE ( QGuiApplication : : focusWindow ( ) , window ) ;
2012-02-26 16:26:53 +00:00
2012-07-11 07:32:16 +00:00
QQuickItem * root = qobject_cast < QQuickItem * > ( window - > rootObject ( ) ) ;
2012-02-26 16:26:53 +00:00
QVERIFY ( root ) ;
HollowTestItem * hollowItem = root - > findChild < HollowTestItem * > ( " hollowItem " ) ;
QVERIFY ( hollowItem ) ;
foreach ( const QPoint & point , points ) {
// check mouse hover
2012-07-11 07:32:16 +00:00
QTest : : mouseMove ( window , point ) ;
2021-03-25 14:08:11 +00:00
QTRY_COMPARE ( hollowItem - > isHovered ( ) , insideTarget ) ;
2012-02-26 16:26:53 +00:00
// check mouse press
2018-02-21 09:41:54 +00:00
QTest : : mousePress ( window , Qt : : LeftButton , Qt : : NoModifier , point ) ;
2021-03-25 14:08:11 +00:00
QTRY_COMPARE ( hollowItem - > isPressed ( ) , insideTarget ) ;
2012-02-26 16:26:53 +00:00
// check mouse release
2018-02-21 09:41:54 +00:00
QTest : : mouseRelease ( window , Qt : : LeftButton , Qt : : NoModifier , point ) ;
2021-03-25 14:08:11 +00:00
QTRY_COMPARE ( hollowItem - > isPressed ( ) , false ) ;
2012-02-26 16:26:53 +00:00
}
2012-07-11 07:32:16 +00:00
delete window ;
2012-02-26 16:26:53 +00:00
}
2012-07-24 03:40:55 +00:00
void tst_QQuickItem : : childAt ( )
{
QQuickItem parent ;
QQuickItem child1 ;
child1 . setX ( 0 ) ;
child1 . setY ( 0 ) ;
child1 . setWidth ( 100 ) ;
child1 . setHeight ( 100 ) ;
child1 . setParentItem ( & parent ) ;
QQuickItem child2 ;
child2 . setX ( 50 ) ;
child2 . setY ( 50 ) ;
child2 . setWidth ( 100 ) ;
child2 . setHeight ( 100 ) ;
child2 . setParentItem ( & parent ) ;
QQuickItem child3 ;
child3 . setX ( 0 ) ;
child3 . setY ( 200 ) ;
child3 . setWidth ( 50 ) ;
child3 . setHeight ( 50 ) ;
child3 . setParentItem ( & parent ) ;
QCOMPARE ( parent . childAt ( 0 , 0 ) , & child1 ) ;
2015-04-21 05:44:24 +00:00
QCOMPARE ( parent . childAt ( 0 , 99 ) , & child1 ) ;
2012-07-24 03:40:55 +00:00
QCOMPARE ( parent . childAt ( 25 , 25 ) , & child1 ) ;
QCOMPARE ( parent . childAt ( 25 , 75 ) , & child1 ) ;
QCOMPARE ( parent . childAt ( 75 , 25 ) , & child1 ) ;
QCOMPARE ( parent . childAt ( 75 , 75 ) , & child2 ) ;
2015-04-21 05:44:24 +00:00
QCOMPARE ( parent . childAt ( 149 , 149 ) , & child2 ) ;
2012-07-24 03:40:55 +00:00
QCOMPARE ( parent . childAt ( 25 , 200 ) , & child3 ) ;
2018-02-21 09:41:54 +00:00
QCOMPARE ( parent . childAt ( 0 , 150 ) , static_cast < QQuickItem * > ( nullptr ) ) ;
QCOMPARE ( parent . childAt ( 300 , 300 ) , static_cast < QQuickItem * > ( nullptr ) ) ;
2012-07-24 03:40:55 +00:00
}
2014-04-28 11:30:04 +00:00
void tst_QQuickItem : : grab ( )
{
2017-08-30 12:39:28 +00:00
if ( ( QGuiApplication : : platformName ( ) = = QLatin1String ( " offscreen " ) )
| | ( QGuiApplication : : platformName ( ) = = QLatin1String ( " minimal " ) ) )
2020-03-18 13:24:29 +00:00
QSKIP ( " Skipping due to grabToImage not functional on offscreen/minimal platforms " ) ;
2017-08-30 12:39:28 +00:00
2014-04-28 11:30:04 +00:00
QQuickView view ;
view . setSource ( testFileUrl ( " grabToImage.qml " ) ) ;
view . show ( ) ;
2018-02-21 08:16:14 +00:00
QVERIFY ( QTest : : qWaitForWindowExposed ( & view ) ) ;
2014-04-28 11:30:04 +00:00
QQuickItem * root = qobject_cast < QQuickItem * > ( view . rootObject ( ) ) ;
QVERIFY ( root ) ;
QQuickItem * item = root - > findChild < QQuickItem * > ( " myItem " ) ;
QVERIFY ( item ) ;
2016-11-16 13:22:36 +00:00
# if QT_CONFIG(opengl)
2014-04-28 11:30:04 +00:00
{ // Default size (item is 100x100)
QSharedPointer < QQuickItemGrabResult > result = item - > grabToImage ( ) ;
QSignalSpy spy ( result . data ( ) , SIGNAL ( ready ( ) ) ) ;
QTRY_VERIFY ( spy . size ( ) > 0 ) ;
QVERIFY ( ! result - > url ( ) . isEmpty ( ) ) ;
QImage image = result - > image ( ) ;
QCOMPARE ( image . pixel ( 0 , 0 ) , qRgb ( 255 , 0 , 0 ) ) ;
QCOMPARE ( image . pixel ( 99 , 99 ) , qRgb ( 0 , 0 , 255 ) ) ;
}
{ // Smaller size
QSharedPointer < QQuickItemGrabResult > result = item - > grabToImage ( QSize ( 50 , 50 ) ) ;
QVERIFY ( ! result . isNull ( ) ) ;
QSignalSpy spy ( result . data ( ) , SIGNAL ( ready ( ) ) ) ;
QTRY_VERIFY ( spy . size ( ) > 0 ) ;
QVERIFY ( ! result - > url ( ) . isEmpty ( ) ) ;
QImage image = result - > image ( ) ;
QCOMPARE ( image . pixel ( 0 , 0 ) , qRgb ( 255 , 0 , 0 ) ) ;
QCOMPARE ( image . pixel ( 49 , 49 ) , qRgb ( 0 , 0 , 255 ) ) ;
}
2016-04-25 10:09:44 +00:00
# endif
2014-04-28 11:30:04 +00:00
}
2015-11-19 06:46:01 +00:00
void tst_QQuickItem : : isAncestorOf ( )
{
QQuickItem parent ;
QQuickItem sub1 ;
sub1 . setParentItem ( & parent ) ;
QQuickItem child1 ;
child1 . setParentItem ( & sub1 ) ;
QQuickItem child2 ;
child2 . setParentItem ( & sub1 ) ;
QQuickItem sub2 ;
sub2 . setParentItem ( & parent ) ;
QQuickItem child3 ;
child3 . setParentItem ( & sub2 ) ;
QQuickItem child4 ;
child4 . setParentItem ( & sub2 ) ;
QVERIFY ( parent . isAncestorOf ( & sub1 ) ) ;
QVERIFY ( parent . isAncestorOf ( & sub2 ) ) ;
QVERIFY ( parent . isAncestorOf ( & child1 ) ) ;
QVERIFY ( parent . isAncestorOf ( & child2 ) ) ;
QVERIFY ( parent . isAncestorOf ( & child3 ) ) ;
QVERIFY ( parent . isAncestorOf ( & child4 ) ) ;
QVERIFY ( sub1 . isAncestorOf ( & child1 ) ) ;
QVERIFY ( sub1 . isAncestorOf ( & child2 ) ) ;
QVERIFY ( ! sub1 . isAncestorOf ( & child3 ) ) ;
QVERIFY ( ! sub1 . isAncestorOf ( & child4 ) ) ;
QVERIFY ( sub2 . isAncestorOf ( & child3 ) ) ;
QVERIFY ( sub2 . isAncestorOf ( & child4 ) ) ;
QVERIFY ( ! sub2 . isAncestorOf ( & child1 ) ) ;
QVERIFY ( ! sub2 . isAncestorOf ( & child2 ) ) ;
QVERIFY ( ! sub1 . isAncestorOf ( & sub1 ) ) ;
QVERIFY ( ! sub2 . isAncestorOf ( & sub2 ) ) ;
}
2012-02-26 16:26:53 +00:00
2021-06-11 12:19:17 +00:00
/*
Verify that a nested item ' s palette responds to changes of the enabled state
and of the window ' s activation state by switching the current color group .
*/
void tst_QQuickItem : : colorGroup ( )
{
QQuickView view ;
view . setSource ( testFileUrl ( " colorgroup.qml " ) ) ;
view . show ( ) ;
QVERIFY ( QTest : : qWaitForWindowExposed ( & view ) ) ;
QQuickItem * root = qobject_cast < QQuickItem * > ( view . rootObject ( ) ) ;
QQuickItem * background = root - > findChild < QQuickItem * > ( " background " ) ;
QVERIFY ( background ) ;
QQuickItem * foreground = root - > findChild < QQuickItem * > ( " foreground " ) ;
QVERIFY ( foreground ) ;
QQuickPalette * palette = foreground - > property ( " palette " ) . value < QQuickPalette * > ( ) ;
QVERIFY ( palette ) ;
view . show ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( & view ) ) ;
QCOMPARE ( palette - > currentColorGroup ( ) , QPalette : : Active ) ;
QCOMPARE ( foreground - > property ( " color " ) . value < QColor > ( ) , palette - > active ( ) - > base ( ) ) ;
background - > setEnabled ( false ) ;
QCOMPARE ( palette - > currentColorGroup ( ) , QPalette : : Disabled ) ;
QCOMPARE ( foreground - > property ( " color " ) . value < QColor > ( ) , palette - > disabled ( ) - > base ( ) ) ;
QWindow activationThief ;
activationThief . show ( ) ;
activationThief . requestActivate ( ) ;
QVERIFY ( QTest : : qWaitForWindowActive ( & activationThief ) ) ;
QCOMPARE ( palette - > currentColorGroup ( ) , QPalette : : Disabled ) ;
QCOMPARE ( foreground - > property ( " color " ) . value < QColor > ( ) , palette - > disabled ( ) - > base ( ) ) ;
background - > setEnabled ( true ) ;
QCOMPARE ( palette - > currentColorGroup ( ) , QPalette : : Inactive ) ;
QCOMPARE ( foreground - > property ( " color " ) . value < QColor > ( ) , palette - > inactive ( ) - > base ( ) ) ;
}
2011-10-14 08:51:42 +00:00
QTEST_MAIN ( tst_QQuickItem )
2011-04-27 12:13:26 +00:00
2011-10-14 08:51:42 +00:00
# include "tst_qquickitem.moc"