QML中的Component使用--自定义一个,然后使用它
来源:互联网 发布:centos ftp 根目录 编辑:程序博客网 时间:2024/06/05 13:32
QML中的Component使用--自定义一个,然后使用它
ParentItem.qml:
import QtQuick 2.2import QtQuick.Dialogs 1.0import QtQuick.Controls 1.4import QtQuick.Window 2.2import QtQml 2.2import QtQml 2.2import QtQuick 2.7Rectangle { id:id_parentItem; property alias shortext: id_text.text; property string longtext: id_text.text;//自定义的属性 signal sig_click(string str);//自定义的信号 //signal clicked width: 116; height: 26; color: "lightsteelblue"; border.color: "red"; Text{ id:id_text; anchors.centerIn: parent; text:"Start"; } MouseArea{ anchors.fill: parent; onClicked: { //console.log("id_parentItem-onClicked"); id_parentItem.sig_click("from "+id_text.text); mouse.accepted=true; id_parentItem.focus=true } } function func_parent(a,b){ return a+b; } Keys.onReturnPressed: { //console.log("Keys.onReturnPressed"); var vret = func_parent("onReturnPressed:",id_text.text); id_parentItem.sig_click(vret); event.accepted=true; } Keys.onSpacePressed: { id_parentItem.sig_click("onSpacePressed:"+id_text.text); event.accepted=true; } Button{ id:id_btn1; } Timer{ id:id_FocusScope1; } Component.onCompleted: { console.log(id_btn1); console.log(id_FocusScope1); } }useItem.qml:
import QtQuick 2.2import QtQuick.Dialogs 1.0import QtQuick.Controls 1.4import QtQuick.Window 2.2import QtQml 2.2import QtQml 2.2import QtQuick 2.7Rectangle { id:id_rec visible: true; width: 400; height: 400; ParentItem{ id:id_pItem1; anchors.left: parent.left; anchors.leftMargin: 3; shortext:"id_pItem1"; onSig_click: { console.log("id_pItem1 接收到信号来自:"+"sig_click "+"信号参数为:"+str); } } ParentItem{ id:id_pItem2; anchors.left: parent.left; anchors.leftMargin: 130; shortext:"id_pItem2"; onSig_click: { console.log("id_pItem2 接收到信号来自:"+"sig_click "+"信号参数为:"+str); } } ParentItem{ id:id_pItem3; anchors.left: parent.left; anchors.leftMargin: 260; shortext:"id_pItem3"; onSig_click: { console.log("id_pItem3 接收到信号来自:"+"sig_click "+"信号参数为:"+str); } }// Item {// width: 100; height: 100// Component {// id: redSquare// Rectangle {// color: "red"// width: 10// height: 10// }// }// Loader { sourceComponent: redSquare }// Loader { sourceComponent: redSquare; x: 20 }// } Component.onCompleted: {// console.log(cp3);// console.log(redSquare1); } }Qt5.5.1 版本
下面是另外一个例子:
ColorPicker.qml:
import QtQuick 2.2import QtQuick.Dialogs 1.0import QtQuick.Controls 1.4import QtQuick.Window 2.2import QtQml 2.2import QtQml 2.2import QtQuick 2.7Rectangle { id:colorPicker; width: 50; height: 30; signal colorPicked(color clr);//自定义的信号 //signal clicked function configureBorder(){ colorPicker.border.width=colorPicker.focus?2:0; colorPicker.border.color=colorPicker.focus?"purple":"gray"; } MouseArea{ anchors.fill: parent; onClicked: { colorPicker.colorPicked(colorPicker.color); mouse.accepted=true; colorPicker.focus=true; } } Keys.onReturnPressed: { colorPicker.colorPicked(colorPicker.color); mouse.accepted=true; } Keys.onSpacePressed: { colorPicker.colorPicked(colorPicker.color); mouse.accepted=true; } onFocusChanged: { configureBorder(); } Component.onCompleted: { configureBorder(); } }
test.qml:
import QtQuick 2.2import QtQuick.Dialogs 1.0import QtQuick.Controls 1.4import QtQuick.Window 2.2import QtQml 2.2Rectangle{ //id:root; visible: true; width: 320; height: 240; Text{ id:coloredText; anchors.horizontalCenter: parent.horizontalCenter; anchors.top: parent.top; anchors.topMargin: 4; text:"Hello World!"; font.pixelSize: 32; } function setTextColor(clr){ coloredText.color=clr; } ColorPicker{ id:redColor; color: "red"; focus: true; anchors.left: parent.left; anchors.leftMargin: 4; anchors.bottom: parent.bottom; anchors.bottomMargin: 4; KeyNavigation.right: blueColor; KeyNavigation.tab: blueColor; onColorPicked: { coloredText.color=clr; } } ColorPicker{ id:blueColor; color: "blue"; focus: true; anchors.left: redColor.right; anchors.leftMargin: 4; anchors.bottom: parent.bottom; anchors.bottomMargin: 4; KeyNavigation.left: redColor; KeyNavigation.right: pinkColor; KeyNavigation.tab: pinkColor; } ColorPicker{ id:pinkColor; color: "pink"; focus: true; anchors.left:blueColor.right; anchors.leftMargin: 4; anchors.bottom: parent.bottom; anchors.bottomMargin: 4; KeyNavigation.left: blueColor; KeyNavigation.tab: redColor; } Component.onCompleted: { blueColor.colorPicked.connect(setTextColor); pinkColor.colorPicked.connect(setTextColor); }}
阅读全文
0 0
- QML中的Component使用--自定义一个,然后使用它
- 使用XIB自定义一个UIView,然后将…
- Qt第三方库QML-Box2D——使用它创建一个飞翔的蝙蝠
- 如何在QML应用中设计一个C++ Model并使用它
- QML中组件Component和装载Loader的使用
- QML之使用C++自定义QML类型(一)
- QML之使用C++自定义QML类型(三)
- 编写一个程序,它读取一个文件以判断其中的字节数,然后使用0覆盖所有的字节
- 在QML中使用自定义属性类型
- 怎么使用(c#)asp.net打开一个xml文件然后对它进行修改保存。后台工作
- QML之QML之使用C++自定义QML类型(二)
- QML: component
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view
- 如何使用QML动态产生Component来完成我们的气球游戏 (1)
- 如何使用QML动态产生Component来完成我们的气球游戏 (2)
- [WPF]如何自定义一个User Control 并在Page.xaml里使用它
- 给出一个现实中的例子,然后用pv操作解决它。
- 昨天下午到现在,将近一天的时间卡在了cmakelists.txt关键的一句话
- Gradle个性化配置
- nyoj 814 又见拦截导弹
- shiro+redis实现session共享<3>
- java 中对类中的属性使用set/get方法的意义和用法
- QML中的Component使用--自定义一个,然后使用它
- Android Bluetooth framework启动流程
- 《leetcode》single-number
- Joda-Time使用参考
- spring cloud -- Ribbon
- CentOS 7 安装 Nginx 1.11.x
- 获取不同长度的UUID
- STL中vector的使用
- spring boot使用内嵌的tomcat解决不安全的HTTP方法安全漏洞