GTK3 和 Glade 在 C 语言中的问题

huangapple go评论51阅读模式
英文:

Problem with GTK3 and Glade in C language

问题

我的代码编译没有错误和警告,但当我运行它时,按钮不起作用,尽管信号已连接。

我正在使用C语言、GTK3和Glade,并使用Code::Blocks作为IDE。

我不明白为什么按钮不起作用。信号已连接,而且没有来自每个按钮的命令行输出。我是不是连接了错误的信号或者出了什么问题?

预期按钮执行函数,但它们没有响应。

英文:

my code compiles with no errors and no warnings, but, when i run it, the buttons doesnt work, even thought the signals are connected.

I am using C language, GTK3 and Glade, and using Code::Blocks as IDE.

The code:

#include <stdio.h>
#include <stdlib.h>
#include "manipulador_texto.h"
#include <gtk/gtk.h>

/*
ESTE PROGRAMA CRIA CADASTROS SIMPLES E OS ARMAZENA EM FORMATO DE ARQUIVOS BINARIOS
As funções estão documentadas no arquivo manipulador_texto.c

Estrutura basica do cadastro:
    int codigo; - código identificador do cadastro, gerado automaticamente
    int idade;
    int telefone;
    char nome[20];
    int cpf;

    Progresso: modelo definido, falta a criação do arquivo em si
    Função de cadastro novo:
    Função de listar cadastros: pronto!
    Função de alterar cadastros: nada feito

*/

void fechar_janela();
void listar_cadastro();
void teste();

int main(int argc, char **argv)
{
    GtkBuilder *construtor = NULL;
    GtkWidget *Janela = NULL;
    GtkWidget *box1 = NULL;
    GtkWidget *botao_criar_cadastro = NULL;
    GtkWidget *botao_listar_cadastro = NULL;
    GtkWidget *botao_alterar_cadastro = NULL;
    GtkWidget *criar_janela = NULL;

    gtk_init(&argc, &argv);

    construtor = gtk_builder_new_from_file("glade.glade");
    if(construtor == NULL)
        {
            printf("ERRO! o arquivo glade.glade nao foi encontrado!\n");
            return 1;
        }

    Janela = GTK_WIDGET(gtk_builder_get_object(construtor, "Janela"));

    box1 = GTK_WIDGET(gtk_builder_get_object(construtor, "box1"));

    if(box1 == NULL)
    {
        printf("ERRO! nao foi possivel carregar o Widget box1\n");
        return 1;
    }

    botao_criar_cadastro = GTK_WIDGET(gtk_builder_get_object(construtor, "botao_criar_cadastro"));

    if(botao_criar_cadastro == NULL)
    {
        printf("ERRO! nao foi possivel carregar o Widget botao_criar_cadastro\n");
        return 1;
    }

    botao_listar_cadastro = GTK_WIDGET(gtk_builder_get_object(construtor, "botao_listar_cadastro"));

    if(botao_listar_cadastro == NULL)
    {
        printf("ERRO! nao foi possivel carregar o Widget botao_listar_cadastro\n");
        return 1;
    }

    botao_alterar_cadastro = GTK_WIDGET(gtk_builder_get_object(construtor, "botao_alterar_cadastro"));

    if(botao_alterar_cadastro == NULL)
    {
        printf("ERRO! nao foi possivel carregar o Widget botao_alterar_cadastro\n");
        return 1;
    }

    criar_janela = GTK_WIDGET(gtk_builder_get_object(construtor, "criar_janela"));

    if(botao_alterar_cadastro == NULL)
    {
        printf("ERRO! nao foi possivel carregar o Widget criar_janela\n");
        return 1;
    }


    g_signal_connect(Janela, "destroy", G_CALLBACK(fechar_janela), NULL);

    g_signal_connect(botao_criar_cadastro, "activate", G_CALLBACK(criar_cadastro), NULL);

    g_signal_connect(botao_listar_cadastro, "activate", G_CALLBACK(listar_cadastro), NULL);

    g_signal_connect(botao_alterar_cadastro, "activate", G_CALLBACK(alterar_cadastro), NULL);

    g_signal_connect(criar_janela, "activate", G_CALLBACK(teste), NULL);

    g_object_unref(construtor);

    gtk_widget_show(Janela);

    gtk_main();

    return 0;
}

void fechar_janela()
{
    gtk_main_quit();
}

void teste()
{
    GtkWidget *novaJanela = NULL;

    novaJanela = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(novaJanela), 400,400);
    gtk_window_set_title(GTK_WINDOW(novaJanela), "TESTE!");

    gtk_widget_show_all(novaJanela);

    return;

}

I do not understand why the buttons doesnt work. The signals are connected, and there is no output in the command line from every button. I am connecting the signals wrong or what?

Expected the buttons to execute the functions, but they do not respond.

答案1

得分: 1

你需要使用 clicked 信号而不是 activate 信号来处理按钮。

英文:

You need to use the clicked signal instead of the activate signal for the buttons.

huangapple
  • 本文由 发表于 2023年5月25日 20:49:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定