Results 1 to 8 of 8

Thread: QML App with toolbar and subView?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2015
    Location
    Austria
    Posts
    23
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default QML App with toolbar and subView?

    Hi,

    I am creating my first QML application and i am a Little bit confused.
    i want to have an application window with a toolbar and a Container where i can Show "SUB" views (maybe StackView as container) which will Show me other views which i want to load via C++

    So i am able to create an Application Window with the QQmlApplicationEngine (with QQuickView i can't create a Toolbar because the view itself creates an application window) and to load my main.qml file.
    The Toolbar is shown. Thats good.
    Now i want to add the "Container view" which should show qml files i want to set/load with C++
    That means first i will show a ListView, after a list view item is clicked i want to show another view, aso...

    Each qml view should have a C++ Class derived from QObject and this class will handle the signals from the current qml file loaded in the "Container view"

    an example with pseudo code:

    Qt Code:
    1. QML
    2. ApplicationWindow {
    3. toolBar: Rectangle {
    4. ...
    5. }
    6.  
    7. ContentView {
    8. id: containerView
    9. }
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. C++
    2. QQmlApplicationEngine engine
    3. appEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    4. // is it possible to have something like a Container view ant to get it from the appEngine?
    5. containerView = appEngine.getObject("containerView");
    6.  
    7. // ist it possible to set the dataContext for the load qml for the view model?
    8. context = new DateContext();
    9. containerView.setSource("qml", context);
    10.  
    11. // is it possible to connect the signals from the qml to a C++ object ??
    12. QObject::connect(qtVewObject, SIGNAL(sigTest(QString)), classObjectWithReceivesSignals, SLOT(slotSignalTest(QString)));
    To copy to clipboard, switch view to plain text mode 


    Is there a nice way to achieve this.
    I've looked around in the Internet but i dont really understand if there is such thing like a "Container view" which i can embedd in the main QML and where i can load other qml files.
    Maybe i can use a StackView for the Container view but i do not Need the ability of pushing an popping views (i will do that by my own)

    Are there any suggestions for me?
    THANKS!
    Last edited by ChriD; 20th January 2016 at 15:33.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML App with toolbar and subView?

    A Loader element can load any QML file and will resize that QML file's root item to its own size.

    You can easily export a C++ object with a property that holds that QML file name and bind the loader's source property to it.
    The Loader will load the new file whenver the C++ object changes the property.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    ChriD (21st January 2016)

  4. #3
    Join Date
    Dec 2015
    Location
    Austria
    Posts
    23
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: QML App with toolbar and subView?

    Hi, Thank You for your answer!
    So it should be something like this?!


    Qt Code:
    1. C++
    2. class View : public QObject
    3. {
    4. Q_OBJECT
    5. Q_PROPERTY( QString qmlViewPath READ getQmlViewPath )
    6.  
    7. public:
    8. explicit View(QObject *parent = 0);
    9. virtual QString getQmlViewPath();
    10. };
    11. }
    12.  
    13.  
    14.  
    15. void main()
    16. {
    17. View firstView;
    18. View secondView;
    19. QQmlApplicationEngine appEngine;
    20. appEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    21. // Show view 1
    22. appEngine.rootContext()->setContextProperty("viewClassObject", (QObject *)&firstView);
    23. // ...
    24. // Show view 2
    25. appEngine.rootContext()->setContextProperty("viewClassObject", (QObject *)&secondView);
    26. // ...
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QML
    2. ApplicationWindow {
    3. visible: true
    4.  
    5. toolBar: Rectangle{
    6. ....
    7. }
    8.  
    9. Loader {
    10. id: loaderViewContainer
    11. anchors.fill: parent
    12. focus: true
    13. source: viewClassObject.qmlViewPath
    14. }
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    Would the change of the context of the root force the loader to load/reload the qml?
    Or would it be better to create a "ViewController" class which contains all the viewsClasses with their contexts so i only have to set the root context (ViewController) once and then set the current view context for each view
    e.g.:

    Qt Code:
    1. void main()
    2. {
    3. ViewController viewController;
    4. QQmlApplicationEngine appEngine;
    5. appEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    6. // Show view which is provided by the viewControllerClass as property
    7. appEngine.rootContext()->setContextProperty("viewControllerObject", (QObject *)&viewController);
    8. }
    9.  
    10.  
    11. changeViewSignalReceipt()
    12. {
    13. // view will be changed automatically by property of the view Controller, but we have to set the context for the loaded view
    14. appEngine.rootContext()->setContextProperty("viewClassObject", (QObject *)&secondView);
    15. // or do i have to set the "view class" context to the loaderObject?
    16. }
    To copy to clipboard, switch view to plain text mode 

    Is it possible to set multiple ContextProperties to the rootContext, or do i have to get the Loader Object and set the view Context there?
    And how would i get the loader Object from the QML to set its context. Or can i bind the context of the loader object from the viewController?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML App with toolbar and subView?

    Quote Originally Posted by ChriD View Post
    So it should be something like this?!
    Not quite.
    Instead of setting a second object with the same id, you would change the value of the property of the already exported object.
    The Q_PROPERTY will need a NOTIFY signal so that the C++ object can tell teh QML engine that the property has to be re-read.

    Quote Originally Posted by ChriD View Post
    Is it possible to set multiple ContextProperties to the rootContext, or do i have to get the Loader Object and set the view Context there?
    You can set many objects as long as their "names" are unique (basically like id values in QML).
    You can also export a list of objects or even a model.

    Quote Originally Posted by ChriD View Post
    And how would i get the loader Object from the QML to set its context. Or can i bind the context of the loader object from the viewController?
    I don't quite get what you mean there.
    What you do internally in your exported object when it changes the view path is up to you.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    ChriD (21st January 2016)

Similar Threads

  1. About Qt Toolbar
    By korg1988 in forum Qt Programming
    Replies: 5
    Last Post: 10th February 2013, 19:12
  2. Replies: 5
    Last Post: 11th September 2010, 14:29
  3. Toolbar order
    By wconstan in forum Newbie
    Replies: 1
    Last Post: 19th December 2009, 21:21
  4. Toolbar
    By assismvla in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2008, 17:42
  5. right justify toolbar
    By magland in forum Qt Programming
    Replies: 5
    Last Post: 16th April 2008, 18:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.