IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

GTK+ avec C & C++ Discussion :

Problème avec gtk_main_quit()


Sujet :

GTK+ avec C & C++

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 92
    Par défaut Problème avec gtk_main_quit()
    Bonjour à tous,

    Pour un projet, j'ai besoin d'ajouter une petite GUI (une simple window) à partir d'un logiciel source existant.

    Puisque gtk_main() est bloquant, j'ai décidé (et c'est surement là une partie de mon erreur), de mettre toutes les instructions GTK (sauf le init()), dans un thread.

    Voici mon code :

    main / programme source
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    ....
    gtk_init(&argc, &argv);
    //To be implemented : detached thread    
    if(pthread_create(&thread_window_gtk, NULL, create_window, (void *)NULL) != 0)
    	perror("Error during GTK window thread creation\n!");
    ...
    GTK Window thread
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
     
    #include <stdlib.h>
    #include <gtk/gtk.h>
    #include <pthread.h>
     
    #include "ui_gtk.h"
     
    #define MAIN_WINDOW_WIDTH 600
    #define MAIN_WINDOW_HEIGHT 90
     
    void * create_window(void * data)
    {
        GtkWidget * MainWindow = NULL;
        GtkWidget * Main_Vertical_Box = NULL;
        GtkWidget * first_Horizontal_Box = NULL;
        GtkWidget * second_Horizontal_Box = NULL;
        GtkWidget * OK_Button = NULL;
        GdkScreen * MainScreen= NULL;
     
        gchar * user_information = NULL;
        gint MainWindow_width = MAIN_WINDOW_WIDTH;    
        gint MainWindow_height = MAIN_WINDOW_HEIGHT;
        gint Screen_width, Screen_height;
        gint adjusted_main_window_x_position, adjusted_main_window_y_position;
     
        printf("******* GTK Window thread created ***********\n");
     
        //Main window creation
        MainWindow = gtk_window_new(GTK_WINDOW_POPUP);
        g_signal_connect(G_OBJECT(MainWindow), "delete-event", G_CALLBACK(gtk_main_quit), NULL);  
     
        //Main box containers creation & initialization
        Main_Vertical_Box = gtk_vbox_new(FALSE, 0);
        first_Horizontal_Box = gtk_hbox_new(FALSE, 0);
        second_Horizontal_Box = gtk_hbox_new(FALSE, 0);
        gtk_container_add(GTK_CONTAINER(MainWindow), Main_Vertical_Box);
        gtk_box_pack_start(GTK_BOX(Main_Vertical_Box), first_Horizontal_Box, FALSE, FALSE, 5);
        gtk_box_pack_start(GTK_BOX(Main_Vertical_Box), second_Horizontal_Box, TRUE, FALSE, 5);
     
        //OK Button creation
        OK_Button = gtk_button_new_with_label("OK");
     
        //Window personalization
        gtk_window_set_default_size(GTK_WINDOW(MainWindow), MainWindow_width, MainWindow_height);
        gtk_window_set_position(GTK_WINDOW(MainWindow), GTK_WIN_POS_CENTER);
     
        //Retrieve screen resolution and adjust popup position (NOTE : Gravity center is upper left corner of windows by default)    
        MainScreen = gtk_window_get_screen(GTK_WINDOW(MainWindow)); 
        Screen_width = gdk_screen_get_width(MainScreen);
        Screen_height = gdk_screen_get_height(MainScreen);
        printf("Main screen resolution is %d*%d \n", Screen_width, Screen_height);
        adjusted_main_window_x_position = (Screen_width/2) - (MainWindow_width/2);
        adjusted_main_window_y_position = Screen_height - MainWindow_height - 10; 
        gtk_window_move(GTK_WINDOW(MainWindow), adjusted_main_window_x_position, adjusted_main_window_y_position);
     
          //Add widget
         ...
        ///
     
           //Add Button & connect "clicked" signal to quit UI interface
        gtk_box_pack_start(GTK_BOX(second_Horizontal_Box), OK_Button, TRUE, FALSE, 5);
        gtk_widget_set_size_request(OK_Button, 70, 30);
        g_signal_connect(G_OBJECT(OK_Button), "clicked", G_CALLBACK(OnDestroy), MainWindow);
     
        //Show main window and loop
        gtk_widget_show_all(MainWindow);
        gtk_main();
     
        printf("Test \n");
     
        return NULL;
    }
     
    void OnDestroy(GtkWidget * pWidget, gpointer pData)
    {
        gtk_widget_destroy(pData);
        gtk_main_quit();
    }
    Mon problème est que si je ne rajoute pas l'instruction :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     gtk_widget_destroy(pData);
    J'ai un comportement étrange puisque la fonction gtk_main retourne bien (le printf s'affiche) mais j'ai le droit à un message d'erreur système (ou plutot la fenêtre qui se colore en gris) me disant "Window not responding". L'application tourne néanmoins toujours en fond.

    Est-ce le fait d'utiliser GTK dans un thread qui ne lui convient pas à la terminaison ?
    Ou alors un problème dans mon code ?

    Dans tous les cas, pourquoi rajouter le gtk_widget_destroy semble (du moins en surface) résoudre mon problème ?

    Merci d'avance,

    CRC

  2. #2
    Modérateur

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2009
    Messages
    1 395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2009
    Messages : 1 395
    Par défaut
    Tu n'as pas à créer de thread pour quelque chose d'aussi simple. Tu crées tes fenêtres, puis tu appelles gtk_main. C'est la première page de n'importe quel tutoriel GTK+.

Discussions similaires

  1. VC++ Direct3D8, problème avec LPD3DXFONT et LPD3DTEXTURE8
    Par Magus (Dave) dans le forum DirectX
    Réponses: 3
    Dernier message: 03/08/2002, 11h10
  2. Problème avec [b]struct[/b]
    Par Bouziane Abderraouf dans le forum CORBA
    Réponses: 2
    Dernier message: 17/07/2002, 10h25
  3. Problème avec le type 'Corba::Any_out'
    Par Steven dans le forum CORBA
    Réponses: 2
    Dernier message: 14/07/2002, 18h48
  4. Problème avec la mémoire virtuelle
    Par Anonymous dans le forum CORBA
    Réponses: 13
    Dernier message: 16/04/2002, 16h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo