Date: 2025jun19
Language: C++
Platform: gtkmm
Keywords: GTK, GNOME, gtkmm-4.0
Q. gtkmm4.x: access the command line arguments from your main window
A. If you have a standard main() like this:
int main(int argc, char* argv[]) { // BEFORE
auto app = Gtk::Application::create("com.example");
return app->make_window_and_run<MyWindow>(argc, argv);
}
Take advantage of main_window_and_run()'s ability to pass arguments to your constructor
int main(int argc, char* argv[]) { // AFTER
auto app = Gtk::Application::create("com.example");
// Pass argc and argv again, but this time it goes to your window
return app->make_window_and_run<MyWindow>(argc, argv, argc, argv);
}
And change your main window class to:
class MyWindow : public Gtk::Window
{
public:
MyWindow(int argc, char *argv[]) {
// - Use command line arguments
// - Do normal initialization
}
};