JSh*_*use 5 gtk rust gtk-rs gtk4
我如何制作可gtk4::Box点击的gtk_rs?
在 GTK3 中,似乎使用 anEventBox是实现此目的的方法,但是在 GTK4 中:
停止使用
GtkEventBox
GtkEventBox不再需要并已被删除。所有小部件都会接收所有事件。
https://docs.gtk.org/gtk4/migration-3to4.html#stop-using-gtkeventbox
所以看来点击处理程序现在应该直接附加到小部件。但是我找不到任何关于如何执行此操作的明确文档或示例。
如果有人能给我一个将点击侦听器附加到 a 的示例,gtk4::Box我将不胜感激。
您可以使用gtk::GestureClick本示例中所示的方法来执行此操作。
这是一个完整的示例(单击窗口内的某个位置,因为该框实际上不可见):
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow};
fn main() {
    // Create a new application
    let app = Application::builder()
        .application_id("org.gtk-rs.example")
        .build();
    // Connect to "activate" signal of `app`
    app.connect_activate(|app| {
        build_ui(app);
    });
    // Run the application
    app.run();
}
fn build_ui(app: &Application) {
    // Create a window and set the title
    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .build();
    // Create a box
    let gtk_box = gtk::builders::BoxBuilder::new()
        .height_request(200)
        .width_request(300)
        .build();
    // Assign a click listener
    let gesture = gtk::GestureClick::new();
    gesture.connect_released(|gesture, _, _, _| {
        gesture.set_state(gtk::EventSequenceState::Claimed);
        println!("Box pressed!");
    });
    gtk_box.add_controller(&gesture);
    // Add the box
    window.set_child(Some(>k_box));
    // Present window to the user
    window.present();
}
假设您实际上还想看到该框,并可能提供一些有关鼠标悬停的用户反馈。然后你需要使用 CSS 规则来实现这一点。
将您的更改main.rs为:
use gtk::gdk::Display;
use gtk::{CssProvider, StyleContext, prelude::*};
use gtk::{Application, ApplicationWindow};
fn main() {
    // Create a new application
    let app = Application::builder()
        .application_id("org.gtk-rs.example")
        .build();
    // Connect to "activate" signal of `app`
    app.connect_activate(|app| {
        // Load a CSS stylesheet from the included bytes.
        let provider = CssProvider::new();
        provider.load_from_data(include_bytes!("style.css"));
        // Give the CssProvider to the default screen so the CSS rules are
        // applied to the window.
        StyleContext::add_provider_for_display(
            &Display::default().expect("Error initializing gtk css provider."),
            &provider,
            gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
        );
        build_ui(app);
    });
    // Run the application
    app.run();
}
fn build_ui(app: &Application) {
    // Create a window and set the title
    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .build();
    // Create a box
    let gtk_box = gtk::builders::BoxBuilder::new()
        .height_request(200)
        .width_request(300)
        .margin_bottom(20)
        .margin_end(20)
        .margin_start(20)
        .margin_top(20)
        .css_classes(vec![String::from("hover-box")])
        .build();
    // Assign a click listener
    let gesture = gtk::GestureClick::new();
    gesture.connect_released(|gesture, _, _, _| {
        gesture.set_state(gtk::EventSequenceState::Claimed);
        println!("Box pressed!");
    });
    gtk_box.add_controller(&gesture);
    // Add the box
    window.set_child(Some(>k_box));
    // Present window to the user
    window.present();
}
style.css并在src/目录(与所在目录相同)中创建一个main.rs包含以下内容的文件:
.hover-box {
    background-color: blue;
    border-radius: 5px;
}
.hover-box:hover {
    background-color: rgb(11, 133, 240);
    border-radius: 5px;
}
现在,您将获得gtk4::Box一个5px边框半径和blue背景颜色,当您将鼠标悬停在框上方时,它们会发生变化。