GTK Rust中按钮上的set_halign(gtk::Align::End)未右对齐

a9wyjsp7  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(115)
    • bounty将在2天后过期**。回答此问题可获得+50的声誉奖励。Daniel正在寻找来自声誉良好来源的答案

因此,我从阅读文档中的理解是,set_halign(gtk::Align::End)将使按钮向右对齐,但这并没有发生,它与具有set_halign(gtk::Align::Start)的按钮保持向左齐平。

pub fn create_widget(&self) -> impl IsA<Widget> {
  let frame1 = frame::create_frame(self, "frame1");

  let box1 = boxx::create_box(self, "box1");
  box1.set_orientation(Orientation::Vertical);
  frame1.add(&box1);

  let superman_box = boxx::create_box(self, "superman box");
  superman_box.set_orientation(Orientation:Horizontal);
  box1.add(&superman_box);

  let superman_button = button::create_text_button(self, "superman", "Superman");
  superman_button.connect_clicked(move |_| {});
  superman_button.set_halign(gtk::Align::Start);
  superman_box.add(&superman_button);

  let sessions_button = button::create_text_button(self, "sessions_button", "Sessions");
  sessions_button.connect_clicked(move |_| {});
  sessions_button.set_halign(gtk::Align::End);
  superman_box.add(&sessions_button);

  let logout_button = button::create_text_button(self, "logout_button", "Logout");
  logout_button.connect_clicked(move |_| {});
  logout_button.set_halign(gtk::Align::End);
  superman_box.add(&logout_button);
}
plicqrtu

plicqrtu1#

所以我决定重构整件事,把超人按钮放在自己的盒子里,会话和注销按钮放在自己的盒子里,就像这样:

let left_box = boxx::create_box(self, "left_box");
left_box.set_orientation(Orientation::Horizontal);
left_box.set_halign(gtk::Align::Start);
header_navbar.add(&left_box);

let left_box_label = label::create_text_label(self, "left_box_label", "Supervisor");
left_box.add(&left_box_label);

let right_box = boxx::create_box(self, "right_box");
right_box.set_orientation(Orientation::Horizontal);
right_box.set_halign(gtk::Align::End);
header_navbar.add(&right_box);

let sessions_button = button::create_text_button(self, "sessions_button", "Sessions");
sessions_button.connect_clicked(move |_| {eprintln!("Sessions button clicked!")});
sessions_button.set_halign(gtk::Align::End);
right_box.add(&sessions_button);

let logout_button = button::create_text_button(self, "logout_button", "Logout");
logout_button.connect_clicked(move |_| {eprintln!("Logout button clicked!")});
logout_button.set_halign(gtk::Align::End);
right_box.add(&logout_button);

相关问题