Get size of widget

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Get size of widget

Klaus Rudolph
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;
}
_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Get size of widget

Phil Wolff-2
A widget's size is negotiated when it is realized. As in one of your
other questions, signal_realized() should help.


On 12/26/2017 01:05 AM, Klaus Rudolph wrote:

> 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;
> }
> _______________________________________________
> gtkmm-list mailing list
> [hidden email]
> https://mail.gnome.org/mailman/listinfo/gtkmm-list

--
It is better to have tried and failed than to have failed to try, but
the result's the same.
    — Mike Dennison
_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Get size of widget

Kjell Ahlstedt-2
I modified your example, adding on_button_realize(). The output becomes

1 1
1 1
1 1
1 1
1 1
1 1
Button has been realized: 49 29
49 29

All your calls to GetSize() occur before the button is realized.

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

class ExampleWindow: public Gtk::Window
{
     Gtk::Button button;
     public:
     ExampleWindow(): button("Hallo")
     {
         add(button);
         GetSize();
         button.signal_realize().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_realize));
     }

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

     void on_button_realize()
     {
       std::cout << "Button has been realized: ";
       GetSize();
     }
};


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;
}

On 2017-12-26 14:59, Phil Wolff wrote:
> A widget's size is negotiated when it is realized. As in one of your
> other questions, signal_realized() should help.
>
>

_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Get size of widget

Daniel Boles
signal ::size-allocate, probably


_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list