gtk开发更新笔记

软件和网站开发以及相关技术探讨
回复
Jacky286
帖子: 111
注册时间: 2015-02-15 17:33
系统: Ubuntu 12.04

gtk开发更新笔记

#1

帖子 Jacky286 » 2018-12-01 15:15

从前glade2联编代码做过一些开发,现在glade3完全不同的风格,需要知识更新,做一下笔记。
一、旧版glade2到新版glade3原代码过渡
glade3.20完全不能观看之前glade2.x的构件参数,64位计算机也不能运行32位的软件,所以要更新。
1、可以采用virtualbox虚拟机方式安装ubuntu12.04.5,仍在支持中,虚拟机可以使用32位的系统,iso下载链接
http://cdimage.ubuntu.com/releases/12.04.5/release/
2、virtualbox虚拟机与实体机共享文件夹,需要安装增强包,完成后VirtualBox自动挂载,文件夹的用户组别为vboxsf,需增加当前用户到vboxsf用户组中
sudo adduser xxx vboxsf #xxx为虚拟机中的用户名
参考链接:
https://www.linuxidc.com/Linux/2017-11/148335.htm
3、在虚拟机的ubuntu12.04.5中安装glade,可以读取glade2.x的源文件

二、安装gtk+2.0开发环境
参考链接:
https://blog.csdn.net/shangguanyunlan/a ... s/51464815
一)安装
1、安装gcc/g++/gdb/make 等基本编程工具
sudo apt-get install build-essential
2、安装 libgtk2.0-dev libglib2.0-dev 等开发相关的库文件
sudo apt-get install gnome-core-devel
3、用于在编译GTK程序时自动找出头文件及库文件位置  
sudo apt-get install pkg-config
4、安装 devhelp GTK文档查看程序
sudo apt-get install devhelp
5、安装 gtk/glib 的API参考手册及其它帮助文档
sudo apt-get install libglib2.0-doc libgtk2.0-doc
6、安装基于GTK的界面GTK是开发Gnome窗口的c/c++语言图形库
sudo apt-get install glade libglade2-dev
或者
sudo apt-get install glade-gnome glade-common glade-doc
7、安装gtk2.0 或者 将gtk+2.0所需的所有文件统通下载安装完毕
sudo apt-get install libgtk2.0*
二)查看GTK库版本
1、查看1.2.x版本
pkg-config --modversion gtk+
2、查看 2.x 版本
pkg-config --modversion gtk+-2.0
3、查看pkg-config的版本
pkg-config --version
4、查看是否安装了gtk
pkg-config --list-all grep gtk
三)测试程序
//hello.c
#include <gtk/gtk.h>
int main(int argc,char *argv[]){
GtkWidget *window;
GtkWidget *label;

gtk_init(&argc,&argv);

/* create the main, top level, window */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

/* give it the title */
gtk_window_set_title(GTK_WINDOW(window),"Hello World");

/* connect the destroy signal of the window to gtk_main_quit
* when the window is about to be destroyed we get a notification and
* stop the main GTK+ loop
*/
g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);

/* create the "Hello, World" label */
label = gtk_label_new("Hello, World");

/* and insert it into the main window */
gtk_container_add(GTK_CONTAINER(window),label);

/* make sure that everything, window and label, are visible */
gtk_widget_show_all(window);

/* start the main loop, and let it rest until the application is closed */
gtk_main();

return 0;
}

1、编译
gcc -o hello hello.c `pkg-config --cflags --libs gtk+-2.0`
(‘ ’符号不是我们的单引号,而是Esc按键下面的那个按键,很容易出错)
2、运行
hello
程序结果:显示一个带有一个按钮的窗口,点击按钮以后窗口关闭,命令行显示Hello world!

三、安装gtk+-3.0开发环境
参考链接:
https://blog.csdn.net/javafoam/article/details/78986783
一)安装
1、安装GCC开发环境:
sudo apt install build-essential #前面已安装可略
2、安装pkg-config包:
sudo apt install pkg-config #前面已安装可略。注意包名,系统会提示另外还有个pkgconf,不是那个。
3、安装gtk+-3.0:
sudo apt install gtk+-3.0
4、安装Gnome开发环境
sudo apt install gnome-devel gnome-devel-docs #注意gnome包名
二)测试
1、编译
gcc hello.c -o main `pkg-config --cflags --libs gtk+-3.0`
2、运行
hello
程序结果:显示一个带有一个按钮的窗口,点击按钮以后窗口关闭,命令行显示Hello world!
Jacky286
帖子: 111
注册时间: 2015-02-15 17:33
系统: Ubuntu 12.04

hello1.c

#2

帖子 Jacky286 » 2018-12-01 23:14

三)结合glade文件
//Hello1.c
#include <gtk/gtk.h>
void bye_clicked_cb(GtkWidget *widget,gpointer data) {
gtk_main_quit();
}

void hello_clicked_cb(GtkWidget *widget,gpointer data)
{
GtkLabel *labeltext=(GtkLabel *)data;
if(g_strcmp0(gtk_label_get_label(labeltext),"Hello,World!")==0)
{
gtk_label_set_label(labeltext,"Very Nice.");
}
else
{
gtk_label_set_label(labeltext,"Chinese?");
}
}

int main(int argc,char *argv[]){
GtkWidget *window;
GtkLabel *labeltext;
GtkButton *hellobutton,*byebutton;
GtkBuilder *builder;

gtk_init(&argc,&argv);
builder = gtk_builder_new();//创建GtkBuilder对象
gtk_builder_add_from_file(builder, "testgtk3.glade", NULL);//读取glade文件

/* create the main, top level, window */
// window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "Mainwindow")); //获取并生成界面对象
gtk_window_set_title(GTK_WINDOW(window),"Hello World你好世界");
g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);

labeltext = GTK_LABEL(gtk_builder_get_object(builder, "label")); //获取标签构件
hellobutton=GTK_BUTTON(gtk_builder_get_object(builder, "hello"));//获取按钮构件
g_signal_connect(hellobutton, "clicked", G_CALLBACK(hello_clicked_cb), (gpointer)labeltext);// 将 hello 按钮的 "clicked" 事件与hello_clicked_cb函数关联.

byebutton=GTK_BUTTON(gtk_builder_get_object(builder, "bye"));
//g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(bye_clicked_cb), NULL);网上此代码会编译出错
g_signal_connect(byebutton, "clicked", G_CALLBACK(bye_clicked_cb), NULL);// 将 bye 按钮的 "clicked" 事件与bye_clicked_cb函数关联.

g_object_unref(G_OBJECT(builder));// 获取到 UI 对象后,GtkBuilder 对象已没有作用,释放

gtk_widget_show_all(window);//显示构件


gtk_main();//进行主循环直到退出

return 0;
}

/*下为testgtk3.glade*/

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="Mainwindow">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Hello</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="hello">
<property name="label" translatable="yes">Hello World!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="hello_clicked_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="bye">
<property name="label" translatable="yes">bye</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="bye_clicked_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

四)编译
gcc hello1.c -o hello1 `pkg-config --cflags --libs gtk+-3.0`
附件
运行截图
运行截图
2018-12-01 23-13-29屏幕截图.png (4.65 KiB) 查看 49663 次
Jacky286
帖子: 111
注册时间: 2015-02-15 17:33
系统: Ubuntu 12.04

gtk_file_selection_new()函数遇瓶颈,哪位熟手大虾了解怎么补救,支个招哈

#3

帖子 Jacky286 » 2018-12-02 20:25

hellobutton=GTK_BUTTON(gtk_builder_get_object(builder, "hello"));//获取按钮构件
g_signal_connect(hellobutton, "clicked", G_CALLBACK(hello_clicked_cb), (gpointer)labeltext);

两句也可以合并成:
g_signal_connect(GTK_BUTTON(gtk_builder_get_object(builder, "hello")), "clicked", G_CALLBACK(hello_clicked_cb), (gpointer)labeltext);

一样运行。
只是在学习gtk_file_selection_new()函数时,遇到了瓶颈
open_dialog = gtk_file_selection_new("请选择一个文件:");
编译出错:
hello3.c: In function ‘main’:
hello3.c:45:16: warning: implicit declaration of function ‘gtk_file_selection_new’; did you mean ‘gtk_font_selection_new’? [-Wimplicit-function-declaration]
open_dialog = gtk_file_selection_new("请选择一个文件:");
^~~~~~~~~~~~~~~~~~~~~~
gtk_font_selection_new
hello3.c:45:14: warning: assignment to ‘GtkWidget *’ {aka ‘struct _GtkWidget *’} from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
open_dialog = gtk_file_selection_new("请选择一个文件:");
^
/usr/bin/ld: /tmp/cc7h05jI.o: in function `main':
hello3.c:(.text+0x1ac): undefined reference to `gtk_file_selection_new'
collect2: error: ld returned 1 exit status

哪位熟手大虾了解怎么补救,支个招哈
附件
hello3.zip
hello3.c原代码
(1.26 KiB) 已下载 1510 次
hello3.c
hello3原代码
(2.49 KiB) 已下载 1569 次
头像
astolia
论坛版主
帖子: 6386
注册时间: 2008-09-18 13:11

Re: gtk开发更新笔记

#4

帖子 astolia » 2018-12-03 17:01

gtk_file_selection_new是早期gtk1时代的产物,早就被标记为了deprecated 。gtk2之后的替代品是 https://developer.gnome.org/gtk3/stable ... ialog.html 。gtk2里还为了兼容性保留,到了gtk3中,已经正式被取代
Jacky286
帖子: 111
注册时间: 2015-02-15 17:33
系统: Ubuntu 12.04

Re: gtk开发更新笔记

#5

帖子 Jacky286 » 2018-12-03 19:14

astolia 写了: 2018-12-03 17:01 gtk_file_selection_new是早期gtk1时代的产物,早就被标记为了deprecated 。gtk2之后的替代品是 https://developer.gnome.org/gtk3/stable ... ialog.html 。gtk2里还为了兼容性保留,到了gtk3中,已经正式被取代
谢谢了!

忽然又开始怀念起glade2的联编,
从前偶一样年少无知,
却在gtk2里遨游了一番,
不知道makefile,
不知道界面解析xml,
只知道autogen.sh
然而却开发出了专业领域的小软件
直到有一天,
glade2被遗弃,
说是为了界面与主程分离
我编程的热情被尘封十年
除了pyqt5让我重遇新欢

技术上的沟沟坎坎
譬如ubuntu 的unity
当一扇门关闭
也关闭了主意

那个被称为似乎重要方向的切割
从此是否成为架空的云梯
不得而见
hhsw1987
帖子: 1
注册时间: 2019-05-13 10:54
系统: ubuntu 16.0.4 lts

Re: gtk开发更新笔记

#6

帖子 hhsw1987 » 2019-05-13 10:59

你好,我安装的glade,系统bantu16.0.4LTs,总是右问题,怎么办??
GladeUI-Message: 1 missing displayable value for GtkWidget::events
GladeUI-Message: 20 missing displayable value for GtkWidget::AtkObject::accessible-role
GladeUI-Message: 1 missing displayable value for GtkEntry::input-hints
GladeUI-Message: 1 missing displayable value for GtkButtonBox::layout-style
GladeUI-Message: 1 missing displayable value for GtkScrolledWindow::hscrollbar-policy
GladeUI-Message: 1 missing displayable value for GtkCellRendererAccel::accel-mode
GladeUI-Message: Glade needs artwork; a default icon will be used for the following classes:
GtkApplicationWindow needs an icon named 'widget-gtk-applicationwindow'

(glade:7699): Gtk-CRITICAL **: gtk_widget_get_preferred_width_for_height: assertion 'height >= 0' failed

(glade:7699): Gtk-WARNING **: gtk_widget_size_allocate(): attempt to allocate widget with width 10 and height -3
回复