2009-10-28 196 views
1

我正在用C++編寫一個使用GTK +(不是gtkmm)的應用程序,因此我需要使用g ++編譯器進行編譯。是否可以用g ++編譯器編譯GTK +應用程序? GTK +和庫與g ++編譯器兼容嗎?使用G ++編譯器編譯GTK +應用程序

我試圖嵌入GTK +函數調用的類象如下:

#include <gtk/gtk.h> 
class LoginWindow 
{ 
    public: 
    LoginWindow(); 
    void on_window_destroy(GtkObject *object, gpointer user_data); 

    private: 

    GtkBuilder  *builder; 
    GtkWidget  *window; 
}; 

LoginWindow::LoginWindow() 
{ 
    builder = gtk_builder_new(); 
    gtk_builder_add_from_file (builder, "login_window.glade", NULL); 
    window = GTK_WIDGET (gtk_builder_get_object (builder, "login_window")); 
    gtk_builder_connect_signals (builder, NULL); 

    g_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(on_window_destroy), NULL); 
    g_object_unref (G_OBJECT (builder)); 
    gtk_widget_show (window); 
} 

void LoginWindow::on_window_destroy (GtkObject *object, gpointer user_data) 
{ 
    gtk_main_quit(); 
} 

int main (int argc, char *argv[]) 
{ 
    gtk_init (&argc, &argv); 

    LoginWindow loginWindow; 

    gtk_main(); 
    return 0; 
} 

我這樣做對嗎?我得到編譯錯誤在線:

g_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(on_window_destroy), NULL); 

login_window.cpp: In constructor "LoginWindow::LoginWindow()": 
login_window.cpp:27: error: invalid use of member (did you forget the "&" ?) 

這樣做的正確方法是什麼?

回答

4

Gtk +是一個基於C的庫,所以你不能把它作爲類成員函數作爲回調。可以將C風格函數定義爲回調,即

extern "C" 
void on_window_destroy(GtkObject *object, gpointer user_data) 
{ 
    // do yer stuff.. keep in mind, there's no this-pointer here, it's not 
    // associated with an object 
} 

任何類聲明之外。登記時的信號,你可能想在登錄窗口對象自身添加爲用戶 - 數據並輸入時的方法和 前進的號召,像簡單地投這麼

extern "C" 
void on_window_destroy(GtkObject *object, gpointer user_data) 
{ 
    static_cast<LoginWindow*>(user_data)->on_window_destroy(); 
} 

如果你想這樣做ç雖然++風格,看看gtkmm,它是Gtk +的C++包裝器,它會爲您提供更多的OOP。在gtkmm中處理信號/回調有點麻煩,但它是安全的,最終會爲您節省很多麻煩。