Reputation: 25623
I simply want to know which size a widget has. I need this info to set a ScrolledWindow to a maximum size if the size of the widget is bigger than the screen.
But all functions I know give a constant value of 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;
}
Upvotes: 3
Views: 1205
Reputation: 4296
This looks a lot like this answer.
Basically it says that before the get_height()
and get_width()
methods return meaningful values, the widget must be realized. Since you are calling these (through your GetSize()
wrapper) inside the window constructor, it (the button inside the window) might not yet be realized, hence the wrong values.
BONUS
According to this ticket:
Realize means to create the GDK resources for a widget. i.e. to instantiate the widget on the display.
Also, to clarify the meaning of the word realize, see this. The author seems to have done some interesting research on the subject to clarify the documentation.
Upvotes: 2