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 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?

  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?

    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,
    _

  3. 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
  •  
Qt is a trademark of The Qt Company.