简体   繁体   English

Gtk :: Layout中的gtkmm居中小部件

[英]gtkmm centering widget in Gtk::Layout

I'm trying to create a fullscreen window covered with an image and one entry widget in the middle of the window. 我正在尝试创建一个全屏窗口,其中包含图像和一个位于窗口中间的条目小部件。

I already have the fullscreen window with the image, however, I'm struggling with positioning the entry box exactly in the middle of the window. 我已经有一个全屏的图像窗口,但是,我很难将输入框准确地定位在窗口中间。 I tried Gtk::Alignment , Gtk::VBox , Gtk::HBox , Gtk::Table and many other containers, but to be honest, I don't really understand all the containers how do they behave. 我尝试了Gtk::AlignmentGtk::VBoxGtk::HBoxGtk::Table和许多其他容器,但是老实说,我并不真正了解所有容器的行为。 I am used to WIN32 API with absolute positioning and this is way different. 我习惯使用绝对定位的WIN32 API,这是完全不同的方法。 I know I can use something like fixed positioning in Gtk/gtkmm, however, it does not seem like the cleanest solution to me. 我知道我可以在Gtk / gtkmm中使用固定定位之类的方法,但是,对于我来说,这似乎不是最干净的解决方案。

class App : public Gtk::Window {
public:
    [...]
    App() {
        fullscreen();
        Gdk::Rectangle rec = get_screen()->get_monitor_workarea(get_screen()->get_primary_monitor());
        set_default_size(rec.get_width(), rec.get_height());
        show();

        m_bgImage.set("image.jpg");
        m_layout.add(m_bgImage);

        m_entry.set_size_request(300, 30);
        m_entry.set_opacity(0.5);
        m_entry.set_visibility(false);
        m_entry.signal_activate().connect(sigc::mem_fun(*this, &PadlockGui::onPasswordEntryReturn));
        m_entry.set_icon_from_icon_name("edit-clear", Gtk::ENTRY_ICON_SECONDARY);
        m_entry.signal_icon_press().connect(sigc::mem_fun(*this, &PadlockGui::clearPasswordEntry));

        m_layout.add(m_entry);
        add(m_layout);

        show_all_children();
    }

private:
    Gtk::Layout m_layout;
    Gtk::Image m_bgImage;
    Gtk::Entry m_entry;
    [...]
};

Here is a picture of my current situation: 这是我当前情况的图片: 在此处输入图片说明

Here is what I am trying to achieve: 这是我要实现的目标: 在此处输入图片说明

I would suggest using CSS to create the background image rather than Gtk::Image . 我建议使用CSS创建背景图像,而不是Gtk::Image Then you can just put the entry directly in the window, and make sure its expand property is set to false and its halign and valign properties are centered. 然后,您可以直接将条目放置在窗口中,并确保将其expand属性设置为false并将其halignvalign属性居中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM