基于qt creator,camke使用boost库中的signals2实现订阅者发布者模式

                                                                        心 火!


几个要点先罗列如下:

1)  可以直接使用boost的源文件,不用编译。

2)在CMakeLists.txt文件中指出boost源文件的所在位置,然后就可以直接在自己的代码中include boost的源文件。   另外有一种直接使用cmake的find_package功能的方法,见文末。

3)学习boost库的使用方法,一定要参照正规book或者官网文档,切莫尝试从别人blog上系统学习,blog只能起到启发作用。

 

我的环境:

Qt creator版本:4.6.2, Camke 版本:3.12.1 , Ubuntu: 14.04

Step1:

下载boost库源文件:https://www.boost.org/users/download/ ,存储到本地目录。boost目录中存储着hpp源文件。

Step2:

使用qt creator新建一个plain c++ project,选择编译方式时,选择cmake。

在CMakeLists.txt文件中指出boost源文件的所在位置:

if(NOT DEFINED BOOST_PATH)

        set(BOOST_PATH  /home/xx /boost_1_70_0) #boost文件夹在此目录下面

endif(NOT DEFINED BOOST_PATH)

include_directories( ${BOOST_PATH} )

Step3

在自己的文件中,可以直接include boost源文件了:例如:

#include <boost/signals2/signal.hpp>

#include <boost/random.hpp>

using namespace boost::signals2;

using namespace boost;


举个例子:

来自《Boost程序库完全开发指南》 罗剑锋,chapter 11,signals2部分:

class.h:

#ifndef CLASSES_H_
#define CLASSES_H_

#include <boost/signals2/signal.hpp>
//#include <boost/signals2.hpp>
#include <boost/random.hpp>

using namespace std;
using namespace boost::signals2;
using namespace boost;

class nurse
{
public:
    nurse(string name)
    {
        _name = name;
    }
    nurse(){}

    void action(string str1,string str2)
    {
        cout<< _name << "wake up and to open the door\n";
    }
private:
    string _name;
};


class baby
{
public:
    baby(string name){_name = name;}
    baby(){}

    void action(string str1,string str2)
    {
        cout<< _name << "wake up and crying ....... \n";
    }
private:
    string _name;
};


class ring
{
public:
    ring(){}
    typedef signal<void(string str1,string str2)> signal_t;
    typedef signal_t::slot_type slot_t;

    connection connect(const slot_t& s)
    {
        return _alarm.connect(s);
    }
    void Press(string str1,string str2)
    {
        cout << "Ring alrams ......\n";
        _alarm(str1,str2);
    }
private:
    signal_t _alarm;
};

#endif

main.cpp

#include <iostream>

#include <boost/signals2/signal.hpp>
//#include <boost/signals2.hpp>
#include <boost/bind/bind.hpp>

#include "classes.h"

using namespace std;
using namespace boost::signals2;
using namespace boost;


int main()
{
    cout << "Hello World!" << endl;

    ring r;
    nurse n1("Nurse1 Miss CaoHong");
    nurse n2("Nurse2 Miss YuLin");
    baby b1("Baby1 GuoGuo");
    baby b2("Baby2 NiuNiu");

    r.connect(boost::bind(&nurse::action,n1,_2,_1));
    r.connect(boost::bind(&nurse::action,n2,_2,_1));
    r.connect(boost::bind(&baby::action,b1,_1,_2));
    r.connect(boost::bind(&baby::action,b2,_1,_2));

    r.Press("Frank F","Bluce Lee");

}


CMakeLists.txt

cmake_minimum_required(VERSION 3.12)

project(TryBoostWithCmake)

###########################################

###
set(CMAKE_CXX_STANDARD 11)


if(NOT DEFINED BOOST_PATH)
    set(BOOST_PATH /home/xxxxxxxxx/downLoaedeBoost/boost_1_70_0)
endif(NOT DEFINED BOOST_PATH)

# must add the following part to tell cmake where to find
# included files, otherwise,
#  #include "xxx.hpp" in your own file
# and #include <boost/noncopyable.hpp> in original boost file
# will not find the hpp file.
#include_directories(${BOOST_PATH}
#                      )

##need to study it###
set(BOOST_ROOT /home/xxxxx/downLoaedeBoost/boost_1_70_0)
find_package(Boost
             )
if(NOT Boost_FOUND)
    message("Not found Boost lalalalalaalal------------------------")
endif()

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})

    MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
    MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")
    MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")
endif()
###

#message("========================")
#message("${CMAKE_MODULE_PATH}")
###


set(
    INC ${BOOST_PATH}/boost/signals2/signal.hpp
    "classes.h"
    #CRB_dev.h
    )
set(SRC
    "main.cpp"
  )
####################################

add_executable(${PROJECT_NAME} ${SRC}
               ${INC})

set( CMAKE_BUILD_TYPE Debug )#fg add

其他:

1.为什么boost.signals2是线程安全的,官网说明: https://www.boost.org/doc/libs/1_70_0/doc/html/signals2/thread-safety.html

2.使用cmake的find_package功能找到boost的源文件

参考了: https://www.jianshu.com/p/1827cd86d576https://www.jianshu.com/p/1827cd86d576  , https://blog.csdn.net/haluoluo211/article/details/80559341

##using camke find package###

set(BOOST_ROOT /home/xxx/boost_1_70_0) # tell cmake where to find your boost  files

find_package(Boost   )

if(NOT Boost_FOUND)

    message("Not found Boost lalalalalaalal------------------------")

endif()

if(Boost_FOUND)

    include_directories(${Boost_INCLUDE_DIRS})#cmake将自动找到的头文件目标进行include

    MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")

    MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")#可能编译过的boost才有效

    MESSAGE( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")

endif()

###


Ref:

《Boost程序库完全开发指南》 罗剑锋,第四版

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Linux中使用Qt Creator实现2048游戏,你需要遵循以下步骤: 1. 安装Qt Creator 如果你还没有安装Qt Creator,请根据你的Linux发行版的说明进行安装。例如,如果你使用的是Ubuntu,则可以在命令行中输入以下命令来安装Qt Creator: ``` sudo apt-get install qtcreator ``` 2. 创建Qt项目 在Qt Creator中创建一个新项目,选择Qt Widgets Application并命名为2048。然后选择一个存储项目的位置并单击下一步。 3. 创建2048游戏界面 在Qt Creator中,你可以使用拖放功能来创建2048游戏的界面。选择“设计”选项卡,在左侧工具箱中选择widget,然后在右侧编辑器中绘制2048游戏的界面。 4. 实现游戏逻辑 在Qt Creator中,你可以使用C++编写游戏逻辑。打开2048项目的源代码文件,并在其中实现游戏逻辑。你需要实现游戏的移动、合并和分数计算等功能。 5. 调试和测试 在Qt Creator中,你可以使用调试器来调试你的代码。单击F5键启动调试器,然后使用游戏界面测试你的代码。 6. 编译和构建 一旦你完成了2048游戏的代码编写和调试,你可以使用Qt Creator中的构建工具来构建你的应用程序。在Qt Creator中,选择“构建”选项卡,然后单击“构建项目”按钮。 7. 运行应用程序 一旦你构建了你的应用程序,你可以在Qt Creator中直接运行它。在Qt Creator中,选择“运行”选项卡,然后单击“运行”按钮。你的2048游戏应该会在Qt Creator中启动。 这些就是在Linux中使用Qt Creator实现2048游戏的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

First Snowflakes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值