Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] How to add a Close Button in TabView Tabs
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to add a Close Button in TabView Tabs

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 5.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    forestdweller
    wrote on 11 Feb 2014, 06:44 last edited by
    #1

    It seems that the TabBar delegate (a MouseArea) hides any mouse area defined in the tabitem.
    Has anyone successfully added a close button to tab defined in a derived TabViewStyle?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jens
      wrote on 11 Feb 2014, 16:12 last edited by
      #2

      It is possible to do this with a custom style. The drawback is that you will have to provide custom styling for the button itself:

      @ TabView {
      id: tabView
      anchors.fill: parent
      anchors.margins: 10
      Tab { title: "first" }
      Tab { title: "second" }
      Tab { title: "third" }
      style: TabViewStyle {
      property color frameColor: "#999"
      property color fillColor: "#eee"
      frameOverlap: 1
      frame: Rectangle {
      color: "#eee"
      border.color: frameColor
      }
      tab: Rectangle {
      color: styleData.selected ? fillColor : frameColor
      implicitWidth: Math.max(text.width + 24, 80)
      implicitHeight: 20
      Rectangle { height: 1 ; width: parent.width ; color: frameColor}
      Rectangle { height: parent.height ; width: 1; color: frameColor}
      Rectangle { x: parent.width -1; height: parent.height ; width: 1; color: frameColor}
      Text {
      id: text
      anchors.left: parent.left
      anchors.verticalCenter: parent.verticalCenter
      anchors.leftMargin: 6
      text: styleData.title
      color: styleData.selected ? "black" : "white"
      }
      Button {
      anchors.right: parent.right
      anchors.verticalCenter: parent.verticalCenter
      anchors.rightMargin: 4
      height: 16
      style: ButtonStyle {
      background: Rectangle {
      implicitWidth: 16
      implicitHeight: 16
      radius: width/2
      color: control.hovered ? "#eee": "#ccc"
      border.color: "gray"
      Text {text: "X" ; anchors.centerIn: parent ; color: "gray"}
      }}
      onClicked: tabView.removeTab(styleData.index)
      }
      }
      }
      }@

      1 Reply Last reply
      0
      • F Offline
        F Offline
        forestdweller
        wrote on 12 Feb 2014, 00:46 last edited by
        #3

        Thanks Jens,
        I actually was doing something similar using an Image and MouseArea instead of a Button. Switching to the Button works fine. I guess I still don't quite get the Controls code yet.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jens
          wrote on 12 Feb 2014, 07:31 last edited by
          #4

          There is nothing really special about the button so I am curious what the problem was. This is how the code might look with a regular mouse area:
          @
          import QtQuick 2.0
          import QtQuick.Controls 1.1
          import QtQuick.Controls.Styles 1.1

          Item {
          width: 400
          height: 300
          TabView {
          id: tabView
          anchors.fill: parent
          anchors.margins: 10
          Tab { title: "first" }
          Tab { title: "second" }
          Tab { title: "third" }
          style: TabViewStyle {
          property color frameColor: "#999"
          property color fillColor: "#eee"
          frameOverlap: 1
          frame: Rectangle {
          color: "#eee"
          border.color: frameColor
          }
          tab: Rectangle {
          color: styleData.selected ? fillColor : frameColor
          implicitWidth: Math.max(text.width + 24, 80)
          implicitHeight: 20
          Rectangle { height: 1 ; width: parent.width ; color: frameColor}
          Rectangle { height: parent.height ; width: 1; color: frameColor}
          Rectangle { x: parent.width -1; height: parent.height ; width: 1; color: frameColor}
          Text {
          id: text
          anchors.left: parent.left
          anchors.verticalCenter: parent.verticalCenter
          anchors.leftMargin: 6
          text: styleData.title
          color: styleData.selected ? "black" : "white"
          }
          Rectangle {
          anchors.right: parent.right
          anchors.verticalCenter: parent.verticalCenter
          anchors.rightMargin: 4
          implicitWidth: 16
          implicitHeight: 16
          radius: width/2
          color: control.hovered ? "#eee": "#ccc"
          border.color: "gray"
          Text {text: "X" ; anchors.centerIn: parent ; color: "gray"}
          MouseArea {
          anchors.fill: parent
          onClicked: tabView.removeTab(styleData.index)
          }
          }
          }
          }
          }
          }
          @

          1 Reply Last reply
          0

          1/4

          11 Feb 2014, 06:44

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved