Create Word Document in Qt Application

Qt is a popular cross-platform application development framework to develop a variety of applications for desktop, mobile, and embedded systems. C++ based Qt framework uses MOC (Meta-Object Compiler) to generate a standard C++ source from the Qt-extended C++. Since Qt supports a wide range of applications that may target various scenarios, integration of Word automation features (i.e. generating Word documents, Word to PDF, and etc.) within the Qt applications has a big scope.

Aspose.Words for C++ provides nearly all basic and advanced Word automation features and it can positively serve the Word processing requirements within the Qt applications. So let’s see how you can integrate and utilize our C++ Word library to create Word documents within Qt applications.

Integrate Aspose.Words for C++ within Qt Application

In order to integrate Aspose.Words for C++, you are required to download and extract the library files from the downloads section. Once you have, you can proceed with one of the following options to develop Qt applications:

In this blog post, I’ll show you how to integrate and use Aspose.Words for C++ library within a Qt Console Application developed in Qt Creator.

Steps to Add Word Library in Qt Application

I assume that you have properly set up the Qt development environment and installed Qt Creator. Once everything is up and running, you can follow the below steps:

  • Open Qt Creator and create a new Qt Console Application.
Create Word in Qt Application
  • Select the QMake option from the Build System dropdown.
select QMake build
  • Select the appropriate kit and finish the wizard.
  • Copy Aspose.Words.Cpp and CodePorting.Native.Cs2Cpp_vc14_20.3 folders from the extracted package of Aspose.Words for C++ into the root of the project.
Add Word library files
  • In order to add paths to lib and include folders, right-click on the project in LHS panel and select Add Library.
Add Word Library in Qt
  • Select the External Library option and browse paths to include and lib folders one by one.
Add external library in Qt
  • Once done, your .pro project file will contain the following entries:
Qt Project File
  • Build the application and you are done with the integration.

Generate a Word Document in Qt Application

Now you are ready to use Aspose.Words for C++ features. Let’s see how to generate a simple Word document in a Qt application. The following are the steps along with the code snippets required to create a Word document.

  • Include the following header files in main.cpp.
#include <Aspose.Words.Cpp/Model/Document/Document.h>
#include <Aspose.Words.Cpp/Model/Document/DocumentBuilder.h>
#include <iostream>
  • Insert the code to generate a Word DOCX document inside the main function.
int main(int argc, char *argv[])
{
    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<<"Done..";

    QCoreApplication a(argc, argv);
    return a.exec();
}
  • Build and run the application.

Learn more about Aspose.Words for C++

Aspose.Words for C++ also lets you create more complex and rich Word documents. Furthermore, you can perform various document conversions within the Qt applications. Learn more about Aspose.Words for C++ using the documentation.

Share your feedback

In case you would have any questions or queries regarding Aspose.Words for C++, feel free to post on our forum.