当消息框出现时控件消失 C++ GTK
Controls disappearing when a message box appears C++ GTK
我已经用 C++ 编写了 GUI。
#include <gtk/gtk.h>
#include <unistd.h>
#include <cstddef>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
int main(){
GtkWidget *_LinuxWindow, *_Box, *_Button;
int argC = 0;
char** argV;
// Setup the window and fixed grid
gtk_init(&argC,&argV);
_LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
_Box = gtk_fixed_new();
// Set the title
gtk_window_set_title(GTK_WINDOW(_LinuxWindow),"Title");
// Finish up
gtk_widget_show(_LinuxWindow);
g_signal_connect(G_OBJECT(_LinuxWindow),"destroy",G_CALLBACK(gtk_main_quit), NULL);
// Add controls
_Button = gtk_button_new_with_label("Click Me!");
gtk_fixed_put(GTK_FIXED(_Box),_Button,20,20);
gtk_fixed_move(GTK_FIXED(_Box),_Button,20,20);
gtk_widget_show(_Box);
gtk_widget_set_size_request(_Button,30,100);
// Create a dialog
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(_LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_INFO,GTK_BUTTONS_OK_CANCEL,"OK or Cancel?",NULL,g_strerror(errno));
gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
printf("%i", ret);
// Add the fixed grid and go the to the main window loop
gtk_container_add(GTK_CONTAINER(_LinuxWindow),_Box);
gtk_widget_show(_Box);
gtk_widget_show_all(_LinuxWindow);
gtk_main();
return 0;
}
当我运行它使用
g++ -o out-withoutCross without-cross.cpp -lX11 `pkg-config --cflags gtk+-3.0`pkg-config --libs gtk+-3.0`
出现一个消息框和一个 window。问题是 window 中的控件在消息框打开时不会出现。我认为这可能是因为消息框 运行 与主 window 在同一个线程上。有什么方法可以给 GTK 添加多线程吗?
此外,打印到控制台的消息框的 return 值是 运行ning 在主要 window 退出后。
问题是你的代码结构..\
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
GtkWidget *_LinuxWindow, *_Box, *_Button;
int argC = 0;
char **argV;
// Setup the window and fixed grid
gtk_init(&argC, &argV);
_LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
_Box = gtk_fixed_new();
// Set the title
gtk_window_set_title(GTK_WINDOW(_LinuxWindow), "Title");
// Finish up
gtk_widget_show_all(_LinuxWindow);
g_signal_connect(G_OBJECT(_LinuxWindow), "destroy", G_CALLBACK(gtk_main_quit),
NULL);
// Add controls
_Button = gtk_button_new_with_label("Click Me!");
gtk_fixed_put(GTK_FIXED(_Box), _Button, 20, 20);
gtk_fixed_move(GTK_FIXED(_Box), _Button, 20, 20);
gtk_widget_set_size_request(_Button, 30, 100);
gtk_container_add(GTK_CONTAINER(_LinuxWindow), _Box);
gtk_widget_show_all(_LinuxWindow);
// Create a dialog
GtkWidget *dialog;
dialog = gtk_message_dialog_new(
GTK_WINDOW(_LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO, GTK_BUTTONS_OK_CANCEL, "OK or Cancel?", NULL);
gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
printf("%i", ret);
// Add the fixed grid and go the to the main window loop
gtk_main();
return 0;
}
gtk_dialog_run 是一个阻塞函数,所以它会等到它从对话框中得到响应.. 才能继续
在您的代码中,您仅在 运行 对话框 window(这是一个阻塞调用)之后才将按钮添加到 window,因此在执行之后,按钮正在添加到 window
我已经用 C++ 编写了 GUI。
#include <gtk/gtk.h>
#include <unistd.h>
#include <cstddef>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
int main(){
GtkWidget *_LinuxWindow, *_Box, *_Button;
int argC = 0;
char** argV;
// Setup the window and fixed grid
gtk_init(&argC,&argV);
_LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
_Box = gtk_fixed_new();
// Set the title
gtk_window_set_title(GTK_WINDOW(_LinuxWindow),"Title");
// Finish up
gtk_widget_show(_LinuxWindow);
g_signal_connect(G_OBJECT(_LinuxWindow),"destroy",G_CALLBACK(gtk_main_quit), NULL);
// Add controls
_Button = gtk_button_new_with_label("Click Me!");
gtk_fixed_put(GTK_FIXED(_Box),_Button,20,20);
gtk_fixed_move(GTK_FIXED(_Box),_Button,20,20);
gtk_widget_show(_Box);
gtk_widget_set_size_request(_Button,30,100);
// Create a dialog
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(_LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_INFO,GTK_BUTTONS_OK_CANCEL,"OK or Cancel?",NULL,g_strerror(errno));
gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
printf("%i", ret);
// Add the fixed grid and go the to the main window loop
gtk_container_add(GTK_CONTAINER(_LinuxWindow),_Box);
gtk_widget_show(_Box);
gtk_widget_show_all(_LinuxWindow);
gtk_main();
return 0;
}
当我运行它使用
g++ -o out-withoutCross without-cross.cpp -lX11 `pkg-config --cflags gtk+-3.0`pkg-config --libs gtk+-3.0`
出现一个消息框和一个 window。问题是 window 中的控件在消息框打开时不会出现。我认为这可能是因为消息框 运行 与主 window 在同一个线程上。有什么方法可以给 GTK 添加多线程吗?
此外,打印到控制台的消息框的 return 值是 运行ning 在主要 window 退出后。
问题是你的代码结构..\
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
GtkWidget *_LinuxWindow, *_Box, *_Button;
int argC = 0;
char **argV;
// Setup the window and fixed grid
gtk_init(&argC, &argV);
_LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
_Box = gtk_fixed_new();
// Set the title
gtk_window_set_title(GTK_WINDOW(_LinuxWindow), "Title");
// Finish up
gtk_widget_show_all(_LinuxWindow);
g_signal_connect(G_OBJECT(_LinuxWindow), "destroy", G_CALLBACK(gtk_main_quit),
NULL);
// Add controls
_Button = gtk_button_new_with_label("Click Me!");
gtk_fixed_put(GTK_FIXED(_Box), _Button, 20, 20);
gtk_fixed_move(GTK_FIXED(_Box), _Button, 20, 20);
gtk_widget_set_size_request(_Button, 30, 100);
gtk_container_add(GTK_CONTAINER(_LinuxWindow), _Box);
gtk_widget_show_all(_LinuxWindow);
// Create a dialog
GtkWidget *dialog;
dialog = gtk_message_dialog_new(
GTK_WINDOW(_LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO, GTK_BUTTONS_OK_CANCEL, "OK or Cancel?", NULL);
gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
printf("%i", ret);
// Add the fixed grid and go the to the main window loop
gtk_main();
return 0;
}
gtk_dialog_run 是一个阻塞函数,所以它会等到它从对话框中得到响应.. 才能继续
在您的代码中,您仅在 运行 对话框 window(这是一个阻塞调用)之后才将按钮添加到 window,因此在执行之后,按钮正在添加到 window