In the previous post, I have shown you how to integrate Aspose.Words for C++ in a Qt application using Qt Creator. In this post, we’ll check out how to set up and use Aspose.Words for C++ in Qt using Visual Studio. Later, we will create a Word document within the Qt application.
Set Up Aspose.Words for C++ in Qt Application
For Qt application development in Visual Studio, I have used the following Visual Studio, Qt and Qt VS Tools versions:
- Visual Studio 2017
- Qt 5.14.2
- Qt VS Tools for Visual Studio 2017
Once you have set up the required environment, you can proceed with the following steps for integrating Aspose’ Word library in the Qt application.
- Download and install CMake.
- Download and unpack Aspose.Words for C++.
- Create a new folder for your project files and copy/paste Aspose.Words.Cpp and CodePorting.Native.Cs2Cpp_vc14_20.3 folders from unpacked package.
- Create a new file named CMakeLists.txt in the same folder.
- Copy and paste the following content within the CMakeLists.txt file.
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
project(Qt_AsposeWords_CMake)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
find_package(CodePorting.Native.Cs2Cpp REQUIRED CONFIG PATHS ${CMAKE_CURRENT_SOURCE_DIR} NO_DEFAULT_PATH)
find_package(Aspose.Words.Cpp REQUIRED CONFIG PATHS ${CMAKE_CURRENT_SOURCE_DIR} NO_DEFAULT_PATH)
add_executable(Qt_AsposeWords_CMake
main.cpp
)
target_link_libraries(Qt_AsposeWords_CMake PRIVATE Qt5::Widgets Aspose::Words)
set_directory_properties(PROPERTIES VS_STARTUP_PROJECT Qt_AsposeWords_CMake)
file(TO_NATIVE_PATH "${Aspose.Words.Cpp_DIR}/lib/${CMAKE_VS_PLATFORM_NAME}" Aspose.Words.Cpp_DLL_PATH)
file(TO_NATIVE_PATH "${CodePorting.Native.Cs2Cpp_DIR}/lib" CodePorting.Native.Cs2Cpp_DLL_PATH)
file(TO_NATIVE_PATH "${Qt5_DIR}/../../../bin" Qt5_DLL_PATH)
set_target_properties(Qt_AsposeWords_CMake PROPERTIES VS_DEBUGGER_ENVIRONMENT "PATH=${Aspose.Words.Cpp_DLL_PATH}\\$<CONFIG>;${CodePorting.Native.Cs2Cpp_DLL_PATH};${Qt5_DLL_PATH};$(Path)")
- Open Command Prompt and cd to the project folder.
- Run the following command by replacing <path-to-qt5> with the path to Qt5Config.cmake file (i.e. C:\QT\5.14.2\msvc2017_64\lib\cmake\Qt5).
cmake -G "Visual Studio 15 2017" -Thost=x64 -Ax64 -S . -B build -D"Qt5_DIR=<path-to-qt5>"
Once you have completed the above-mentioned steps, a Visual Studio solution will be created in the folder. You are now ready to use Aspose.Words for C++ features within the Qt application. Lets now check out how to create a Word document in this Qt project.
Create Word Documents in Qt Application using Visual Studio
The following are the steps to create a Word document in the Qt application using Aspose.Words for C++:
- Open the solution in Visual Studio.
- Create a new .cpp file named main.cpp and include the following headers files in it.
#include <iostream>
#include <Aspose.Words.Cpp/Model/Document/Document.h>
#include <Aspose.Words.Cpp/Model/Document/DocumentBuilder.h>
- Copy/paste the following code in the main function:
auto doc = System::MakeObject<Aspose::Words::Document>();
auto builder = System::MakeObject<Aspose::Words::DocumentBuilder>(doc);
builder->Writeln(u"Hello World!");
doc->Save(u"HelloWorld.docx");
std::cout << "Word document has been created.";
QCoreApplication a(argc, argv);
return a.exec();
- Build and run the application.
Learn more about Aspose.Words for C++
Explore the documentation of Aspose.Words for C++ in order to learn how to create more complex and rich Word documents.