QML中的Component使用--自定义一个,然后使用它

QML中的Component使用--自定义一个,然后使用它


ParentItem.qml:

import QtQuick 2.2
import QtQuick.Dialogs 1.0
import QtQuick.Controls 1.4
import QtQuick.Window 2.2
import QtQml 2.2
import QtQml 2.2
import QtQuick 2.7

Rectangle {
     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.2
import QtQuick.Dialogs 1.0
import QtQuick.Controls 1.4
import QtQuick.Window 2.2
import QtQml 2.2
import QtQml 2.2
import QtQuick 2.7

Rectangle {
    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.2
import QtQuick.Dialogs 1.0
import QtQuick.Controls 1.4
import QtQuick.Window 2.2
import QtQml 2.2
import QtQml 2.2
import QtQuick 2.7

Rectangle {
     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.2
import QtQuick.Dialogs 1.0
import QtQuick.Controls 1.4
import QtQuick.Window 2.2
import QtQml 2.2

Rectangle{
    //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);
    }
}







  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值