Adding a function

After the library project is created, you can add functions to export services to applications.

To add a function:
  1. Click the Edit icon on the left side, right-click the QtLibrary folder in the Projects view, then choose Add New... in the popup menu.
  2. In the New File dialog, select C++ in the Files and Classes list, then C++ Class in the list of file types (shown in the middle), then click Choose...
  3. In the Details page of the C++ Class Wizard dialog, name the class Foo, then click Next.
  4. In the Summary page, click Finish.

    Qt Creator creates two new files, foo.h and foo.cpp, and adds them to the project.

  5. Open foo.h for editing (by double-clicking its entry in the Project view), and add this content to the file:
    #ifndef FOO_H
    #define FOO_H
    
    #include <QString>
    
    class Foo
    {
    public:
        Foo();
        QString message() const;
    };
    
    #endif // FOO_H
    
    Note: The message() function is declared in the public part of the class so it's visible to application code outside of the library.
  6. After saving the header file, edit foo.cpp to add this content:
    #include "foo.h"
    
    Foo::Foo()
    {
    }
    
    QString Foo::message() const
    {
        return QStringLiteral("QtLibrary says hello world");
    }
    
    Note: We define the most basic function that simply returns a string to its caller, just to illustrate the mechanism for implementing library functionality. You'll write functions that do more useful actions but the method of defining them in library projects is always the same.
You can now build the library into an .so file containing the defined functionality.