Цитата(tumanovalex @ 21.5.2020, 0:38)
При запуске в терминальном (консольном) режиме появляется ошибка "(cbGtkStudyWin.exe:1644): Gtk-CRITICAL **: gtk_box_reorder_child: assertion `GTK_IS_WIDGET (child)' failed". Просмотрел весь код несколько раз, но так и не понял, из-за чего возникает ошибка. Помогите, пожалуйста, разобраться
У меня похожая программа, правда под Linux. Файл colorbuttons.c:
#include <gtk/gtk.h>
static void destroy (GtkWidget*, gpointer);
static gboolean delete_event (GtkWidget*, GdkEvent*, gpointer);
static void color_changed(GtkColorButton*, GtkWidget*);
int main(int argc, char *argv[]) {
GtkWidget *window, *button, *label, *hbox;
GdkRGBA color;
gtk_init(&argc, &argv);
window= gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Color Button");
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
/* Set the initial color as #003366 and set the dialog title. */
gdk_rgba_parse(&color, "#003366");
button= gtk_color_button_new_with_rgba(&color);
gtk_color_button_set_title(GTK_COLOR_BUTTON(button), "Peek a Color");
label= gtk_label_new("Look at my color!");
color_changed(GTK_COLOR_BUTTON(button), label);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event),
NULL);
g_signal_connect(G_OBJECT(button), "color_set",
G_CALLBACK(color_changed),
(gpointer)label);
hbox= gtk_box_new(FALSE, 5);
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 5);
gtk_container_add(GTK_CONTAINER(window), hbox);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
/* Retrieve the selected color and set it as the GtkLabel's foreground color. */
static void color_changed(GtkColorButton *button, GtkWidget *label) {
GdkRGBA color;
gtk_color_chooser_get_rgba((GtkColorChooser*) button, &color);
GtkCssProvider *css_provider= gtk_css_provider_new();
GString *css_gstring= g_string_new("");
g_string_printf(css_gstring,
"* { background-image:none; "
"background-color:%s; }",
gdk_rgba_to_string(&color));
gtk_css_provider_load_from_data(css_provider,
css_gstring->str,
-1, NULL);
GtkStyleContext *context= gtk_widget_get_style_context(label);
gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(css_provider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
g_object_unref(css_provider);
g_string_free(css_gstring, TRUE);
}
/* Stop the GTK+ main loop function when the window is destroyed. */
static void destroy(GtkWidget *window, gpointer data) {
if (window && data == NULL) {
gtk_main_quit();
}
}
/* Return FALSE to destroy the widget. By returning TRUE, you can cancel
a delete-event. This can be used to confirm quitting the application. */
static gboolean delete_event(GtkWidget *window, GdkEvent *event,
gpointer data) {
if (window && event && data) {
return FALSE;
}
return FALSE;
}
Makefile:
source = colorbuttons.c
all: program
program: $(source)
gcc -g $(source) -o $@ \
`pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0`
clean:
rm program
Работает, ошибок не выдаёт.