C ++中的多线程GTK对话框消息框

问题描述 投票:0回答:1

我已经用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`

将显示一个消息框和一个窗口。问题是打开消息框时,窗口中的控件没有出现。我认为这可能是因为消息框在与主窗口相同的线程上运行。有什么方法可以向GTK添加多线程吗?

退出主窗口后,打印到控制台的消息框的返回值也在运行。

c++ linux gtk
1个回答
0
投票

[gtk_dialog_run正在阻止:有关更多信息,请参见official documentation

[如果出于某种原因要在启动时显示对话框,请添加一个GSourceFunc,该创建并使用g_idle_add运行该对话框。这将在GTK循环启动且所有内容均已正确绘制后将执行排队。有关主循环如何工作的更多信息,请参见g_idle_add

© www.soinside.com 2019 - 2024. All rights reserved.