Russian Qt Forum
Март 28, 2024, 12:27 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: [1]   Вниз
  Печать  
Автор Тема: Обработка сигнала с клавиатуры [QML]  (Прочитано 3738 раз)
razorqhex
Гость
« : Сентябрь 26, 2018, 14:37 »

Всем привет. Очень нуждаюсь в вашей помощи

Есть обычный целочисленный SpinBox на QML. И нужно сделать так, чтобы в переменную property real spinBoxValue было присвоено значение этого SpinBox с помощью верхних цифровых кнопок.

Вот код верхних цифр 0, 1, 2, 3
Код
Javascript
Keys.onReleased:
{
if(event.key === Qt.Key_0) { //virtual button 0
spinBoxValue = spBox_MaxRate.value * 15
console.log(":: Current value: " + spinBoxValue)
 
event.accepted = true;
}
 
if(event.key === Qt.Key_1) { //virtual button1
spinBoxValue = spBox_MaxRate.value * 15
console.log(":: Current value: " + spinBoxValue)
 
event.accepted = true;
}
 
if(event.key === Qt.Key_2) { //virtual button2
spinBoxValue = spBox_MaxRate.value * 15
console.log(":: Current value: " + spinBoxValue)
 
event.accepted = true;
}
 
if(event.key === Qt.Key_3) { //virtual button3
spinBoxValue = spBox_MaxRate.value * 15
console.log(":: Current value: " + spinBoxValue)
 
event.accepted = true;
}
}

Всё бы хорошо, только в переменную записывается текущее число, которое было в SpinBox, когда я начинаю менять его цифрами, то всё равно результат тот же. Но(!!!) когда я не нажимаю кнопку Enter, то число которое я написал клавиатурой присваивается в переменную.

Как сделать так, чтобы эти числа, которые я ввожу через клавиатуру сразу присваивались в переменную без нажатия на кнопку Enter?

Я буду очень благодарен за ответ

Вот код всего SpinBox
   
Код
Javascript
SpinBox {
id: spBox_MaxRate
 
from: -800
to: 800
value: 800
 
editable: true
wheelEnabled: true
wrap: true
 
Keys.onReleased: {
if(event.key === Qt.Key_0) { //virtual button 0
spinBoxValue = spBox_MaxRate.value * 15
console.log(":: Current value: " + spinBoxValue)
 
event.accepted = true;
}
 
if(event.key === Qt.Key_1) { //virtual button1
spinBoxValue = spBox_MaxRate.value * 15
console.log(":: Current value: " + spinBoxValue)
 
event.accepted = true;
}
 
if(event.key === Qt.Key_2) { //virtual button2
spinBoxValue = spBox_MaxRate.value * 15
console.log(":: Current value: " + spinBoxValue)
 
event.accepted = true;
}
 
if(event.key === Qt.Key_3) { //virtual button3
spinBoxValue = spBox_MaxRate.value * 15
console.log(":: Current value: " + spinBoxValue)
 
event.accepted = true;
}
}
Записан
razorqhex
Гость
« Ответ #1 : Октябрь 01, 2018, 09:50 »

Решил проблему:

Код
Javascript
SpinBox {
id: spBox_MaxRate
 
anchors.centerIn: parent
 
from: -800
to: 800
value: 800
 
editable: true
wheelEnabled: true
wrap: true
 
Material.accent: "#2196F3"
Material.theme: Material.Dark
Material.foreground: "white"
 
//Signal for changing values
onValueChanged: {
spinBoxValue = spBox_MaxRate.value * 15
}
 
focus: true
 
//Signals for button presses
Keys.onReleased: {
if(event.key === Qt.Key_0) { //button 0
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
event.accepted = true;
}
 
if(event.key === Qt.Key_1) { //button 1
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
event.accepted = true;
}
 
if(event.key === Qt.Key_2) { //button 2
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
event.accepted = true;
}
 
if(event.key === Qt.Key_3) { //button 3
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
event.accepted = true;
}
 
if(event.key === Qt.Key_4) { //button 4
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
event.accepted = true;
}
 
if(event.key === Qt.Key_5) { //button 5
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
event.accepted = true;
}
 
if(event.key === Qt.Key_6) { //button 6
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
event.accepted = true;
}
 
if(event.key === Qt.Key_7) { //button 7
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
event.accepted = true;
}
 
if(event.key === Qt.Key_8) { //button 8
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
event.accepted = true;
}
 
if(event.key === Qt.Key_9) { //button 9
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
event.accepted = true;
}
 
if(event.key === Qt.Key_Backspace) {
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
 
event.accepted = true;
}
 
if(event.key === Qt.Key_Delete) {
spBox_MaxRate.focus = false
spBox_MaxRate.focus = true
event.accepted = true;
}
}
//===================
 
//Allocating values with a mouse
contentItem: TextInput {
inputMethodHints: Qt.ImhFormattedNumbersOnly
selectByMouse: true
text: parent.textFromValue(parent.value, parent.locale)
font.pointSize: 13
horizontalAlignment: Text.AlignHCenter
color: "white"
selectedTextColor: "black"
selectionColor: "white"
}
 
onFocusChanged: {
console.log("Focus changed")
 
//Output the value when the widget focus is true
if(spBox_MaxRate.focus === true) {
spinBoxValue = spBox_MaxRate.value * 15
console.log("Value: " + spinBoxValue)
}
}
}

spinBoxValue это переменная property real spinBoxValue
Записан
Страниц: [1]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.146 секунд. Запросов: 20.