Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StandardListView current_item member allows invalid index #7530

Open
JaberwockySeamonstah opened this issue Feb 3, 2025 · 0 comments
Open
Labels
a:widgets Implementation of widgets (from std-widgets.slint) and their styles (mF,bS) bug Something isn't working need triaging Issue that the owner of the area still need to triage

Comments

@JaberwockySeamonstah
Copy link

Bug Description

The code below creates a simple StandardListView with 3 elements and a button to remove the currently selected element from that StandardListView. The element will be removed via the attached Rust code.

To reproduce the issue, just select the last element (Green) and click the button. The element will disappear as expected. Since no element is selected anymore, I assumed the value is -1, instead it is still 2. So clicking the button again, will make the application panic.

I understand, that I can reset the value manual to -1 or any other logical value, but I was surprised to find, that this simple setup would allow me to panic.

Reproducible Code (if applicable)

import { Button, StandardListView } from "std-widgets.slint";
export component MainWindow inherits Window {
    in-out property <[StandardListViewItem]> items: [{ text: "Blue" }, { text: "Red" }, { text: "Green" }];
    callback remove_item(int);

    VerticalLayout {
        list_view := StandardListView {
            width: 320px;
            height: 256px;
            model: root.items;
        }
        Button {
            text: "Click";
            clicked => {
                root.remove_item(list_view.current-item);
            }
        }        
    }
}

// Rust Code
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use slint::Model;
slint::include_modules!();

fn main() -> Result<(), slint::PlatformError> {
    let main_window      = MainWindow::new()?;

    let items:Vec<slint::StandardListViewItem> = main_window.get_items().iter().collect();
    let items                                  = std::rc::Rc::new(slint::VecModel::from(items));
    let main_window_weak                       = main_window.as_weak();

    main_window.on_remove_item(move |idx: i32| {
        if idx >= 0 {
            items.remove(idx as usize);
            main_window_weak.unwrap().set_items(items.clone().into());
        }
    });
    main_window.run()
}

Environment Details

  • Slint Version: 1.9.2
  • Platform/OS: Linux, KDE, Wayland
  • Programming Language: Rust
  • Backend/Renderer: No idea, probably the default

Product Impact

I'm building an open-source application with slint, it is my first slint project. This issue is a minor one, because there are simple workarounds.

@JaberwockySeamonstah JaberwockySeamonstah added bug Something isn't working need triaging Issue that the owner of the area still need to triage labels Feb 3, 2025
@ogoffart ogoffart added the a:widgets Implementation of widgets (from std-widgets.slint) and their styles (mF,bS) label Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:widgets Implementation of widgets (from std-widgets.slint) and their styles (mF,bS) bug Something isn't working need triaging Issue that the owner of the area still need to triage
Projects
None yet
Development

No branches or pull requests

2 participants