• Roson的Qt之旅#103 QML之标签导航控件TabBar


    TabBar提供了一个基于标签的导航模型。


    TabBar由TabButton控件填充,可以与任何提供currentIndex属性的布局或容器控件一起使用,例如StackLayout或SwipeView。

    1.   TabBar {
    2.       id: bar
    3.       width: parent.width
    4.       TabButton {
    5.           text: qsTr("Home")
    6.       }
    7.       TabButton {
    8.           text: qsTr("Discover")
    9.       }
    10.       TabButton {
    11.           text: qsTr("Activity")
    12.       }
    13.   }
    14.   StackLayout {
    15.       width: parent.width
    16.       currentIndex: bar.currentIndex
    17.       Item {
    18.           id: homeTab
    19.       }
    20.       Item {
    21.           id: discoverTab
    22.       }
    23.       Item {
    24.           id: activityTab
    25.       }
    26.   }

    如上所示,TabBar通常由一组静态的标签按钮填充,这些按钮被内联定义为标签栏的子项。它也可以在运行时动态地添加、插入、移动和删除项目。这些项目可以使用itemAt()或contentChildren来访问。

    调整标签的大小

    默认情况下,TabBar会调整其按钮的大小以适应控件的宽度。可用的空间会平均分配给每个按钮。默认的大小调整行为可以通过为按钮设置一个显式的宽度来重写。
    下面的例子说明了如何保持每个标签按钮的隐含大小,而不是被调整大小以适应标签栏。  

    1.   TabBar {
    2.       width: parent.width
    3.       TabButton {
    4.           text: "First"
    5.           width: implicitWidth
    6.       }
    7.       TabButton {
    8.           text: "Second"
    9.           width: implicitWidth
    10.       }
    11.       TabButton {
    12.           text: "Third"
    13.           width: implicitWidth
    14.       }
    15.   }


      
      
      
    可滑动的标签

    如果按钮的总宽度超过了标签栏的可用宽度,它就会自动变成可滑动的。
     

    1.   TabBar {
    2.       id: bar
    3.       width: parent.width
    4.  
    5.       Repeater {
    6.           model: ["First", "Second", "Third", "Fourth", "Fifth"]
    7.           TabButton {
    8.               text: modelData
    9.               width: Math.max(100, bar.width / 5)
    10.           }
    11.       }
    12.   }

    参见TabButton, Customizing TabBar, Navigation Controls, and Container Controls.


    属性说明

    real contentHeight

    此属性持有内容高度。它用于计算标签栏的总隐含高度。
    除非明确重写,否则内容高度会根据标签的最大隐含高度自动计算。
    这个QML属性在QtQuick.Controls 2.2(Qt 5.9)中引入。
    另见contentWidth。

    real contentWidth

    此属性持有内容宽度。它用于计算标签栏的总隐含宽度。
    除非明确重写,否则内容宽度会根据标签的总隐含宽度和标签栏的间距自动计算。
    这个QML属性在QtQuick.Controls 2.2(Qt 5.9)中引入。
    另见contentHeight。

    enumeration position

    此属性持有标签栏的位置。
    注意:如果标签栏被指定为ApplicationWindow或Page的页眉或页脚,适当的位置会自动设置。
    可能的值:

    • TabBar.Header    标签栏在顶部,作为窗口或页面的标题。
    • TabBar.Footer    标签栏在底部,作为窗口或页面的页脚。

    默认值是style-specific。


    参见ApplicationWindow::header, ApplicationWindow::footer, Page::header, and Page::footer.
      
      
      
      


      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      

  • 相关阅读:
    【lwip】08-ARP协议一图笔记及源码实现
    windows安装pytorch
    旋转接头安装使用注意事项
    如何通过一个项目征服Java
    计算机网络-第4章 网络层
    JVM知识点
    Azure 机器学习 - 使用 ONNX 对来自 AutoML 的计算机视觉模型进行预测
    vscode 连接 GitHub
    Ubuntu 22.04 无法使用网易云音乐
    动态规划——474. 一和零
  • 原文地址:https://blog.csdn.net/jolin678/article/details/126086400