macOS C++使用gtkmm GUI 快速开发原生桌面程序

安装环境

gtkmm依赖特别低,只需要brew下载几十M的lib就完成了开发环境依赖的安装,比安装动则20+G的xcode,简直是光速。

# 安装gtkmm
brew install gtkmm3

# 找到libffi配置文件
find /usr/local/Cellar -name libffi.pc
# 把libffi配置文件的目录加入PKG_CONFIG_PATH
export PKG_CONFIG_PATH=/usr/local/Cellar//libffi/3.2.1/lib/pkgconfig/

# 查看是否可以获取gtkmm编译参数
pkg-config gtkmm-3.0  --cflags --libs

编写测试代码

vim simple.cc

#include <gtkmm.h>

int main(int argc, char *argv[])
{
  auto app =
    Gtk::Application::create(argc, argv,
      "org.gtkmm.examples.base");

  Gtk::Window window;
  window.set_default_size(200, 200);

  return app->run(window);
}

编译

g++ -std=c++11  simple.cc -o simple `pkg-config gtkmm-3.0  --cflags --libs`

执行得到的文件

./simple

参考
https://developer.gnome.org/g...

编写helloworld

mkdir helloworld
cd helloworld

vim 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:
  void on_button_clicked();

  //Member widgets:
  Gtk::Button m_button;
};

#endif // GTKMM_EXAMPLE_HELLOWORLD_H

vim 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;
}

vim main.cc

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

int main (int argc, char *argv[])
{
  auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

  HelloWorld helloworld;

  //Shows the window and returns when it is closed.
  return app->run(helloworld);
}

编译

g++ -std=c++11 main.cc helloworld.h helloworld.cc   `pkg-config gtkmm-3.0  --cflags --libs`

运行./a.out

macOS C++使用gtkmm GUI 快速开发原生桌面程序

默认组件与自定义组件

gtkmm自带的各种组件(按钮、输入框等)具体看文档,gtkmm自定义组件文档: https://developer.gnome.org/g...

相关推荐