如何使gtk4::Box在gtk_rs中可点击
在GTK3中,使用EventBox似乎是实现这一目标的方法,但是在GTK4中:
停止使用
GtkEventBoxGtkEventBox已不再需要,并已被删除。 所有小部件都接收所有事件。 https://docs.gtk.org/gtk4/migrating-3to4.html#stop-using-gtkeventbox
因此,单击处理程序现在应该直接附加到小部件上。但是,我找不到关于如何做到这一点的任何明确的文档或示例。
如果有人能给我一个将点击侦听器附加到gtk4::Box的例子,我们将不胜感激。
发布于 2021-11-11 10:55:47
您可以使用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目录( main.rs所在的同一目录)中创建一个具有以下内容的src/文件:
.hover-box {
background-color: blue;
border-radius: 5px;
}
.hover-box:hover {
background-color: rgb(11, 133, 240);
border-radius: 5px;
}现在,您将得到一个gtk4::Box,它具有5px边框半径和blue背景色,当您将鼠标悬停在框上方时,背景色会发生变化。
https://stackoverflow.com/questions/69891299
复制相似问题