PDA

View Full Version : Qt Creator Subfolder Organization



hiead
19th October 2013, 21:30
I've reviewed several similar questions and their answers and nothing I've tried thus far has worked.

I'm starting a Qt project that will be quite large in scope. To keep things better organized, I intend on utilizing subdirectories within my Qt project. Things are not off to a good start.

I am using: Qt Creator 2.8.1 based on Qt 5.1.1

Here is what I've come up with based on previous answers I've seen:

feather.pro

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = feather
TEMPLATE = app

include(gui/gui.pri)

SOURCES += main.cpp
main.cpp


#include <QApplication>
#include "gui/mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}
gui/gui.pri

SOURCES += gui/mainwindow.cpp
HEADERS += gui/mainwindow.h
gui/mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};

#endif // MAINWINDOW_H
gui/mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}

MainWindow::~MainWindow()
{
}
This is about as barebones as it gets. The error received is:


Error: dependent '..\feather\mainwindow.h' does not exist. jom:
C:\Users\Zac\Dev\Qt\build-feather-Desktop_Qt_5_1_1_MSVC2012_32bit-Debug\Makefile
[debug] Error 2 16:12:50: The process
"C:\Qt\Qt5.1.1\Tools\QtCreator\bin\jom.exe" exited with code 2. Error
while building/deploying project feather (kit: Desktop Qt 5.1.1
MSVC2012 32bit) When executing step 'Make'

The error is the same if I use the standard feather.pro:

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = feather
TEMPLATE = app

SOURCES += main.cpp \
gui/mainwindow.cpp
HEADERS += gui/mainwindow.h
I don't know why it's dependent on feather/mainwindow.h instead of feather/gui/mainwindow.h. Any advice?

ChrisW67
19th October 2013, 22:07
You are #include-ing "mainwindow.h". The first place the preprocessor will look is the compiler's current working directory which in a standard build is the folder containing the PRO file i.e. not the gui sub directory. If the file is not found there then it will search the INCLUDEPATH folders before giving up.

If you use the PRI file approach to combine files from multiple folders into a single project then often you want (or need) to set INCLUDEPATH in the sub folder PRI files:


INCLUDEPATH += gui
DEPENDPATH += gui
SOURCES += gui/mainwindow.cpp
HEADERS += gui/mainwindow.h
// Or, HEADERS += $${PWD}/mainwindow.h, see note below

You can also use $${PWD} in place of a hard coded directory name so that you can rename/relocate "gui" without having to revisit the PRI files: this is not required though. DEPENDPATH is set so that Makefile rules are created to capture dependencies between files in the parent and child directories of your project.

hiead
20th October 2013, 02:26
Thanks for the response; however, I'm still not able to get this to work. I've changed to the following:
feather.pro

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = feather
TEMPLATE = app

include(gui/gui.pri)

SOURCES += main.cpp
gui/mainwindow.cpp

#include "gui/mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}

MainWindow::~MainWindow()
{

}
gui/gui.pri

INCLUDEPATH += gui
DEPENDPATH += gui
SOURCES += gui/mainwindow.cpp
HEADERS += gui/mainwindow.h
(I also tried with $${PWD} in place of gui, but it did not help)

If the mainwindow class is simply in the same directory as main.cpp, it works fine; however, I do not wish to have my entire source in one directory.

ChrisW67
21st October 2013, 00:50
There was no need to change mainwindow.cpp, only the PRI file, and then you must rerun qmake.


//>>>>> feather.pro

QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = feather
TEMPLATE = app
include(gui/gui.pri)
SOURCES += main.cpp

//>>>>> main.cpp

#include <QApplication>
#include "gui/mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

//>>>>> gui/gui.pri

INCLUDEPATH += gui
DEPENDPATH += gui
SOURCES += gui/mainwindow.cpp
HEADERS += gui/mainwindow.h

//>>>>> gui/mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};

#endif // MAINWINDOW_H

//>>>>> gui/mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}

MainWindow::~MainWindow()
{
}

Works fine, even if you remove the "gui/" from the #include in main.cpp.