织梦CMS - 轻松建站从此开始!

罗索

选择图形界面库的烦恼

落鹤生 发布于 2011-03-19 23:27 点击:次 
在linux平台上的开发工具可说是百花齐放,可也同时造成各自为政割据一方的痛苦。图形界面开发更是如此,常用的就有gtk+, qt, wxWidgets, Tk, etc.且其中没有哪种能占据统治地位,成为事实上的标准。这对程序员来说是个很大的负担,毕竟全部精通是不可能的。
TAG:

在linux平台上的开发工具可说是百花齐放,可也同时造成各自为政割据一方的痛苦。图形界面开发更是如此,常用的就有gtk+, qt, wxWidgets, Tk, etc.且其中没有哪种能占据统治地位,成为事实上的标准。这对程序员来说是个很大的负担,毕竟全部精通是不可能的。
1 gtk+:
   基于LGPL协议,属于GNU所有,没有版权问题,常用于商业软件开发和嵌入式系统的开发。提供C语言的接口,其对应的C++语言包装为gtkmm

gtk+ example:

helloword.c

#include <gtk/gtk.h>

/* This is a callback function. The data arguments are ignored
 * in this example. More on callbacks below. */

static void hello( GtkWidget *widget,
                   gpointer data )
{
  g_print ("Hello World\n");
}

static gboolean delete_event( GtkWidget *widget,
                              GdkEvent *event,
                              gpointer data )
{
  /* If you return FALSE in the "delete_event" signal handler,
   * GTK will emit the "destroy" signal. Returning TRUE means
   * you don't want the window to be destroyed.
   * This is useful for popping up 'are you sure you want to quit?'
   * type dialogs. */


  g_print ("delete event occurred\n");

  /* Change TRUE to FALSE and the main window will be destroyed with
   * a "delete_event". */


  return TRUE;
}

/* Another callback */
static void destroy( GtkWidget *widget,
                     gpointer data )
{
  gtk_main_quit ();
}

int main( int argc,
          char *argv[] )
{
  /* GtkWidget is the storage type for widgets */
  GtkWidget *window;
  GtkWidget *button;
    
  /* This is called in all GTK applications. Arguments are parsed
   * from the command line and are returned to the application. */

  gtk_init (&argc, &argv);
    
  /* create a new window */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    
  /* When the window is given the "delete_event" signal (this is given
   * by the window manager, usually by the "close" option, or on the
   * titlebar), we ask it to call the delete_event () function
   * as defined above. The data passed to the callback
   * function is NULL and is ignored in the callback function. */

  g_signal_connect (G_OBJECT (window), "delete_event",
                    G_CALLBACK (delete_event), NULL);
    
  /* Here we connect the "destroy" event to a signal handler.
   * This event occurs when we call gtk_widget_destroy() on the window,
   * or if we return FALSE in the "delete_event" callback. */

  g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (destroy), NULL);
    
  /* Sets the border width of the window. */
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    
  /* Creates a new button with the label "Hello World". */
  button = gtk_button_new_with_label ("Hello World");
    
  /* When the button receives the "clicked" signal, it will call the
   * function hello() passing it NULL as its argument. The hello()
   * function is defined above. */

  g_signal_connect (G_OBJECT (button), "clicked",
                    G_CALLBACK (hello), NULL);
    
  /* This will cause the window to be destroyed by calling
   * gtk_widget_destroy(window) when "clicked". Again, the destroy
   * signal could come from here, or the window manager. */

  g_signal_connect_swapped (G_OBJECT (button), "clicked",
                            G_CALLBACK (gtk_widget_destroy),
                            G_OBJECT (window));
    
  /* This packs the button into the window (a gtk container). */
  gtk_container_add (GTK_CONTAINER (window), button);
    
  /* The final step is to display this newly created widget. */
  gtk_widget_show (button);
    
  /* and the window */
  gtk_widget_show (window);
    
  /* All GTK applications must have a gtk_main(). Control ends here
   * and waits for an event to occur (like a key press or
   * mouse event). */

  gtk_main ();
    
  return 0;
}

 

编译:

 

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

 

 gtkmm example:

 

helloworld.h

 


#ifndef GTKMM_EXAMPLE_HELLOWORLD_H
#define GTKMM_EXAMPLE_HELLOWORLD_H

#include <gtkmm/button.h>
#include <gtkmm/window.h>

class HelloWorld : public Gtk::Window
{

public:
  HelloWorld();
  virtual ~HelloWorld();

protected:
  //Signal handlers:

  virtual void on_button_clicked();

  //Member widgets:

  Gtk::Button m_button;
};

#endif // GTKMM_EXAMPLE_HELLOWORLD_H

helloworld.cc

#include "helloworld.h"
#include <iostream>

HelloWorld::HelloWorld()
: m_button("Hello World") // creates a new button with label "Hello World".

{
  // Sets the border width of the window.

  set_border_width(10);

  // When the button receives the "clicked" signal, it will call the

  // on_button_clicked() method defined below.

  m_button.signal_clicked().connect(sigc::mem_fun(*this,
              &HelloWorld::on_button_clicked));

  // This packs the button into the Window (a container).

  add(m_button);

  // The final step is to display this newly created widget...

  m_button.show();
}

HelloWorld::~HelloWorld()
{
}

void HelloWorld::on_button_clicked()
{
  std::cout << "Hello World" << std::endl;
}

main.cc 

#include <gtkmm/main.h>
#include "helloworld.h"

int main (int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);
  HelloWorld helloworld;
  //Shows the window and returns when it is closed.
  Gtk::Main::run(helloworld);
  return 0;
}

编译:

$ g++ `pkg-config --cflags --libs gtkmm-2.4` *cc *.h -o helloworld

2 qt

example:

#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
        QApplication app(argc, argv);
        QLabel *label = new QLabel("Hello Qt!");
        label->show();
        return app.exec();
}

编译:

$ qmake -project #generate project: helloworld.pro

$ qmake helloworld.pro

 

3 wxWidgets

example:

hworld.cpp

#include "wx/wx.h"

// 定义应用程序类
class MyApp : public wxApp
{
public:
    // 这个函数将会在程序启动的时候被调用
    virtual bool OnInit();
};

// 定义主窗口类
class MyFrame : public wxFrame
{
public:
    // 主窗口类的构造函数
    MyFrame(const wxString& title);

    // 事件处理函数
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);

private:
    // 声明事件表
    DECLARE_EVENT_TABLE()
};

// 有了这一行就可以使用 MyApp& wxGetApp()了
DECLARE_APP(MyApp)
// 告诉wxWidgets主应用程序是哪个类
IMPLEMENT_APP(MyApp)
// 初始化程序
bool MyApp::OnInit()
{
    // 创建主窗口
    MyFrame *frame = new MyFrame(wxT("Minimal wxWidgets App"));
    // 显示主窗口
    frame->Show(true);
    // 开始事件处理循环
    return true;
}

// MyFrame类的事件表
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
    EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
END_EVENT_TABLE()

void MyFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg;
    msg.Printf(wxT("Hello and welcome to %s"),wxVERSION_STRING);
    wxMessageBox(msg, wxT("About Minimal"),wxOK | wxICON_INFORMATION, this);
}

void MyFrame::OnQuit(wxCommandEvent& event)
{
    // 释放主窗口
    Close();
}


//#include "mondrian.xpm"
MyFrame::MyFrame(const wxString& title): wxFrame(NULL, wxID_ANY, title)
{
// 设置窗口图标
// SetIcon(wxIcon(mondrian_xpm));
// 创建菜单条
    wxMenu *fileMenu = new wxMenu;
    // 添加“关于”菜单项
    wxMenu *helpMenu = new wxMenu;
    helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),wxT("Show about dialog"));
    fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"),wxT("Quit this program"));
    // 将菜单项添加到菜单条中
    wxMenuBar *menuBar = new wxMenuBar();
    menuBar->Append(fileMenu, wxT("&File"));
    menuBar->Append(helpMenu, wxT("&Help"));

    // ...然后将菜单条放置在主窗口上
    SetMenuBar(menuBar);

    // 创建一个状态条来让一切更有趣些。
    CreateStatusBar(2);
    SetStatusText(wxT("Welcome to wxWidgets!"));
}

编译:

$ g++ hworld.cpp `wx-config --libs` `wx-config --cxxflags` -o hworld


(thinkfree)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201103/11095.html]
本文出处:thinkfree.cublog.cn 作者:thinkfree
顶一下
(1)
25%
踩一下
(3)
75%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容