Results 1 to 8 of 8

Thread: Change a TabView height following its tab child

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

    Default Change a TabView height following its tab child

    I wanted the tabview height to be based on the sum of the children rect.

    Eg: tab1 has height of 24(one label of height 24), so when tab1 is selected the tabview height should be 24
    however when tab2 has 3 labels each of height 24, so when tab2 is selected the tabview height should be 72.

    Here's a sample code
    Qt Code:
    1. TabView {
    2. Tab {
    3. title: "tab1"
    4.  
    5. RowLayout {
    6. Text {
    7. text: "Text1"
    8. }
    9. }
    10. }
    11. Tab {
    12. title: "tab2"
    13.  
    14. RowLayout {
    15. Text {
    16. text: "Text1"
    17. }
    18. Text {
    19. text: "Text2"
    20. }
    21. Text {
    22. text: "Text3"
    23. }
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    Kindly advice how to achieve this

  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: Change a TabView height following its tab child

    Since both your tabs have rows of buttons, they should have the same height.

    But in general you could bind the TabView's size properties to expressions that contain the respective properties of the tabs, e.g. something like
    Qt Code:
    1. height: Math.max(tab1.height, tab2.height)
    To copy to clipboard, switch view to plain text mode 

    If you have an unknown number of tabs, you might need a function in the tab view that iterates through the tabs and calculates the max, and call this function from within the tabs when they change size.

    Cheers,
    _

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

    Default Re: Change a TabView height following its tab child

    Thanks anda_skoa for the input

    this function for two tabs "height: Math.max(tab1.height, tab2.height)" will return 72 always.

    I want the height of the tabview to be based on the total height of all children. Can this be done?

    Kindly advice

  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: Change a TabView height following its tab child

    Maybe I didn't understand you correctly.

    Do you want the tab view to be the size of the currently active tab?
    This could work for that:
    Qt Code:
    1. height: getTab(currentIndex).height
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

    Default Re: Change a TabView height following its tab child

    Hi anda_skoa,

    Thanks for the suggestion. But I'm facing a small problem

    Here's the way I set the height in the TabView
    Qt Code:
    1. height: {
    2. var height
    3. console.log("$$$$$$$$$$$$$$$$$$ Current Index " + currentIndex)
    4. console.log("$$$$$$$$$$$$$$$$$$ Tab at index " + getTab(currentIndex))
    5. height = getTab(currentIndex).childrenRect.height + 40
    6. console.log("$$$$$$$$$$$$$$$$$ Final height" + height)
    7. return height
    8. }
    To copy to clipboard, switch view to plain text mode 

    Here's the output
    qml: $$$$$$$$$$$$$$$$$$ Current Index 0
    qml: $$$$$$$$$$$$$$$$$$ Tab at index undefined
    qrc:/qml/controls/CustomTabView.qml:28: TypeError: Cannot read property 'childrenRect' of undefined

    It seems the tab isn't ready at the time, is there a workaround for this problem?

    Kindly advice

  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: Change a TabView height following its tab child

    You could check the result of getTab before calling anything on it.

    Qt Code:
    1. readonly property Tab currentTab: getTab(currentIndex)
    2. height: currentTab ? currentTab.childrenRect.height : 0
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

    Default Re: Change a TabView height following its tab child

    Thanks anda_skoa for the suggestions

    However, I noticed that it returns 0 when the tabview is loaded or when he switches the tab the first time.
    It works fine when switching tabs the second time

    To fix that I tried using Component.onCompleted, but noticed that the count of tabs is 0 when the component is created

    Here's the code to fix those problems
    Qt Code:
    1. onCountChanged:{
    2. if(count == 1){
    3. tabview.height = Qt.binding(function() {
    4. return getTab(currentIndex).childrenRect.height
    5. })
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    Is the code mentioned here the right way to do so or is there a more effective way to do so?

    Kindly advice

  8. #8
    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: Change a TabView height following its tab child

    It looks ok.

    Would need to experiement to see if this can be done nicer.

    Cheers,
    _

Similar Threads

  1. how to change the height of QHeaderView?
    By TorAn in forum Qt Programming
    Replies: 1
    Last Post: 13th March 2015, 08:43
  2. How to change line height in QMenu?
    By fsmoke in forum Qt Programming
    Replies: 1
    Last Post: 17th September 2014, 05:12
  3. QTableView How to change height for all rows?
    By sergey_85 in forum Qt Programming
    Replies: 2
    Last Post: 1st December 2009, 19:49
  4. updateEditorGeometry() and height change
    By Lykurg in forum Qt Programming
    Replies: 1
    Last Post: 1st October 2008, 20:08
  5. Qidget height resize if child widgets invisible
    By visor_ua in forum Qt Programming
    Replies: 1
    Last Post: 27th April 2008, 11:27

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.