linux下使用C语言进行GTK桌面程序开发
Posted 我要出家当道士
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux下使用C语言进行GTK桌面程序开发相关的知识,希望对你有一定的参考价值。
目录
1、环境部署
我的开发环境是 ubuntu18.04
编译工具:g++
GTK版本:3.0
GTK的安装方法见链接:
2、教程
具体的使用方法可以看官方文档,里面有详细的使用方法介绍。
The GTK Project - A free and open-source cross-platform widget toolkithttps://www.gtk.org/docs/getting-started/Gtk – 3.0https://docs.gtk.org/gtk3/
3、demo
我的需求是在ubuntu上开发一个文件拷贝的程序,需要界面输入源路径和目的路径。整体来说需求很简单,需要两个目录选择的控件和一个提交按钮。
#include <iostream>
#include <malloc.h>
#include <gtk/gtk.h>
using namespace std;
typedef struct _folderPath
void *srcfolder;
void *dstfolder;
folderPath;
static folderPath *path;
// gcc -o demo gtk_1.c `pkg-config --cflags --libs gtk+-3.0`
static void publisher (GtkWidget *widget, gpointer data)
folderPath *path = (folderPath *)data;
char *srcfolderStr = (char *)gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(path->srcfolder));
char *dstfolderStr = (char *)gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(path->dstfolder));
g_print ("src folder: %s\\ndst folder: %s\\n", srcfolderStr, dstfolderStr);
static void page (GtkApplication *app, gpointer user_data)
GtkWidget *window;
GtkWidget *srcFolderSelect, *dstFolderSelect;
GtkWidget *confirm;
GtkWidget *grid;
GtkWidget *srcTextView, *dstTextView;
GtkTextBuffer *srcTextBuffer, *dstTextBuffer;
// default path string
char folderpath[50] = "/home";
// window
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Publisher");
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
// container
grid = gtk_grid_new();
gtk_container_add (GTK_CONTAINER (window), grid);
// view
srcTextView = gtk_text_view_new();
dstTextView = gtk_text_view_new();
srcTextBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(srcTextView));
dstTextBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(dstTextView));
// Src Folder selector
gtk_text_buffer_set_text(srcTextBuffer, "Src Folder : ", -1);
// select folder
srcFolderSelect = gtk_file_chooser_button_new ("", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
// default path
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (srcFolderSelect), folderpath);
// add to grid
gtk_grid_attach(GTK_GRID(grid), srcTextView, 2, 1, 1, 1);
gtk_grid_attach(GTK_GRID(grid), srcFolderSelect, 3, 1, 2, 1);
// Dst Folder selector
gtk_text_buffer_set_text(dstTextBuffer, "Dst Folder : ", -1);
dstFolderSelect = gtk_file_chooser_button_new ("", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dstFolderSelect), folderpath);
gtk_grid_attach(GTK_GRID(grid), dstTextView, 2, 2, 1, 1);
gtk_grid_attach(GTK_GRID(grid), dstFolderSelect, 3, 2, 2, 1);
// confirm button
path->srcfolder = (void *)srcFolderSelect;
path->dstfolder = (void *)dstFolderSelect;
confirm = gtk_button_new_with_label ("publish");
g_signal_connect (confirm, "clicked", G_CALLBACK (publisher), (void *)path);
// add to grid
gtk_grid_attach(GTK_GRID(grid), confirm, 2, 3, 3, 1);
gtk_widget_show_all (window);
int start()
GtkApplication *app;
int status;
path = (folderPath *)malloc(sizeof(folderPath));
app = gtk_application_new ("com.yjz.publisher", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (page), NULL);
status = g_application_run (G_APPLICATION (app), 0, NULL);
g_object_unref (app);
free(path);
return status;
int main()
start();
return 0;
运行编译
gcc -o demo gtk_1.c `pkg-config --cflags --libs gtk+-3.0`
以上是关于linux下使用C语言进行GTK桌面程序开发的主要内容,如果未能解决你的问题,请参考以下文章
使用Fedora8 iso开发环境开发gtk3跨Linux多版本桌面应用