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

发布时间:2019-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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...

脚本宝典总结

以上是脚本宝典为你收集整理的macOS C++使用gtkmm GUI 快速开发原生桌面程序全部内容,希望文章能够帮你解决macOS C++使用gtkmm GUI 快速开发原生桌面程序所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。