Important: Please read the Qt Code of Conduct -

How to use part of one project in other project?

  • Hi.

    This is my first real app with QT and QtCreator. Can someone tell me how in QtCreator 3.5.1 in Ubuntu use part of one project into another? Directory three looks that

               #some headers
                #some sources
            #other sources

    my-qt-project is a console version of app. The second one is version with GUI which use a lot of code from the first one, but I cannot import headers and compile it. In project build settings I set my-qt-project as a dependency.

    There a simple way to share part of one project with another? I tried use but it does not work. - I was here too.

    Best regards.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The subdirs template is the right thing to use. Usually you would create one or more library projects with the common code and then one project per application.

    What did you try exactly ? Can you share your .pro files ?

  • Basic project

    CONFIG += c++11
    QMAKE_CXXFLAGS += -std=c++11
    QT += sql
    QT -= gui
    TEMPLATE = app
    TARGET = qt-simple-notifier
    INCLUDEPATH += . \
    # Input
    SOURCES += main.cpp \
    DISTFILES += \
        LICENSE \
    HEADERS += \

    Project with GUI

    template = subdirs
    SUBDIRS += ../qt-simple-notifier
    INCLUDEPATH += ../qt-simple-notifier
    CONFIG += c++11
    QMAKE_CXXFLAGS += -std=c++11
    QT       += core gui
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    TARGET = qt-simple-notifier-gui
    TEMPLATE = app
    SOURCES += main.cpp \
            mainwindow.cpp \
    HEADERS += mainwindow.h \
    FORMS    += mainwindow.ui \
    RESOURCES += \
    DISTFILES += \
        LICENSE \

    My *.pro files.

    I don;t want create library project with shared code. I just want include and compile a few class in project with GUI from project with console interface.

    Best regards.

  • Lifetime Qt Champion

    Even without a library, your design is wrong.

    You should have something like:

    -- MyProject
    ---- cli_project
    ---- gui_project

    With MyProject being the subdirs template pointing to the other two. Then in your GUI project you'll use SOURCES += ../cli_project/file_you_want.cpp etc.

  • That was more complicated than I thought.
    Thanks for help. Now all work fine. - link to project with valid files.

    Best regards.

  • Lifetime Qt Champion

    Why are you adding the src folder to INCLUDEPATH?

Log in to reply