ورود

View Full Version : مشکل با بلاک If در QML



returnx
دوشنبه 21 اسفند 1391, 18:50 عصر
من دارم برای نمونه یک دکمه با QML می سازم و می خوام در صورتی که خاصیت Use_gradient برابر true بود به دکمه Gradient بدم در غیر اینصورت از همان Color معمولی استفاده کنم، با کلی کلنجار رفتن ، تونستم با کد زیر جواب بگیرم ، اما باید راه بهتری باشه ، زیاد هم جستجو کردم اما به نتیجه ی خاصی نرسیدم :

Rectangle {

property variant text
property bool use_gradient: true
property color backcolor: "lightgray"
property color gradient_start_color: "lightgray"
property color gradient_end_color: "white"

property variant temp_gradient: Gradient{
GradientStop {position: 0.0; color:gradient_start_color}
GradientStop {position: 1.0; color: gradient_end_color}
}

id:container
width: button_txt.width + 20
height: button_txt.height + 10
text:qsTr("Button")
border.width: 1
radius: 4
smooth: true
color: if (use_gradient == false){backcolor}
gradient: if (use_gradient == true ) {temp_gradient}


//Set Button Caption:
Text {
id: button_txt
text: qsTr(parent.text)
anchors.centerIn: parent
}راه بهتری هم هست !؟البته اگر از عملگر ؟ استفاده نشه بهتره...

alamate_aoal
شنبه 26 اسفند 1391, 21:36 عصر
چرا نباید از عملگر :? استفاده بشه؟

QMLButton.qml

import QtQuick 1.0

Rectangle {
id: container

signal clicked

property bool useGradient : true
property alias text: buttonLabel.text

radius: 8; smooth: true; border.width: 1;
width: buttonLabel.width + 20; height: buttonLabel.height + 5

gradient: Gradient {
GradientStop {
position: 0.0
color: { if (useGradient) return "lightgray"; else return "white"; }
}
GradientStop { position: 1.0; color: "white" }
}

MouseArea {
id: mouseArea
anchors.fill: parent
onClicked: container.clicked();
}

Text {
id: buttonLabel
anchors.centerIn: parent
text: "QMLButton"
}
}


main.qml

import QtQuick 1.1

Rectangle {
id: mainWin
width: 360; height: 360

QMLButton {
id: qmlButton1
width: 90; height: 36
text: "Click me!"

anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter

onClicked: { qmlButton1.useGradient = !qmlButton1.useGradient }
}
}

returnx
یک شنبه 27 اسفند 1391, 14:40 عصر
چرا نباید از عملگر :? استفاده بشه؟
خیلی دلیل خاصی نداره ، فقط به این دلیل که خوانایی کد را میاره پایین...