Source Files
All the source code will be located here in the source directory. In other word, only files in this directory should be pushed into your version control. You can skip this session if you only interested in building the tutorial project.
Below are the files in this tutorial project.
SimpleApp
|-CMakeLists.txt
|-main.cpp
|-mainwindow.h
|-Resources.qrc
|-SimpleQml.qml
|-mainwindow.cpp
|-mainwindow.ui
Let observe the CMakeList.txt file.
CMAKE_MINIMUM_REQUIRED(VERSION
2.8.11)
PROJECT(SimpleApp)
SET(CMAKE_INCLUDE_CURRENT_DIR
ON)
SET(ARCH_32BIT
${CMAKE_SIZEOF_VOID_P}
EQUAL
4)
SET(ARCH_64BIT
${CMAKE_SIZEOF_VOID_P}
EQUAL
8)
|
The architecture of complier can be checked by reading the size of void pointer.
The code below manages QT lib. It will check the current build environment, and store that value to QT_INSTALLED_PATH. That path is the path that you were asked by QT's installer wizard. Note that the WIN32 refer to both 32 bit and 64 bit of Windows.
#
=================================
#
Setup QT
#
=================================
IF(
WIN32
AND
${ARCH_32BIT})
SET(QT_INSTALLED_PATH
"C:/QtMSVCX86/Qt5.5.0/5.5/msvc2013"
)
ELSEIF(WIN32
AND
${ARCH_64BIT})
SET(QT_INSTALLED_PATH
"C:/QtMSVCX64/Qt5.5.0/5.5/msvc2013"
)
ELSEIF(UNIX
AND
NOT
MINGW AND
${ARCH_32BIT})
SET(QT_INSTALLED_PATH
"/opt/Qt5.5.0/5.5/gcc/"
)
ELSEIF(UNIX
AND
NOT
MINGW AND
${ARCH_64BIT})
SET(QT_INSTALLED_PATH
"/opt/Qt5.5.0/5.5/gcc_64/"
)
ENDIF()
SET(CMAKE_AUTOMOC
ON)
SET(CMAKE_AUTOUIC
ON)
SET(CMAKE_AUTORCC
ON)
FIND_PACKAGE(Qt5Widgets
PATHS
${QT_INSTALLED_PATH})
FIND_PACKAGE(Qt5Qml
PATHS
${QT_INSTALLED_PATH})
FIND_PACKAGE(Qt5Quick
PATHS
${QT_INSTALLED_PATH})
|
The QT’s MOC, UIC and RCC are handled automatically by CMAKE_AUTOMOC, CMAKE_AUTOUIC and CMAKE_AUTORCC. The QT’s packages that were included are Qt5Widgets, Qt5QML and Qt5Quck. You can add other package such as Qt5Network here.
#
============================
#
Setup Boost
#
============================
#
Add search with prefix "lib". Reason:
#
On windows, b2 generate lib with lib prefix
#
such as libboost_signals-vc120-s-1_59.lib
SET(CMAKE_FIND_LIBRARY_PREFIXES
lib)
#
Disable Boost_NO_BOOST_CMAKE.
#
Reason: This is to remove
#
"BOOST_DIR not found" in INSTALL build.
SET(Boost_NO_BOOST_CMAKE
ON)
#
Do not use system boost ie. Boost from
#
apt-get
SET(Boost_NO_SYSTEM_PATHS
ON)
IF(
WIN32
AND
${ARCH_32BIT})
SET(BOOST_INSTALLED_PATH
C:/Boost)
ELSEIF(WIN32
AND
${ARCH_64BIT})
SET(BOOST_INSTALLED_PATH
C:/Boost64)
ELSEIF(UNIX
AND
NOT
MINGW AND
${ARCH_32BIT})
SET(BOOST_INSTALLED_PATH
/opt/boost)
ELSEIF(UNIX
AND
NOT
MINGW AND
${ARCH_64BIT})
SET(BOOST_INSTALLED_PATH
/opt/boost64)
ENDIF()
SET(BOOST_ROOT
${BOOST_INSTALLED_PATH})
#
Boost 1.59 built into /include/boost-version/
SET(BOOST_INCLUDEDIR
${BOOST_INSTALLED_PATH}/include/boost-1_59)
SET(BOOST_USE_STATIC_LIBS
ON)
SET(BOOST_USE_MULTITHREADED
ON)
#
Use default runtime-link (thing like vc redist ..)
SET(BOOST_USE_STATIC_RUNTIME
OFF)
FIND_PACKAGE(Boost
1.57.0 COMPONENTS
signals
system
thread
date_time
chrono
REQUIRED)
|
Similar to that of QT, the code is very straight forward. Make sure that you have installed boost to the BOOST_INSTALLED_PATH path by reviewing b2’s --prefix.
The code below will compile and link the source codes to the exe file.
#
=========================
#
Linker
#
=========================
ADD_EXECUTABLE(
SimpleApp
main.cpp
mainwindow.cpp
Resources.qrc)
#
Use C++11 flag on C++ compiler.
#
For most compliers ,
#
The auto is a feature that require C++11 flag
TARGET_COMPILE_FEATURES(
SimpleApp
PUBLIC cxx_auto_type)
#
Add boost include directory
TARGET_INCLUDE_DIRECTORIES(
SimpleApp
PUBLIC
${BOOST_INCLUDEDIR})
TARGET_LINK_LIBRARIES(SimpleApp
Qt5::Widgets
Qt5::Qml
Qt5::Quick
${Boost_LIBRARIES}
)
#
ptherad is required for linux
IF(UNIX
AND
NOT
MINGW)
TARGET_LINK_LIBRARIES(SimpleApp
-lpthread )
ENDIF()
|
Below are the codes in mainwindows.cpp and .h file
#ifndef
MAINWINDOW_H
#define
MAINWINDOW_H
#include
<QMainWindow>
#include
<QtQuick/QQuickView>
#include
<boost/signals2/signal.hpp>
#include
<memory>
namespace
Ui {
class
MainWindow;
}
class
MainWindow : public
QMainWindow
{
Q_OBJECT
public:
explicit
MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void
OnQmlSignaled();
private:
std::unique_ptr<Ui::MainWindow>
m_Ui;
std::unique_ptr<QQuickView>
m_QmlView;
boost::signals2::signal<void()>
m_BoostSignal;
};
#endif // MAINWINDOW_H |
#include
<QtQuick/QQuickView>
#include
<QQuickItem>
#define
BOOST_THREAD_PROVIDES_FUTURE
#include
<boost/thread.hpp>
#include
<boost/thread/future.hpp>
#include
"mainwindow.h"
#include
"ui_mainwindow.h"
MainWindow::MainWindow(QWidget
*parent) :
QMainWindow(parent),
m_Ui(new
Ui::MainWindow),
m_QmlView(new
QQuickView)
{
\\..
Code skipped ..
m_BoostSignal.connect(
[this](){
boost::future<QString>
f =
boost::async(
[](){return
QString("Hello
from boost future");}
);
this->m_Ui->plainTextEdit->appendPlainText(f.get());
}
);
connect(m_Ui->pushButton,
&QPushButton::clicked,
[this](){
m_BoostSignal();
});
}
void
MainWindow::OnQmlSignaled()
{
m_BoostSignal();
}
MainWindow::~MainWindow()
= default;
|
No comments:
Post a Comment