Skip to content

Commit

Permalink
Fix support normalized ranges for Slider with Qt style
Browse files Browse the repository at this point in the history
The OpenGL Texture import example uses a Slider in the range 0..1. That doesn't work with
QStyleOptionSlider out of the box because it's
using integer values.
  • Loading branch information
tronical committed Jun 28, 2023
1 parent 734a5b8 commit 07f42af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project are documented in this file.
### General

- Fixed compiler panics when some complex expressions are used for the model expression in `for` (#2977)
- Native style: Fixed support for floating point ranges in Slider.

### Slint Language

Expand Down
33 changes: 18 additions & 15 deletions internal/backends/qt/qt_widgets/slider.rs
Expand Up @@ -36,12 +36,15 @@ pub struct NativeSlider {
}

cpp! {{
void initQSliderOptions(QStyleOptionSlider &option, bool pressed, bool enabled, int active_controls, int minimum, int maximum, int value) {
void initQSliderOptions(QStyleOptionSlider &option, bool pressed, bool enabled, int active_controls, float minimum, float maximum, float float_value) {
option.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle;
option.activeSubControls = { active_controls };
option.orientation = Qt::Horizontal;
option.maximum = maximum;
option.minimum = minimum;
// Slint slider supports floating point ranges, while Qt uses integer. To support (0..1) ranges
// of values, scale up a little, before truncating to integer values.
option.maximum = maximum * 1024;
option.minimum = minimum * 1024;
int value = float_value * 1024;
option.sliderPosition = value;
option.sliderValue = value;
if (enabled) {
Expand Down Expand Up @@ -72,18 +75,18 @@ impl Item for NativeSlider {
_window_adapter: &Rc<dyn WindowAdapter>,
) -> LayoutInfo {
let enabled = self.enabled();
let value = self.value() as i32;
let min = self.minimum() as i32;
let max = self.maximum() as i32;
let value = self.value() as f32;
let min = self.minimum() as f32;
let max = self.maximum() as f32;
let data = self.data();
let active_controls = data.active_controls;
let pressed = data.pressed;

let size = cpp!(unsafe [
enabled as "bool",
value as "int",
min as "int",
max as "int",
value as "float",
min as "float",
max as "float",
active_controls as "int",
pressed as "bool"
] -> qttypes::QSize as "QSize" {
Expand Down Expand Up @@ -222,9 +225,9 @@ impl Item for NativeSlider {

fn_render! { this dpr size painter widget initial_state =>
let enabled = this.enabled();
let value = this.value() as i32;
let min = this.minimum() as i32;
let max = this.maximum() as i32;
let value = this.value() as f32;
let min = this.minimum() as f32;
let max = this.maximum() as f32;
let data = this.data();
let active_controls = data.active_controls;
let pressed = data.pressed;
Expand All @@ -233,9 +236,9 @@ impl Item for NativeSlider {
painter as "QPainterPtr*",
widget as "QWidget*",
enabled as "bool",
value as "int",
min as "int",
max as "int",
value as "float",
min as "float",
max as "float",
size as "QSize",
active_controls as "int",
pressed as "bool",
Expand Down

0 comments on commit 07f42af

Please sign in to comment.