如何用gtk3编译gtk2.0程序?

发布于 2024-12-03 20:25:49 字数 2279 浏览 2 评论 0原文

我有一个简单的程序,在ubuntu中用gtk2.0编译。在ubuntu11.04中我安装了gtk3。然后我编译相同的代码,我在下面一行中出现错误,

/* Add a timer callback to update the value of the progress bar */
timer = gtk_timeout_add (100, progress_timeout, pdata);

我只是注释该行并重新编译它。然后我得到了输出文件。但如果没有注释行,它就无法正常工作。

在 gtk2.0 中,我通过以下命令进行编译

gcc progressbar.c `pkg-config --cflags --libs gtk+-2.0`

,在 gtk3 中,

gcc progressbar.c `pkg-config --cflags --libs gtk+-3.0`

我怀疑 gtk3 中的该方法是否有任何弃用。请给我一个带有示例的简单文档的链接。2 和 3 之间的主要区别是什么。 完整源码如下所示

#include <gtk/gtk.h>

typedef struct _ProgressData {
GtkWidget *pbar; 
} ProgressData;

gint progress_timeout( gpointer data )
{
  ProgressData *pdata = (ProgressData *)data;
  gdouble new_val;
  new_val = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (pdata->pbar)) + 0.01;

  if (new_val > 1.0)
new_val = 0.0;

  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pdata->pbar), new_val);

  return TRUE;
} 


int main( int   argc,
      char *argv[])
{
ProgressData *pdata;
GtkWidget *align;
GtkWidget *window;

int timer;  

GtkWidget *vbox;

gtk_init (&argc, &argv);

/* Allocate memory for the data that is passed to the callbacks */
pdata = g_malloc (sizeof (ProgressData));

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_resizable (GTK_WINDOW (window), TRUE);

    g_signal_connect ( window, "destroy", gtk_main_quit, NULL ) ;

gtk_window_set_title (GTK_WINDOW (window), "GtkProgressBar");
gtk_container_set_border_width (GTK_CONTAINER (window), 0);

vbox = gtk_vbox_new (FALSE, 5);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);

/* Create a centering alignment object */
align = gtk_alignment_new (0.5, 0.5, 0, 0);
gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 5);
gtk_widget_show (align);

/* Create the GtkProgressBar */
pdata->pbar = gtk_progress_bar_new ();

gtk_container_add (GTK_CONTAINER (align), pdata->pbar);
gtk_widget_show (pdata->pbar);

/* Add a timer callback to update the value of the progress bar */
timer = gtk_timeout_add (100, progress_timeout, pdata);


gtk_widget_show (window);

gtk_main ();

return 0;
}

i have a simple program which is compiled with gtk2.0 in ubuntu.In ubuntu11.04 i installed gtk3.then i compile the same code,i got an error in the following line

/* Add a timer callback to update the value of the progress bar */
timer = gtk_timeout_add (100, progress_timeout, pdata);

i just comment the line and recompile it.then i got output file.but it is not properly working without the commented line.

in gtk2.0 i compiled by the following command

gcc progressbar.c `pkg-config --cflags --libs gtk+-2.0`

and in gtk3

gcc progressbar.c `pkg-config --cflags --libs gtk+-3.0`

I have a doubt that,is there any deprecation in that method in gtk3.please give me a link to an easy documentation with examples.what are the main difference between 2 and 3.
The full source code is as shown below

#include <gtk/gtk.h>

typedef struct _ProgressData {
GtkWidget *pbar; 
} ProgressData;

gint progress_timeout( gpointer data )
{
  ProgressData *pdata = (ProgressData *)data;
  gdouble new_val;
  new_val = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (pdata->pbar)) + 0.01;

  if (new_val > 1.0)
new_val = 0.0;

  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pdata->pbar), new_val);

  return TRUE;
} 


int main( int   argc,
      char *argv[])
{
ProgressData *pdata;
GtkWidget *align;
GtkWidget *window;

int timer;  

GtkWidget *vbox;

gtk_init (&argc, &argv);

/* Allocate memory for the data that is passed to the callbacks */
pdata = g_malloc (sizeof (ProgressData));

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_resizable (GTK_WINDOW (window), TRUE);

    g_signal_connect ( window, "destroy", gtk_main_quit, NULL ) ;

gtk_window_set_title (GTK_WINDOW (window), "GtkProgressBar");
gtk_container_set_border_width (GTK_CONTAINER (window), 0);

vbox = gtk_vbox_new (FALSE, 5);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);

/* Create a centering alignment object */
align = gtk_alignment_new (0.5, 0.5, 0, 0);
gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 5);
gtk_widget_show (align);

/* Create the GtkProgressBar */
pdata->pbar = gtk_progress_bar_new ();

gtk_container_add (GTK_CONTAINER (align), pdata->pbar);
gtk_widget_show (pdata->pbar);

/* Add a timer callback to update the value of the progress bar */
timer = gtk_timeout_add (100, progress_timeout, pdata);


gtk_widget_show (window);

gtk_main ();

return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮色兮凉城 2024-12-10 20:25:49

您需要将 gtk_timeout_add 调用更改为 g_timeout_add

gtk_timeout_add ()

guint               gtk_timeout_add                     (guint32 interval,
                                                         GtkFunction function,
                                                         gpointer data);

Warning

gtk_timeout_add has been deprecated since version 2.4 and should not be used in 
newly-written code. Use g_timeout_add() instead.

Google“gtk_timeout_add g_timeout_add”将给你举个例子,例如这个,http://gna.org/patch/?2563

You need to change the gtk_timeout_add call to g_timeout_add.

gtk_timeout_add ()

guint               gtk_timeout_add                     (guint32 interval,
                                                         GtkFunction function,
                                                         gpointer data);

Warning

gtk_timeout_add has been deprecated since version 2.4 and should not be used in 
newly-written code. Use g_timeout_add() instead.

Google "gtk_timeout_add g_timeout_add" will get you examples, e.g. this one, http://gna.org/patch/?2563.

晨曦慕雪 2024-12-10 20:25:49

正如 jesse 告诉您的,您正在使用 gtk_timeout_add,它在 GTK2 中已被弃用。 GTK2 中弃用的所有符号均在 GTK3 中删除。

要让您的程序在 GTK3 中运行,您需要确保不使用任何 GTK2 已弃用的符号。为此,使用 G_DISABLE_DEPRECATED(对于 GLib)、GTK_DISABLE_DEPRECATED 等符号. 这可以帮助您确保在使用 GTK2 进行编译时没有使用在 GTK3 中删除的符号。

还有一个 GTK2 到 GTK3 迁移指南您可以使用它,以及一些链接到 GNOME 中用于完成相同任务的补丁的 GNOME 目标,例如 GLibGTK 符号。

As jesse told you, you're using gtk_timeout_add, which has been deprecated in GTK2. All the symbols deprecated in GTK2 were removed in GTK3.

To have your program work in GTK3, you need to make sure you don't use any GTK2-deprecated symbols. For this, use symbols like G_DISABLE_DEPRECATED (for GLib), GTK_DISABLE_DEPRECATED, etc. that can help you make sure when you compile with GTK2 that you're not using a symbol that was removed in GTK3.

There is also a GTK2 to GTK3 migration guide that you can use, as well as some GNOME Goals that link to the patches that were used in GNOME to accomplish that same task, for GLib and GTK symbols.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文