QML TabView动态添加tab和赋初值

通过调用TabView的addTab  动态添加新的选项卡:Tab addTab(string titleComponent component),其中title为选项卡标题,component为选项卡内的组件(var component = Qt.createComponent("souces.qml")。

动态添加完成后,返回一个Tab,通过引用Tab的item可以访问component内的方法和属性

动态添加Tab和给Tab内的component赋初值源码:

               var component = Qt.createComponent("souces.qml")
                if(component.status===Component.Ready){
                    var tab=tabView.addTab("sdfasdf",component)
                    tabView.currentIndex=(tabView.count-1)//当前选中项
                    tab.item.tableModel.clear()
                    tab.item.tableModel.append({
                                                 a: 122222,
                                                 b: 2,
                                                 c:"Bit",
                                                 d: 4,
                                                 remark: "备注"
                                             })
                }    

TabView源码:

 1     TabView  {
 2         id:tabView
 3         anchors.top: functionButton.bottom
 4         anchors.bottom: parent.bottom
 5         anchors.right: parent.right
 6         anchors.left: parent.left
 7         style: TabViewStyle {
 8             frameOverlap: 1
 9             tab: Rectangle {
10                 color: styleData.selected ? "#dcdada" :"white"
11                 border.color:  "steelblue"
12                 implicitWidth: Math.max(text1.width + 4, 80)
13                 implicitHeight: 30
14                 radius: 2
15                 Text {
16                     id: text1
17                     anchors.centerIn: parent
18                     text: styleData.title
19                     color: styleData.selected ? "white" : "black"
20                 }
21             }
22             frame: Rectangle { color: "white" }
23             rightCorner:Rectangle{
24                 anchors.right: parent.right
25                 Text {
26                     id: text2
27                     text: "+++"
28     //                    color: styleData.selected ? "white" : "orange"
29                 }
30             }
31         }
32     }

souces.qml源码:

  1     property alias tableModel:configModel//属性别名
  2     ListModel{
  3           id: configModel
  4           ListElement{
  5               a: 1;
  6               b: 2;
  7               c:"Bit"
  8               d: 4
  9               remark: "备注"
 10           }
 11     }
 12     TableView{
 13       id: configTable
 14       alternatingRowColors:true//交替行颜色
 15       anchors.bottom: table.bottom
 16       anchors.top: functionButton.bottom
 17       anchors.left: table.left
 18       anchors.right: table.right
 19       model: configModel
 20 //      width:table.width
 21       TableViewColumn{
 22           id:byteOffset
 23           role:"byteOffset"
 24           title:"字偏移"
 25           width: columnWidth
 26       }
 27       TableViewColumn{
 28           id:bitOffset
 29           role:"bitOffset"
 30           title:"位偏移"
 31           width: columnWidth
 32       }
 33       TableViewColumn{
 34           id:fieldLenType
 35           role:"fieldLenType"
 36           title:"字段长度类型"
 37           width: columnWidth
 38       }
 39       TableViewColumn{
 40           id:fieldLen
 41           role:"fieldLen"
 42           title:"字段长度"
 43           width: columnWidth
 44       }
 45       TableViewColumn{
 46           id:fieldType
 47           role:"fieldType"
 48           title:"字段类型"
 49           width: columnWidth
 50       }
 51       TableViewColumn{
 52           id:fieldShowType
 53           role:"fieldShowType"
 54           title:"字段显示类型"
 55           width: columnWidth
 56       }
 57       TableViewColumn{
 58           id:fieldName
 59           role:"fieldName"
 60           title:"字段名称"
 61           width: columnWidth
 62       }
 63       TableViewColumn{
 64           id:fieldValue
 65           role:"fieldValue"
 66           title:"字段值"
 67           width: columnWidth
 68       }
 69
 70       TableViewColumn{
 71           id:remark
 72           role:"remark"
 73           title:"备注"
 74           width: columnWidth
 75           elideMode: Text.ElideRight
 76       }
 77
 78
 79       headerDelegate :Rectangle{//设置表头的样式
 80 //          implicitWidth: 10
 81 //          implicitHeight: 30
 82           height: 30
 83           border.color: "#b9b8b8"
 84           Text{
 85               anchors.centerIn : parent//居中
 86               anchors.verticalCenter: parent.verticalCenter
 87               text: styleData.value
 88 //              color:
 89               font.bold: true
 90           }
 91           MouseArea {//自定义左击/右击功能  左击被屏蔽
 92               anchors.fill:parent
 93               acceptedButtons: Qt.RightButton|Qt.LeftButton
 94               onClicked: {
 95                   tableHeader_menu.popup()
 96               }
 97           }
163
164       }
165       //行代理可以修改行高等信息
166       rowDelegate: Rectangle {
167             height: 20
168             MouseArea {//自定义左击/右击功能
169                 anchors.fill:parent
170                 acceptedButtons: Qt.RightButton|Qt.LeftButton
171                 onClicked: {
172                     if(mouse.button === Qt.RightButton){
173 //                        console.log(styleData.row )
174                         if(styleData.row!==undefined){
175                             configTable.selection.clear()//取消所有选择
176                             currrrRow=styleData.row
177                             configTable.selection.select(styleData.row)//选择行
178                             insertRowUp.visible=true
179                             deletRow.visible=true
180                             content_menu.popup()
181                         }else{
182                             currrrRow=configTable.rowCount-1
183                             configTable.selection.clear()//取消所有选择
184                             insertRowUp.visible=false
185                             deletRow.visible=false
186                             content_menu.popup()
187                         }
188
189                     }else if(mouse.button === Qt.LeftButton){
190                         if(styleData.row!==undefined){
191                             currrrRow=styleData.row
192                             configTable.selection.clear()//取消所有选择
193                             configTable.selection.select(styleData.row)//选择行
194                         }else{
195                             currrrRow=-1
196                             configTable.selection.clear()//取消所有选择
197                         }
198                      }
199                 }
200                 onDoubleClicked: {
201                     if(styleData.row!==undefined){
202                         dataFormatEidt.visible=true
203                         dataFormatEidt.linedata=configModel.get(currrrRow)
204                     }
205                 }
206             }
207         }
208     }

原文地址:https://www.cnblogs.com/jingsichen/p/11583800.html

时间: 2024-08-03 09:22:02

QML TabView动态添加tab和赋初值的相关文章

easyUI树形节点点击和动态添加Tab-

easyUI是一种基于jQuery的UI框架,可以直接阅读官网的API教程学习,最近做的项目要用到这个框架,就一边看一边做,先看一下动态添加Tab,实际项目中我们都是希望点击一个节点,增加一个iframe的窗口出来,但是昨天看着API做一个简单的点击A标签增加iframe,死活就是不行,晚上回去终于搞定,看一下官方的例 [html] view plain copy print? function addTab(title, url){ if ($('#tt').tabs('exists', ti

EasyUI创建异步树形菜单和动态添加tab页面

创建异步树形菜单 添加树形菜单的ul标签 <ul class="easyui-tree" id="treeMenu"> </ul> 写js代码,对菜单的ul标签元素使用tree函数 $('#treeMenu').tree({ url:'tree_data.json' //url的值是异步获取数据的页面地址 }); 写用来异步获取数据的页面(tree_data.json页面).返回的需是Json值(此处用数组代替从数据库获取的数据,以省略连接数

Android中动态添加tab

来源过于啰嗦,这里只有简化后的. 转载请注明出处  http://www.cnblogs.com/zaiyuzhong/p/add-tab-dynamic-in-android.html 建立对应的布局配置:/res/layout/activity_main.xml <?xml version="1.0" encoding="utf-8"?> <TabHost android:layout_width="fill_parent"

java-easyui动态添加tab

layout效果 代码: <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd&

拷贝构造,深度拷贝,关于delete和default相关的操作,explicit,类赋初值,构造函数和析构函数,成员函数和内联函数,关于内存存储,默认参数,静态函数和普通函数,const函数,友元

 1.拷贝构造 //拷贝构造的规则,有两种方式实现初始化. //1.一个是通过在后面:a(x),b(y)的方式实现初始化. //2.第二种初始化的方式是直接在构造方法里面实现初始化. 案例如下: #include<iostream> //如果声明已经定义,边不会生成 class classA { private: int a; int b; public: //拷贝构造的规则,有两种方式实现初始化 //1.一个是通过在后面:a(x),b(y)的方式实现初始化 //2.第二种初始化的方式是直

C# DataGridView控件动态添加新行

C# DataGridView控件动态添加新行 DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态添加新行的两种方法: 方法一: int index=this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[

【转】console.time 简单分析javascript动态添加Dom节点的性能

本文代码约定 1 el: 指的是增加直接点的DOM节点 2 totalNum: 为100000(值越大越能体现差距)指的是循环创建的DOM节点 3 for(var i=0;i<totalNum;i++){}: 我们用for来表示就好了,简写代码 如果叫你用javascript动态增加DOM节点,你有哪几种思路呢? 1 .使用innerHTML和字符串拼接 console.time("time1"); var str = ""; for{ str += &quo

console.time 简单分析javascript动态添加Dom节点的性能

Bullshit 本来想每天都更新下博客的,但是最近要考试,还有就是自己还是停留在暗自窃喜中吧(这种想法要改变).其实最近总在想,自己要怎么去管理自己的数据,每天的生活都是对自己的数据的增删查改.昨天把自己的电脑重装了,确实很多软件的存放要改地方了,之前不知道怎么去管理软件安装,所以放得乱七八糟的.说好一大堆废话之后,我最后再说一遍,管好自己的时间.数据真的比你学习东西重要. Method 本文代码约定 1 el: 指的是增加直接点的DOM节点 2 totalNum: 为100000(值越大越能

javascript动态添加html节点

之前一直没怎么接触需要动态添加和删除html节点的项目,这次项目中用到了,也学习了. 在一个<table id="tab">标签中添加一个<tr id="tr">标签,<tr>标签里面包含4个<td>标签 function insertTr() { tr = document.createElement("tr"); html = "<td></td>"+