Search code examples
c++gtkmm

How to move my Gtk::Window to a specific position on screen in gtkmm 4


I subclassed Gtk::Window and i want to move it to a position on the screen, but i cannot find any method to do this. Was Gtk::Window::move removed in Gtk4 ? If yes, how can i move my window ?
I currently have this in the constructor of my window class :

this->set_title(_("Screenshot"));
// this is for getting the size of the screen, and calculating the center position
Glib::RefPtr<Glib::ObjectBase> first_monitor = Gdk::Display::get_default()->get_monitors()->get_object(0);
Gdk::Rectangle rect;
first_monitor->get_rectangle(rect);
int posx = (rect.get_width() - this->get_width()) / 2;
this->move(posx, -1)
this->show();

and it gives me following errors when compiling :

src/prefswindow.cpp: In constructor ‘PrefsWindow::PrefsWindow()’:
src/prefswindow.cpp:7:17: error: ‘using element_type = class Glib::ObjectBase {aka class Glib::ObjectBase}’ has no member named ‘get_rectangle’
  first_monitor->get_rectangle(rect);
                 ^~~~~~~~~~~~~
src/prefswindow.cpp:9:8: error: ‘class PrefsWindow’ has no member named ‘move’; did you mean ‘Role’?
  this->move(posx, -1)
        ^~~~
        Role

where class PrefsWindow inherits from Gtk::Window.
Thanks in advance !


Solution

  • Looks like GTK continues to devolve. See How to center GtkWindows in gtk4?, where the answer appears to be "no way, the gtk_window_set_position() API has been removed" (confirmed by a GTK core developer). And the comments in that thread seem to imply that GTK is going into the direction of removing everything related to window management and anything else related to screen, rather than the internal details of the application windows.

    So, as I understand it now, you have the following options:

    • use some platform-specific way of moving your window
    • switch widget toolkit (to e.g. Qt, or an older version of GTK+)
    • abandon the idea of moving your window.