Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QmlRegisterType with shared library inside subdirs

QmlRegisterType with shared library inside subdirs

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 852 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davidino
    wrote on last edited by
    #1

    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
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        1
        • D Offline
          D Offline
          davidino
          wrote on last edited by
          #4

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

          Thank you

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved