Qt Quick 字体颜色改变

本例子为Qt Quick 核心编程中的一个小例子,点击对应颜色方块,字体改变对应颜色:

ColorPicker.qml

import QtQuick 2.0

Rectangle {
    id:colorPicker;
    width: 50;
    height: 30;
    signal colorPicked(color clc);

    function configureBorder()
    {
        colorPicker.border.width = colorPicker.focus?2:0;
        colorPicker.border.color = colorPicker.focus?"#90D750":"#808080";
    }

    MouseArea{
        anchors.fill: parent
        onClicked: {
            colorPicker.colorPicked(colorPicker.color);
            mouse.accepted = true;
            colorPicker.focus = true;
        }

    }

    Keys.onReturnPressed: {
        colorPicker.colorPicked(colorPicker.color);
        event.accepted = true;
    }

    Keys.onSpacePressed: {
        colorPicker.colorPicked(colorPicker.color);
        event.accepted = true;
    }

    onFocusChanged: {
        configureBorder();
    }

    Component.onCompleted: {
        configureBorder();
    }

}

 component_file.qml

import QtQuick 2.0
import QtQuick.Window 2.12

Window{
    visible: true
    width: 320
    height: 240
    color: "#EEEEEE";

    Text {
        id: coloredText;
        anchors.horizontalCenter: parent.horizontalCenter;
        anchors.top: parent.top;
        anchors.topMargin: 10;
        text: qsTr("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 = "red";
        }
    }

    ColorPicker{
        id:blueColor;
        color: "blue";
        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";
        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);
    }




}

效果也非常的简单,希望大家能够喜欢下去,可以让我们学习到更多的知识。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值