Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QmlRegisterType with shared library inside subdirs



  • Goodmorning,
    I'm using Qt 5.11 and I'm creating a project composed by two subprojects:

    • SysInfo, a singleton shared library with its implementation, SysInfoLinuxImpl.
    • Performance, a QML application.
      Basically, I want that SysInfoLinuxImpl Q_PROPERTIES to be visible in QML subProject.

    For this purpose, I tried to register SysinfoLinuxImpl with qmlRegisterType, but main.cpp doesn't recognize #include "SysInfoLinuxImpl.h" since it's contained in a different subdirs (the shared library) respect main.cpp (the QML application).

    Could you help me? Thank you in advance.
    Below some code.

    mainProject.pro

    TEMPLATE = subdirs
    
    SUBDIRS += \
        SysInfo \
        Performance
    
    unix:!macx: LIBS += -L$$OUT_PWD/SysInfo/ -lSysInfo
    INCLUDEPATH += $$PWD/SysInfo
    DEPENDPATH += $$PWD/SysInfo
    
    Performance.depends = SysInfo
    

    Sysinfo.h code

    #include "sysinfo_global.h"
    #include <QObject>
    
    class SYSINFOSHARED_EXPORT SysInfo
    {
    
    public:
        static	SysInfo&	instance();
        virtual	~SysInfo();
    
        virtual	void	init()	=	0;
        virtual	double	cpuLoadAverage()	=	0;
        virtual	double	memoryUsed()	=	0;
    
    protected:
        explicit	SysInfo();
    
    private:
        SysInfo(const SysInfo& rhs);
        SysInfo& operator= (const SysInfo& rhs);
    };
    

    SysInfoImpl.h

    #ifndef SYSINFOLINUXIMPL_H
    #define SYSINFOLINUXIMPL_H
    
    #include	<QtGlobal>
    #include	<QVector>
    #include    <QFile>
    #include    <QIODevice>
    #include    <QTimer>
    #include    <QThread>
    
    #include	"sysinfo.h"
    #include    "sysinfo_global.h"
    #include    "sysinfolinuxworker.h"
    
    #include    <QObject>
    
    class SYSINFOSHARED_EXPORT SysInfoLinuxImpl	:	public QObject, public SysInfo
    {
        Q_OBJECT
        Q_PROPERTY(double mCpuLoadAverage READ cpuLoadAverage)
        Q_PROPERTY(double mMemoryUsed READ memoryUsed)
    
    public:
        SysInfoLinuxImpl();
        void	init()	override;
        double	cpuLoadAverage()	override;
        double	memoryUsed()	override;
    
    // Calculation are done on a separate thread and get by these slots
    public slots:
        void memoryCalculationDone(double percent)  {mMemoryUsed = percent;}
        void cpuCalculationDone(double percent)     {mCpuLoadAverage = percent;}
    
    private:
        QVector<qulonglong>	cpuRawData();
    
    public:
        QVector<qulonglong>	mCpuLoadLastValues;
        double mCpuLoadAverage;
        double mMemoryUsed;
    
    private:
        QTimer mTimer;
        QThread mThread;
    };
    
    #endif // SYSINFOLINUXIMPL_H
    
    

  • Lifetime Qt Champion

    Hi,

    When having sub-projects with libraries. What I usually do is to create one .pri file per library that contains the INCLUDEPATH and LIBS statement necessary to use it and include that .pri file where needed in the other sub-projects.


  • Qt Champions 2017

    In addition to what @SGaist said, while compiling your main project, compiler is not able to find that appropriate header file. You need use INCLUDEPATH to set where are your header files. @SGaist has already given good suggestion on how to do this.



  • Hello @SGaist and @dheerendra ,
    thank you for your answers. I did as you said and the problem is solved.

    Thank you


Log in to reply