Search code examples
c++qtqmakeqtcoreqtplugin

Qt subdirs incude classes


I'm follow Qt Echo Plugin example and trying to write complex application. My project have following structure:

MainDir \
    Main.pro
    kernel \
        kernel.pro
        abstractinterface.h
        main.cpp
    testplugin \
        testplugin.pro
        abstractplugin.h
        abstractplugin.cpp

Problem is in plugin header file:

#include <QObject>
#include <QtPlugin>
#include "abstractinterface.h"

class AbstractPlugin : public QObject, AbstractInterface
// An error appears here
// expected class-name before '{' token
{
    Q_OBJECT
    //... plugin initialization code ...
public:
    explicit AbstractPlugin(QObject *parent = 0);
};

Also, autocompletion can't find class AbstractInterface.

So, question is: what I'm doing wrong? In testplugin.pro file I have line INCLUDEPATH += ../kernel/.

Any help appreciated.

---- EDIT -----
abstractinterface.h

#include <QtPlugin>

#define INTERFACE_ID "AbstractInterface/1.0"

class AbstractInterface
{
public:
    virtual ~AbstractInterface();


    virtual void init();
    virtual void enable();
    virtual void disable();
};

Q_DECLARE_INTERFACE(AbstractInterface, INTERFACE_ID)

Solution

  • Given that your pasted files look correct and work here, I am leaning towards that your problem is this line:

    INCLUDEPATH += ../kernel/
    

    You likely execute qmake from the project root where your main project file resides calling qmake recursively to generate the Makefiles. However, at the point of generation, the aforementioned path will extend from the project root rather than from the sub-directory. Please fix your testplugin.pro project file by using this instead:

    INCLUDEPATH += $$PWD/../kernel/
    

    However, what is even better design is to not handle it inside that project file, but the other kernel.pro where the header files reside. It is more flexible design to add this in there:

    INCLUDEPATH += $$PWD
    

    Edit: Based on your comment which was not in the original question, it seems that you have another problem. You seem to have messed with the include guards called the same in two different files and that is why the second inclusion did not result in providing the accessibility for you.