Results 1 to 7 of 7

Thread: Creating dynamic tab in Qml TabView

  1. #1
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Creating dynamic tab in Qml TabView

    I'm creating tabs dynamically in QML TabView on Component.onCompleted

    Qt Code:
    1. var t = tabview.addTab("tab", Qt.createComponent("TabItem.qml"));
    2.  
    3. console.log("Creating Tabs " + t)
    4. if(t != null)
    5. {
    6. var last = tabview.count-1;
    7. tabview.getTab(last).active = true;
    8. t.item.partsModel = model.getMenuModel(i)
    9. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that I need to set an objectName to the dynamic tab to set properties from Qt.
    Can you suggest a method for the same?

  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: Creating dynamic tab in Qml TabView

    The object name is a property called objectName in QML.

    However, "set properties from Qt" sounds like you are trying to manipulate a QML created object from C++.
    There are usually better way than doing that.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating dynamic tab in Qml TabView

    Is it possible to set the objectName to a dynamically created tab like the way listed here?
    Qt Code:
    1. QQuickItem* tab= view->rootObject()->findChild<QQuickItem*>("dynamicTab");
    2.  
    3. QQuickItem* tabInfoPanel = tab->findChild<QQuickItem*>("tabInfoPanel");
    4.  
    5. tabInfoPanel->setProperty("visible", false);
    To copy to clipboard, switch view to plain text mode 
    I get runtime error from the tab pointer saying it's not initialized.

    owever, "set properties from Qt" sounds like you are trying to manipulate a QML created object from C++.
    There are usually better way than doing that.
    Can you suggest me them?

  4. #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: Creating dynamic tab in Qml TabView

    Quote Originally Posted by volcano View Post
    Is it possible to set the objectName to a dynamically created tab like the way listed here?
    It is a standard property, defined in QObject.
    You can set it and read it like any other property.

    Quote Originally Posted by volcano View Post
    Can you suggest me them?
    The most common way is for C++ to expose values via QObject properties or as a list model and then bind to these values in QML.
    This way you only have a dependency from QML to C++, usually QtQuick to C++, so GUI to core, not the other way around.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating dynamic tab in Qml TabView

    I understand that's it's better to keep it in the direction from C++ to QML and not make it bidirectional

    However, the app has been designed with the requirement to be bidirectional to set certain properties.

    Here's the code implementation but I still get this error, that tab pointer isn't initialized.

    Qt Code:
    1. var last = tabview.count-1;
    2. tabview.getTab(last).active = true;
    3. t.item.partsModel = model.getMenuModel(i)
    4. t.objectName = "tabItem"
    To copy to clipboard, switch view to plain text mode 

    To recover from Qt/C++, done in the overridden class using QQuickView
    Qt Code:
    1. QQuickItem* tab= view->rootObject()->findChild<QQuickItem*>("tabItem");
    To copy to clipboard, switch view to plain text mode 

    Can you suggest what am I missing?

  6. #6
    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: Creating dynamic tab in Qml TabView

    Quote Originally Posted by volcano View Post
    I understand that's it's better to keep it in the direction from C++ to QML and not make it bidirectional

    However, the app has been designed with the requirement to be bidirectional to set certain properties.
    Ok, but it is often better to invest time in improving the design than to make the implementation of a bad design work.

    Quote Originally Posted by volcano View Post
    Can you suggest what am I missing?
    Maybe the tab isn't part of the QObject tree, QQuickItems don't necessarily have a QObject parent.

    You could try manuyll recursing over the childItems() or calling a C++ function from QML when you have created the tab.

    Cheers,
    _

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

    volcano (15th March 2016)

  8. #7
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating dynamic tab in Qml TabView

    Thanks anda_skoa

    Will try the suggestion.

Similar Threads

  1. Creating draggable dynamic items
    By Annihilator in forum Qt Quick
    Replies: 0
    Last Post: 8th July 2015, 17:27
  2. Creating a dynamic QML component from a non Qt thread
    By Valerian in forum Qt Programming
    Replies: 3
    Last Post: 5th July 2015, 13:30
  3. How to arrange tabs of TabView in multiple rows?
    By TheIndependentAquarius in forum Qt Quick
    Replies: 2
    Last Post: 20th March 2015, 09:08
  4. TabView issue
    By MattieB in forum Qt Quick
    Replies: 1
    Last Post: 7th April 2014, 08:52
  5. Replies: 3
    Last Post: 25th September 2011, 14:44

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.