我有 2 个盒子和操作栏。当我将 box2 作为子项添加到窗口时。当我删除将 box2 添加到窗口的代码时,当我尝试运行编译的应用程序时,会出现这些错误。编译时没有错误,但当我尝试运行该应用程序时出现错误。
以下是错误:
(BIDE:12460): GLib-GObject-CRITICAL **: 22:50:34.898: invalid uninstantiatable type '(null)' in cast to 'GtkWidget'
(BIDE:12460): Gtk-CRITICAL **: 22:50:34.898: gtk_widget_get_parent: assertion 'GTK_IS_WIDGET (widget)' failed
(BIDE:12460): Gtk-CRITICAL **: 22:50:34.898: gtk_widget_insert_before: assertion 'GTK_IS_WIDGET (widget)' failed
(BIDE:12460): Gtk-CRITICAL **: 22:50:34.940: gtk_widget_get_visible: assertion 'GTK_IS_WIDGET (widget)' failed
(BIDE:12460): Gtk-CRITICAL **: 22:50:34.940: gtk_widget_child_focus: assertion 'GTK_IS_WIDGET (widget)' failed
Segmentation fault (core dumped)
这是我当前的代码:
#include <gtk/gtk.h>
#include <pango/pango.h>
#include <stdio.h>
//char verString[5] = "0.0.1";
char buffer[100];
//READERS! This code is meant to be easily read for people who want to learn!
GtkWidget *helpWindow;
static void showWindow() {
gtk_window_present(GTK_WINDOW(helpWindow));
// gtk_widget_set_visible(GTK_WIDGET(windowToOpen), true);
}
//Handles initializing the window
static void activateWindow(GtkApplication *app) {
GtkWidget *window = gtk_application_window_new(app);
helpWindow = gtk_application_window_new(app);
GtkWidget *sw = gtk_scrolled_window_new();
gtk_widget_set_vexpand(sw, TRUE);
//Info Bar | Bottom
GtkWidget *infoBar = gtk_action_bar_new();
GtkWidget *box2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_window_set_child(GTK_WINDOW(window), box2);
GtkWidget *sw2 = gtk_scrolled_window_new();
gtk_widget_set_vexpand(sw2, TRUE);
gtk_box_append(GTK_BOX(box2), infoBar);
gtk_box_append(GTK_BOX(box2), sw2);
GtkWidget *verInfo = gtk_label_new("Testing");
gtk_action_bar_pack_start(GTK_ACTION_BAR(infoBar), GTK_WIDGET(verInfo));
gtk_widget_set_size_request(GTK_WIDGET(verInfo),-1, 200);
//Stuff for help window
GtkWidget *ytURI = gtk_link_button_new_with_label("https://www.youtube.com/channel/UCvECibTCuY1Txj_DtZTddKQ", "My YouTube");
GtkWidget *gitURI = gtk_link_button_new_with_label("https://github.com/thatitalianguy08", "My Github");
gtk_window_set_child(GTK_WINDOW(helpWindow), GTK_WIDGET(ytURI));
gtk_window_set_child(GTK_WINDOW(helpWindow), GTK_WIDGET(gitURI));
//TopActionBar | Creation
GtkWidget *topActionBar = gtk_action_bar_new();
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(box));
gtk_box_append(GTK_BOX(box), topActionBar);
gtk_box_append(GTK_BOX(box), sw);
//TopActionBar | TITLE
GtkWidget *bideLabel = gtk_label_new(NULL);
gchar *bideLabelText = g_markup_printf_escaped("<b>BIDE</b>");
gtk_label_set_markup(GTK_LABEL(bideLabel), bideLabelText);
gtk_action_bar_pack_start(GTK_ACTION_BAR(topActionBar), GTK_WIDGET(bideLabel));
gtk_widget_set_size_request(GTK_WIDGET(bideLabel), -1, 50);
//TopActionBar | Open Folder Button
GtkWidget *openFolder = gtk_button_new();
gtk_button_set_label(GTK_BUTTON(openFolder), "Open Folder");
gtk_action_bar_pack_start(GTK_ACTION_BAR(topActionBar), GTK_WIDGET(openFolder));
gtk_widget_set_size_request(GTK_WIDGET(openFolder), -1, 50);
//TopActionBar | Help Menu Button
gchar *helpButtonText = g_markup_printf_escaped("<u>HELP</u>");
GtkWidget *helpButton = gtk_button_new();
GtkWidget *helpLabel = gtk_label_new("Help");
gtk_label_set_markup(GTK_LABEL(helpLabel), helpButtonText);
g_signal_connect_swapped(helpButton, "clicked", G_CALLBACK(showWindow), NULL);
gtk_widget_set_size_request(GTK_WIDGET(helpButton), -1, 50);
gtk_button_set_child(GTK_BUTTON(helpButton), GTK_WIDGET(helpLabel));
//TopActionBar | Save Button
GtkWidget *saveButton = gtk_button_new_with_label("Save File");
gtk_widget_set_size_request(GTK_WIDGET(saveButton), -1, 50);
gtk_action_bar_pack_start(GTK_ACTION_BAR(topActionBar), GTK_WIDGET(saveButton));
gtk_action_bar_pack_start(GTK_ACTION_BAR(topActionBar), GTK_WIDGET(helpButton));
gtk_window_set_hide_on_close(GTK_WINDOW(helpWindow), true);
gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(box));
gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(box2));
gtk_window_present(GTK_WINDOW(window));
}
//Main stuff
int main(int argc, char *argv[]){
GtkApplication *app = gtk_application_new("me.lufthor.bide", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "activate", G_CALLBACK(activateWindow), NULL);
return g_application_run(G_APPLICATION(app), argc, argv);
}
我尝试重新编写 box2/infoBar 的代码。
该程序中的问题是尝试向一个窗口添加多个子窗口。
一个窗口一次只能包含一个子窗口。因此,如果您想添加两个 GtkBox,则需要三个。将一个 GtkBox 添加到窗口中,然后将另外两个添加到第一个 GtkBox 中。 另外儿童不应添加超过一次。
我先注释掉了两行,然后得到了以下结果。
//Stuff for help window
GtkWidget *ytURI = gtk_link_button_new_with_label("https://www.youtube.com/channel/UCvECibTCuY1Txj_DtZTddKQ", "My YouTube");
GtkWidget *gitURI = gtk_link_button_new_with_label("https://github.com/thatitalianguy08", "My Github");
gtk_window_set_child(GTK_WINDOW(helpWindow), GTK_WIDGET(ytURI)); // Window set Child
// gtk_window_set_child(GTK_WINDOW(helpWindow), GTK_WIDGET(gitURI)); // Window set Child
和
gtk_action_bar_pack_start(GTK_ACTION_BAR(topActionBar), GTK_WIDGET(helpButton));
gtk_window_set_hide_on_close(GTK_WINDOW(helpWindow), true);
gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(box));
///gtk_window_set_child(GTK_WINDOW(window), GTK_WIDGET(box2));
gtk_window_present(GTK_WINDOW(window));