gtkmm 获取小部件的大小

gtkmm get size of widget

我只是想知道小部件的大小。如果小部件的尺寸大于屏幕,我需要此信息将 ScrolledWindow 设置为最大尺寸。

但是我知道的所有函数都给出一个常量值 1。

#include <iostream>
#include <gtkmm.h>
#include <gtkmm/window.h>

class ExampleWindow: public Gtk::Window
{
    Gtk::Button button;
    public:
    ExampleWindow(): button("Hallo")
    {
        add(button);
        GetSize();
    }

    void GetSize()
    {
        std::cout << button.get_width() << " " << button.get_height() << std::endl;
        std::cout << button.get_allocated_width() << " " << button.get_allocated_height() << std::endl;
    }
};


int main(int argc, char* argv[])
{
    Gtk::Main kit(argc, argv);

    ExampleWindow window;

    window.GetSize();
    window.show_all_children();
    window.GetSize();
    Gtk::Main::run(window);
    return 0;
}

这看起来很像 this answer

基本上它说在 get_height()get_width() 方法 return 有意义的值之前,小部件必须 实现 。由于您在 window 构造函数中调用这些(通过 GetSize() 包装器),它(window 中的按钮)可能尚未实现,因此值错误。


奖金

根据this ticket

Realize means to create the GDK resources for a widget. i.e. to instantiate the widget on the display.

另外,为了阐明实现这个词的含义,请参阅this。作者似乎对该主题进行了一些有趣的研究以澄清文档。