From e0cf43d8e173406e9d64737aada6de74fd0df774 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Tue, 9 Jul 2019 10:16:04 +0200 Subject: [PATCH] doing the GridLayoutPage in Qml to demonstrate how parts written in Qml can be embedded into C++ --- examples/layouts/GridLayoutPage.cpp | 42 +++++++--------------- examples/layouts/GridLayoutPage.h | 2 +- examples/layouts/TestRectangle.cpp | 28 +++++++++++---- examples/layouts/TestRectangle.h | 13 ++++++- examples/layouts/layouts.pro | 5 ++- examples/layouts/layouts.qml | 54 +++++++++++++++++++++++++++++ examples/layouts/layouts.qrc | 5 +++ examples/layouts/main.cpp | 23 +++--------- 8 files changed, 115 insertions(+), 57 deletions(-) create mode 100644 examples/layouts/layouts.qml create mode 100644 examples/layouts/layouts.qrc diff --git a/examples/layouts/GridLayoutPage.cpp b/examples/layouts/GridLayoutPage.cpp index 61518554..32acc223 100644 --- a/examples/layouts/GridLayoutPage.cpp +++ b/examples/layouts/GridLayoutPage.cpp @@ -4,49 +4,31 @@ *****************************************************************************/ #include "GridLayoutPage.h" -#include "TestRectangle.h" -#include #include +#include -namespace +static QQuickItem* qskCreateItemQml( const QUrl& url ) { - class Box : public QskGridBox - { - public: - Box( QQuickItem* parent = nullptr ) - : QskGridBox( parent ) - { - setObjectName( "GridBox" ); + QQmlEngine engine( nullptr ); - setBackgroundColor( Qt::white ); - setMargins( 10 ); + QQmlComponent component( &engine ); + component.loadUrl( url, QQmlComponent::PreferSynchronous ); - addItem( new TestRectangle( "PaleVioletRed" ), 0, 0, 1, 2 ); - addItem( new TestRectangle( "DarkSeaGreen" ), 1, 0, 2, 1 ); - addItem( new TestRectangle( "SkyBlue" ), 2, 1, 1, 1 ); - addItem( new TestRectangle( "NavajoWhite" ), 0, 2, -1, 1 ); - - setRowStretchFactor( 0, 1 ); - setRowStretchFactor( 1, 2 ); - setRowStretchFactor( 2, 1 ); - } - - void mirror() - { - setLayoutMirroring( !layoutMirroring() ); - } - }; + return qobject_cast< QQuickItem* >( component.create() ); } GridLayoutPage::GridLayoutPage( QQuickItem* parent ) : QskControl( parent ) { -#if 1 setMargins( 10 ); setBackgroundColor( QskRgbValue::LightSteelBlue ); -#endif setAutoLayoutChildren( true ); - new Box( this ); + + if ( auto item = qskCreateItemQml( QUrl( "qrc:/qml/layouts.qml" ) ) ) + { + item->setParentItem( this ); + item->setParent( this ); + } } diff --git a/examples/layouts/GridLayoutPage.h b/examples/layouts/GridLayoutPage.h index 38812b68..de08d3d4 100644 --- a/examples/layouts/GridLayoutPage.h +++ b/examples/layouts/GridLayoutPage.h @@ -4,7 +4,7 @@ *****************************************************************************/ #ifndef GRID_LAYOUT_PAGE -#define GRID_LAYOUT_PAGE 1 +#define GRID_LAYOUT_PAGE #include diff --git a/examples/layouts/TestRectangle.cpp b/examples/layouts/TestRectangle.cpp index 948fb798..fc5e9f9a 100644 --- a/examples/layouts/TestRectangle.cpp +++ b/examples/layouts/TestRectangle.cpp @@ -5,17 +5,33 @@ #include "TestRectangle.h" -#include - -TestRectangle::TestRectangle( const char* colorName, QQuickItem* parent ) +TestRectangle::TestRectangle( QQuickItem* parent ) : QskTextLabel( parent ) { - setObjectName( colorName ); - setAlignment( Qt::AlignCenter ); - setBackgroundColor( colorName ); setPreferredSize( 10, 10 ); setSizePolicy( QskSizePolicy::Minimum, QskSizePolicy::Minimum ); setFocusPolicy( Qt::TabFocus ); // for checking the focus tab chain } + +TestRectangle::TestRectangle( const char* colorName, QQuickItem* parent ) + : TestRectangle( parent ) +{ + setColorName( colorName ); +} + +void TestRectangle::setColorName( const QString& colorName ) +{ + m_colorName = colorName; + + setObjectName( colorName ); + setBackgroundColor( colorName ); +} + +QString TestRectangle::colorName() const +{ + return m_colorName; +} + +#include "moc_TestRectangle.cpp" diff --git a/examples/layouts/TestRectangle.h b/examples/layouts/TestRectangle.h index 05c31082..ca0896c0 100644 --- a/examples/layouts/TestRectangle.h +++ b/examples/layouts/TestRectangle.h @@ -4,14 +4,25 @@ *****************************************************************************/ #ifndef TEST_RECTANGLE -#define TEST_RECTANGLE 1 +#define TEST_RECTANGLE #include class TestRectangle : public QskTextLabel { + Q_OBJECT + + Q_PROPERTY( QString color READ colorName WRITE setColorName ) + public: + TestRectangle( QQuickItem* parent = nullptr ); TestRectangle( const char* colorName, QQuickItem* parent = nullptr ); + + void setColorName( const QString& ); + QString colorName() const; + + private: + QString m_colorName; }; #endif diff --git a/examples/layouts/layouts.pro b/examples/layouts/layouts.pro index bf8a6f23..5ce8b85c 100644 --- a/examples/layouts/layouts.pro +++ b/examples/layouts/layouts.pro @@ -1,4 +1,7 @@ -CONFIG += qskexample +CONFIG += qskexample qskqmlexport + +RESOURCES += \ + layouts.qrc HEADERS += \ TestRectangle.h \ diff --git a/examples/layouts/layouts.qml b/examples/layouts/layouts.qml new file mode 100644 index 00000000..59462744 --- /dev/null +++ b/examples/layouts/layouts.qml @@ -0,0 +1,54 @@ +import QtQuick 2.0 +import Skinny 1.0 +import Test 1.0 + +GridBox +{ + margins: 10 + autoFillBackground : true + + background + { + stops: [ + { position: 0.0, color: "White" }, + { position: 1.0, color: "White" }, + ] + } + + TestRectangle + { + id: paleVioletRed + color: "PaleVioletRed" + } + + TestRectangle + { + id: darkSeaGreen + color: "DarkSeaGreen" + } + + TestRectangle + { + id: skyBlue + color: "SkyBlue" + } + + TestRectangle + { + id: navajoWhite + color: "NavajoWhite" + } + + Component.onCompleted: + { + addItem( paleVioletRed, 0, 0, 1, 2 ); + addItem( darkSeaGreen, 1, 0, 2, 1 ); + addItem( skyBlue, 2, 1, 1, 1 ); + addItem( navajoWhite, 0, 2, -1, 1 ); + + setRowStretchFactor( 0, 1 ); + setRowStretchFactor( 1, 2 ); + setRowStretchFactor( 2, 1 ); + } +} + diff --git a/examples/layouts/layouts.qrc b/examples/layouts/layouts.qrc new file mode 100644 index 00000000..26e81af0 --- /dev/null +++ b/examples/layouts/layouts.qrc @@ -0,0 +1,5 @@ + + + layouts.qml + + diff --git a/examples/layouts/main.cpp b/examples/layouts/main.cpp index f8642efd..820fff46 100644 --- a/examples/layouts/main.cpp +++ b/examples/layouts/main.cpp @@ -8,6 +8,7 @@ #include "LinearLayoutPage.h" #include "GridLayoutPage.h" #include "StackLayoutPage.h" +#include "TestRectangle.h" #include #include @@ -21,31 +22,17 @@ #include #include #include +#include #include -class DummyLabel : public QskTextLabel -{ - public: - DummyLabel( const QString& text, QQuickItem* parent = nullptr ) - : QskTextLabel( text, parent ) - { - setBackgroundColor( Qt::black ); - setTextColor( Qt::white ); - setFontRole( QskSkin::MediumFont ); - - setWrapMode( QskTextOptions::WordWrap ); - - setAlignment( Qt::AlignCenter ); - setSizePolicy( QskSizePolicy::Ignored, QskSizePolicy::Ignored ); - } -}; - int main( int argc, char* argv[] ) { #ifdef ITEM_STATISTICS QskObjectCounter counter( true ); #endif + QskQml::registerTypes(); + qmlRegisterType< TestRectangle >( "Test", 1, 0, "TestRectangle" ); QGuiApplication app( argc, argv ); @@ -69,7 +56,7 @@ int main( int argc, char* argv[] ) tabView->addTab( "Stack Layout", new StackLayoutPage() ); - tabView->setCurrentIndex( 4 ); + tabView->setCurrentIndex( 0 ); QSize size( 800, 600 ); size = size.expandedTo( tabView->sizeHint().toSize() );