Skip to content

Instantly share code, notes, and snippets.

@foxoman
Created February 3, 2019 16:49
Show Gist options
  • Save foxoman/bb2ce2e8e16cfd68096e8e5b367bfbf5 to your computer and use it in GitHub Desktop.
Save foxoman/bb2ce2e8e16cfd68096e8e5b367bfbf5 to your computer and use it in GitHub Desktop.
Number Randomizer with Qt/Qml
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
ApplicationWindow {
id: root
visible: true
height: Screen.height / 1.5
width: Screen.width / 3
minimumHeight: cl.implicitHeight
minimumWidth: cl.implicitWidth
title: "Randomizer!"
Material.theme: Material.Light
Material.primary: Material.color(Material.Grey, Material.Shade300)
Material.accent: "royalblue" //"#007AFF"
Material.background: Material.color(Material.Grey, Material.Shade100)
function generateRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min)
}
font.family: "SF Display"
header: ToolBar {
Material.elevation: 1
RowLayout {
anchors.fill: parent
Label {
text: "Randomizer"
font.pixelSize: 22
font.bold: true
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Layout.fillWidth: true
}
}
}
ColumnLayout {
id: cl
anchors.fill: parent
anchors.centerIn: parent
Label {
id: txt
Layout.fillHeight: true
Layout.fillWidth: true
text: slider.value.toFixed(0)
font.bold: true
font.pointSize: 500
color: Material.color(Material.Grey, Material.Shade400)
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
ColumnLayout {
Layout.fillWidth: true
spacing: 10
Rectangle {
Layout.fillWidth: true
height: 1
color: Material.color(Material.Grey, Material.Shade300)
}
Label {
text: "How Many ?"
color: "grey"
leftPadding: 40
rightPadding: 40
}
Slider {
id: slider
stepSize: 1
from: 0
value: 2
to: 10
Layout.fillWidth: true
leftPadding: 60
rightPadding: 60
snapMode: Slider.SnapAlways
ToolTip {
parent: slider.handle
visible: slider.pressed
text: slider.value.toFixed(0)
}
}
}
Button {
text: "Roll"
Layout.alignment: Qt.AlignHCenter
implicitWidth: parent.width / 2
highlighted: true
onClicked: {
txt.text = generateRandomNumber(0, slider.value.toFixed(0))
}
}
Item {
height: 20
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment