首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在GTK4中制作一个可点击的盒子

在GTK4中制作一个可点击的盒子
EN

Stack Overflow用户
提问于 2021-11-08 23:58:31
回答 1查看 509关注 0票数 2

如何使gtk4::Boxgtk_rs中可点击

在GTK3中,使用EventBox似乎是实现这一目标的方法,但是在GTK4中:

停止使用GtkEventBox GtkEventBox已不再需要,并已被删除。 所有小部件都接收所有事件。 https://docs.gtk.org/gtk4/migrating-3to4.html#stop-using-gtkeventbox

因此,单击处理程序现在应该直接附加到小部件上。但是,我找不到关于如何做到这一点的任何明确的文档或示例。

如果有人能给我一个将点击侦听器附加到gtk4::Box的例子,我们将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-11 10:55:47

您可以使用gtk::GestureClick (如本示例中所示)来完成此操作。

下面是一个完整的示例(单击窗口内的某个位置,因为该框实际上是不可见的):

代码语言:javascript
复制
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(&gtk_box));

    // Present window to the user
    window.present();
}

一个更复杂的例子

假设你也想看到这个盒子,或者提供一些用户对鼠标悬停的反馈。然后,您将需要使用CSS规则来实现这一点。

main.rs更改为:

代码语言:javascript
复制
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(&gtk_box));

    // Present window to the user
    window.present();
}

并在style.css目录( main.rs所在的同一目录)中创建一个具有以下内容的src/文件:

代码语言:javascript
复制
.hover-box {
    background-color: blue;
    border-radius: 5px;
}

.hover-box:hover {
    background-color: rgb(11, 133, 240);
    border-radius: 5px;
}

现在,您将得到一个gtk4::Box,它具有5px边框半径和blue背景色,当您将鼠标悬停在框上方时,背景色会发生变化。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69891299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档