Linux / C++有关glade3 和gtkmm 的帮助

发布于 2024-09-07 04:44:09 字数 5705 浏览 12 评论 0原文

下面是一个 C 应用程序源代码,它使用 Glade3 和 GTK2+ 创建 GUI:

// gcc -o simple simple.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

GtkBuilder *builder;
GtkWidget  *window1;

G_MODULE_EXPORT void on_window1_destroy (GtkObject *object, gpointer user_data)
{
    gtk_main_quit();
}

G_MODULE_EXPORT void on_button2_clicked (GtkObject *object, gpointer user_data)
{
    gtk_main_quit();
}

G_MODULE_EXPORT void on_button1_clicked (GtkObject *object, gpointer user_data)
{
    const gchar *name;
    GtkWidget *name_entry = GTK_WIDGET(gtk_builder_get_object(builder, "entry1"));
    name = gtk_entry_get_text(GTK_ENTRY(name_entry));
    g_print("Name is: %s\n", name);
}

int main(int argc, char** argv)
{
    GError     *error = NULL;

    /* Init GTK+ */
    gtk_init( &argc, &argv );

    /* Create new GtkBuilder object */
    builder = gtk_builder_new();
    /* Load UI from file. If error occurs, report it and quit application.*/
    if( ! gtk_builder_add_from_file( builder, "simple.glade", &error ) )
    {
        g_warning( "%s", error->message );
        g_free( error );
        return( 1 );
    }

    /* Get main window pointer from UI */
    window1 = GTK_WIDGET( gtk_builder_get_object( builder, "window1" ) );

    /* Connect signals */
    gtk_builder_connect_signals( builder, NULL );

    /* Destroy builder, since we don't need it anymore */
    //g_object_unref( G_OBJECT( builder ) );

    /* Show window. All other widgets are automatically shown by GtkBuilder */
    gtk_widget_show( window1 );

    /* Start main loop */
    gtk_main();

    return( 0 );
}

和 Glade 文件:

<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="window1">
    <signal name="destroy" handler="on_window1_destroy"/>
    <child>
      <object class="GtkTable" id="table1">
        <property name="visible">True</property>
        <property name="n_rows">2</property>
        <property name="n_columns">2</property>
        <child>
          <object class="GtkHButtonBox" id="hbuttonbox1">
            <property name="visible">True</property>
            <child>
              <object class="GtkButton" id="button1">
                <property name="label" translatable="yes">OK</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal name="clicked" handler="on_button1_clicked"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="top_attach">1</property>
            <property name="bottom_attach">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkHButtonBox" id="hbuttonbox2">
            <property name="visible">True</property>
            <child>
              <object class="GtkButton" id="button2">
                <property name="label" translatable="yes">Exit</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal name="clicked" handler="on_button2_clicked"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="right_attach">2</property>
            <property name="top_attach">1</property>
            <property name="bottom_attach">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="label" translatable="yes">Enter your name:</property>
          </object>
        </child>
        <child>
          <object class="GtkEntry" id="entry1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="invisible_char">&#x25CF;</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="right_attach">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

现在我想用 C++ 编写这个应用程序。谁能给出使用 gtkmm 的示例或教程吗?或者更好地为我转换这个简单的例子?提前致谢!

Here is a C application source code, which creates GUI using Glade3 and GTK2+:

// gcc -o simple simple.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

GtkBuilder *builder;
GtkWidget  *window1;

G_MODULE_EXPORT void on_window1_destroy (GtkObject *object, gpointer user_data)
{
    gtk_main_quit();
}

G_MODULE_EXPORT void on_button2_clicked (GtkObject *object, gpointer user_data)
{
    gtk_main_quit();
}

G_MODULE_EXPORT void on_button1_clicked (GtkObject *object, gpointer user_data)
{
    const gchar *name;
    GtkWidget *name_entry = GTK_WIDGET(gtk_builder_get_object(builder, "entry1"));
    name = gtk_entry_get_text(GTK_ENTRY(name_entry));
    g_print("Name is: %s\n", name);
}

int main(int argc, char** argv)
{
    GError     *error = NULL;

    /* Init GTK+ */
    gtk_init( &argc, &argv );

    /* Create new GtkBuilder object */
    builder = gtk_builder_new();
    /* Load UI from file. If error occurs, report it and quit application.*/
    if( ! gtk_builder_add_from_file( builder, "simple.glade", &error ) )
    {
        g_warning( "%s", error->message );
        g_free( error );
        return( 1 );
    }

    /* Get main window pointer from UI */
    window1 = GTK_WIDGET( gtk_builder_get_object( builder, "window1" ) );

    /* Connect signals */
    gtk_builder_connect_signals( builder, NULL );

    /* Destroy builder, since we don't need it anymore */
    //g_object_unref( G_OBJECT( builder ) );

    /* Show window. All other widgets are automatically shown by GtkBuilder */
    gtk_widget_show( window1 );

    /* Start main loop */
    gtk_main();

    return( 0 );
}

and the glade file:

<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="window1">
    <signal name="destroy" handler="on_window1_destroy"/>
    <child>
      <object class="GtkTable" id="table1">
        <property name="visible">True</property>
        <property name="n_rows">2</property>
        <property name="n_columns">2</property>
        <child>
          <object class="GtkHButtonBox" id="hbuttonbox1">
            <property name="visible">True</property>
            <child>
              <object class="GtkButton" id="button1">
                <property name="label" translatable="yes">OK</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal name="clicked" handler="on_button1_clicked"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="top_attach">1</property>
            <property name="bottom_attach">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkHButtonBox" id="hbuttonbox2">
            <property name="visible">True</property>
            <child>
              <object class="GtkButton" id="button2">
                <property name="label" translatable="yes">Exit</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal name="clicked" handler="on_button2_clicked"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="right_attach">2</property>
            <property name="top_attach">1</property>
            <property name="bottom_attach">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="label" translatable="yes">Enter your name:</property>
          </object>
        </child>
        <child>
          <object class="GtkEntry" id="entry1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="invisible_char">●</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="right_attach">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Now I want to code this application in C++. Can anyone give an example or tutorial using gtkmm? Or better convert this simple example for me? Thanks in advance!

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

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

发布评论

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

评论(1

明媚如初 2024-09-14 04:44:09

我建议您阅读 gtkmm 教程

我可以费心将其全部转换,但它看起来像这样:

void button1_clicked()
{
  ...
}

int main(int argc, char argv)
{
  Gtk::Main kit(argc, argv);

  Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("my_gui.ui");

  Gtk::Window *window1 = 0;
  builder->get_widget("window1", window1);
  Gtk::Button *button1 = 0;
  builder->get_widget("button1", button1);
  // get other widgets
  ...

  button1->signal_clicked().connect(sigc::mem_fun(*this, &button1_clicked));

  // connect more signals
  ...

  Gtk::Main::run(*m_main_window);

  return 0;
}

请注意,在使用 gtkmm 时,您不能使用 Glade 连接信号,您需要手动执行此操作。

I suggest that you read the gtkmm tutorial.

I can bother to convert it all but it would look something like this:

void button1_clicked()
{
  ...
}

int main(int argc, char argv)
{
  Gtk::Main kit(argc, argv);

  Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("my_gui.ui");

  Gtk::Window *window1 = 0;
  builder->get_widget("window1", window1);
  Gtk::Button *button1 = 0;
  builder->get_widget("button1", button1);
  // get other widgets
  ...

  button1->signal_clicked().connect(sigc::mem_fun(*this, &button1_clicked));

  // connect more signals
  ...

  Gtk::Main::run(*m_main_window);

  return 0;
}

Note that you cannot use Glade to connect your signals when using gtkmm, you need to do that manually.

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