C++ 入門指南 4.01
練習 31.10 參考程式 - 練習猜數字遊戲的 Qt Quick 專案
import QtQuick
import QtQuick.Controls
import io.qt.examples.guesscontroller 1.0
ApplicationWindow {
id: root
width: 200
height: 140
visible: true
title: qsTr("猜數字遊戲")
GuessController {
id: controller
}
Column {
Row {
width: 200
height: 25
TextField {
id: input
width: 200
color: "blue"
placeholderText: qsTr("輸入猜測數字")
text: controller.userInput
onEditingFinished: controller.userInput = text
}
}
Row {
width: 200
height: 20
Text {
id: display
width: 200
text: qsTr("提示訊息")
}
}
Row {
width: 200
height: 20
Text {
width: 60
text: qsTr("猜測次數:")
}
Text {
id: counter
width: 140
text: qsTr("0")
}
}
Row {
width: 200
height: 25
Button {
width: 200
text: "猜測"
onClicked: {
if (controller.userInput.length !== 4) {
display.text = qsTr("長度不符!!")
}
else if (controller.findNumber()) {
display.text = qsTr("重複數字!!")
}
else if (controller.test()) {
display.text = qsTr("恭喜猜對")
}
else {
display.text = controller.getAB()
}
counter.text = controller.getTimes()
input.text = ""
}
}
}
Row {
width: 200
height: 25
Button {
width: 200
text: qsTr("新遊戲")
onClicked: {
controller.setGuessObject();
display.text = qsTr("輸入數字開始遊戲")
}
}
}
}
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:main.qml
功能:示範猜數字遊戲的 GUI 程式
作者:張凱慶 */
回到練習 31.10 題目