处理命令行参数时不显示 GUI
GUI doesn't display when handling command line arguments
问题是程序只处理命令行参数并退出,而不是显示 GUI。
例如:
#include <gtk/gtk.h>
static gint print_cmd_arg(GApplication *app, GApplicationCommandLine *app_cmd, int *argc)
{
if (*argc > 1)
{
char **argv = g_application_command_line_get_arguments(app_cmd, argc);
GFile *file = g_file_new_for_commandline_arg(argv[1]);
if (g_file_query_exists(file, NULL))
{
char *text;
g_file_load_contents(file, NULL, &text, NULL, NULL, NULL);
g_print("%s", text);
g_free(text);
g_object_unref(file);
return 0;
}
else
{
g_print("File \'%s\' does not exist.\n", argv[1]);
g_object_unref(file);
return 1;
}
}
return 2;
}
static void activation(GtkApplication *app, gpointer user_data)
{
GtkWidget *window = gtk_application_window_new(app);
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_set_title(GTK_WINDOW(window), "Test");
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_widget_destroy), NULL);
GtkWidget *button = gtk_button_new_with_label("Quit");
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
}
int main(int argc, char **argv)
{
int status;
GtkApplication *test = gtk_application_new("this.is.only.a.test", G_APPLICATION_NON_UNIQUE | G_APPLICATION_HANDLES_COMMAND_LINE);
g_signal_connect(test, "activate", G_CALLBACK(activation), NULL);
g_signal_connect(G_APPLICATION(test), "command-line", G_CALLBACK(print_cmd_arg), &argc);
status = g_application_run(G_APPLICATION(test), argc, argv);
return status;
}
试试 运行 这个,你会发现这个程序完全忽略了函数 activation
.
我想让这个程序做的是处理命令行参数和显示 GUI。
此外,我知道我应该在 print_cmd_arg
中使用 g_application_command_line_set_exit_status() 而不是 return
,但我不知道如何执行此操作并收到编译器警告。
由于增加了灵活性,处理命令行在 GtkApplication/GApplication 上下文中是一个复杂的主题。
如果您是 "overriding" 命令行 handling/parsing 那么可以使用多种方法。
关于这个主题的信息有些分散在文档中。其中一些在这里:
- https://wiki.gnome.org/HowDoI/GtkApplication
- https://wiki.gnome.org/HowDoI/GtkApplication/CommandLine
- https://developer.gnome.org/gio//2.30/GApplication.html#g-application-run
- https://developer.gnome.org/gtk3/stable/GtkApplication.html
- https://developer.gnome.org/gtk3/stable/gtk-getting-started.html
来自1)您可以阅读:
Dealing with the command line
Normally, GtkApplication will assume that arguments passed on the
command line are files to be opened. If no arguments are passed, then
it assumes that an application is being launched to show its main
window or an empty document. In the case that files were given, you
will receive these files (in the form of GFile) from the open signal.
Otherwise you will receive an activate signal. It is recommended that
new applications make use of this default handling of command line
arguments.
If you want to deal with command line arguments in more advanced ways,
there are several (complementary) mechanisms by which you can do this.
重要的一点是:
Note in particular that it is possible to use action activations
instead of activate or open. It is perfectly reasonable that an
application could start without an activate signal ever being emitted.
activate is only supposed to be the default "started with no options" signal. Actions are meant to be used for anything else.
因此,有多种方法可以处理参数。问题是你想做什么以及你想做什么。如果参数是文件,则可以使用 open
信号处理它们并避免处理命令行。
要解决实际问题,正如您可能得出的结论,激活不会被触发,但您可以通过在命令行回调中使用 g_application_activate
来实现,例如:
static gint print_cmd_arg(GApplication *app, GApplicationCommandLine *app_cmd, int *argc)
{
/* ADD THIS TO YOUR CODE */
g_application_activate(app);
/* ENDS HERE */
if (*argc > 1)
{
char **argv = g_application_command_line_get_arguments(app_cmd, argc);
GFile *file = g_file_new_for_commandline_arg(argv[1]);
if (g_file_query_exists(file, NULL))
{
char *text;
g_file_load_contents(file, NULL, &text, NULL, NULL, NULL);
g_print("%s", text);
g_free(text);
g_object_unref(file);
return 0;
}
else
{
g_print("File \'%s\' does not exist.\n", argv[1]);
g_object_unref(file);
return 1;
}
}
return 2;
}
问题是程序只处理命令行参数并退出,而不是显示 GUI。
例如:
#include <gtk/gtk.h>
static gint print_cmd_arg(GApplication *app, GApplicationCommandLine *app_cmd, int *argc)
{
if (*argc > 1)
{
char **argv = g_application_command_line_get_arguments(app_cmd, argc);
GFile *file = g_file_new_for_commandline_arg(argv[1]);
if (g_file_query_exists(file, NULL))
{
char *text;
g_file_load_contents(file, NULL, &text, NULL, NULL, NULL);
g_print("%s", text);
g_free(text);
g_object_unref(file);
return 0;
}
else
{
g_print("File \'%s\' does not exist.\n", argv[1]);
g_object_unref(file);
return 1;
}
}
return 2;
}
static void activation(GtkApplication *app, gpointer user_data)
{
GtkWidget *window = gtk_application_window_new(app);
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_set_title(GTK_WINDOW(window), "Test");
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_widget_destroy), NULL);
GtkWidget *button = gtk_button_new_with_label("Quit");
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
}
int main(int argc, char **argv)
{
int status;
GtkApplication *test = gtk_application_new("this.is.only.a.test", G_APPLICATION_NON_UNIQUE | G_APPLICATION_HANDLES_COMMAND_LINE);
g_signal_connect(test, "activate", G_CALLBACK(activation), NULL);
g_signal_connect(G_APPLICATION(test), "command-line", G_CALLBACK(print_cmd_arg), &argc);
status = g_application_run(G_APPLICATION(test), argc, argv);
return status;
}
试试 运行 这个,你会发现这个程序完全忽略了函数 activation
.
我想让这个程序做的是处理命令行参数和显示 GUI。
此外,我知道我应该在 print_cmd_arg
中使用 g_application_command_line_set_exit_status() 而不是 return
,但我不知道如何执行此操作并收到编译器警告。
由于增加了灵活性,处理命令行在 GtkApplication/GApplication 上下文中是一个复杂的主题。
如果您是 "overriding" 命令行 handling/parsing 那么可以使用多种方法。
关于这个主题的信息有些分散在文档中。其中一些在这里:
- https://wiki.gnome.org/HowDoI/GtkApplication
- https://wiki.gnome.org/HowDoI/GtkApplication/CommandLine
- https://developer.gnome.org/gio//2.30/GApplication.html#g-application-run
- https://developer.gnome.org/gtk3/stable/GtkApplication.html
- https://developer.gnome.org/gtk3/stable/gtk-getting-started.html
来自1)您可以阅读:
Dealing with the command line
Normally, GtkApplication will assume that arguments passed on the command line are files to be opened. If no arguments are passed, then it assumes that an application is being launched to show its main window or an empty document. In the case that files were given, you will receive these files (in the form of GFile) from the open signal. Otherwise you will receive an activate signal. It is recommended that new applications make use of this default handling of command line arguments.
If you want to deal with command line arguments in more advanced ways, there are several (complementary) mechanisms by which you can do this.
重要的一点是:
Note in particular that it is possible to use action activations instead of activate or open. It is perfectly reasonable that an application could start without an activate signal ever being emitted. activate is only supposed to be the default "started with no options" signal. Actions are meant to be used for anything else.
因此,有多种方法可以处理参数。问题是你想做什么以及你想做什么。如果参数是文件,则可以使用 open
信号处理它们并避免处理命令行。
要解决实际问题,正如您可能得出的结论,激活不会被触发,但您可以通过在命令行回调中使用 g_application_activate
来实现,例如:
static gint print_cmd_arg(GApplication *app, GApplicationCommandLine *app_cmd, int *argc)
{
/* ADD THIS TO YOUR CODE */
g_application_activate(app);
/* ENDS HERE */
if (*argc > 1)
{
char **argv = g_application_command_line_get_arguments(app_cmd, argc);
GFile *file = g_file_new_for_commandline_arg(argv[1]);
if (g_file_query_exists(file, NULL))
{
char *text;
g_file_load_contents(file, NULL, &text, NULL, NULL, NULL);
g_print("%s", text);
g_free(text);
g_object_unref(file);
return 0;
}
else
{
g_print("File \'%s\' does not exist.\n", argv[1]);
g_object_unref(file);
return 1;
}
}
return 2;
}