Results 1 to 4 of 4

Thread: Qt Creator Subfolder Organization

  1. #1
    Join Date
    Jun 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt Creator Subfolder Organization

    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 Code:
    1. QT += core gui
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. TARGET = feather
    6. TEMPLATE = app
    7.  
    8. include(gui/gui.pri)
    9.  
    10. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "gui/mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    gui/gui.pri
    Qt Code:
    1. SOURCES += gui/mainwindow.cpp
    2. HEADERS += gui/mainwindow.h
    To copy to clipboard, switch view to plain text mode 
    gui/mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class MainWindow : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MainWindow(QWidget *parent = 0);
    12. ~MainWindow();
    13. };
    14.  
    15. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    gui/mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. }
    7.  
    8. MainWindow::~MainWindow()
    9. {
    10. }
    To copy to clipboard, switch view to plain text mode 
    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 Code:
    1. QT += core gui
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. TARGET = feather
    6. TEMPLATE = app
    7.  
    8. SOURCES += main.cpp \
    9. gui/mainwindow.cpp
    10. HEADERS += gui/mainwindow.h
    To copy to clipboard, switch view to plain text mode 
    I don't know why it's dependent on feather/mainwindow.h instead of feather/gui/mainwindow.h. Any advice?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt Creator Subfolder Organization

    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:
    Qt Code:
    1. INCLUDEPATH += gui
    2. DEPENDPATH += gui
    3. SOURCES += gui/mainwindow.cpp
    4. HEADERS += gui/mainwindow.h
    5. // Or, HEADERS += $${PWD}/mainwindow.h, see note below
    To copy to clipboard, switch view to plain text mode 
    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.

  3. #3
    Join Date
    Jun 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Creator Subfolder Organization

    Thanks for the response; however, I'm still not able to get this to work. I've changed to the following:
    feather.pro
    Qt Code:
    1. QT += core gui
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. TARGET = feather
    6. TEMPLATE = app
    7.  
    8. include(gui/gui.pri)
    9.  
    10. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 
    gui/mainwindow.cpp
    Qt Code:
    1. #include "gui/mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. }
    7.  
    8. MainWindow::~MainWindow()
    9. {
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 
    gui/gui.pri
    Qt Code:
    1. INCLUDEPATH += gui
    2. DEPENDPATH += gui
    3. SOURCES += gui/mainwindow.cpp
    4. HEADERS += gui/mainwindow.h
    To copy to clipboard, switch view to plain text mode 
    (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.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt Creator Subfolder Organization

    There was no need to change mainwindow.cpp, only the PRI file, and then you must rerun qmake.
    Qt Code:
    1. //>>>>> feather.pro
    2.  
    3. QT += core gui
    4. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    5. TARGET = feather
    6. TEMPLATE = app
    7. include(gui/gui.pri)
    8. SOURCES += main.cpp
    9.  
    10. //>>>>> main.cpp
    11.  
    12. #include <QApplication>
    13. #include "gui/mainwindow.h"
    14.  
    15. int main(int argc, char *argv[])
    16. {
    17. QApplication a(argc, argv);
    18. MainWindow w;
    19. w.show();
    20.  
    21. return a.exec();
    22. }
    23.  
    24. //>>>>> gui/gui.pri
    25.  
    26. INCLUDEPATH += gui
    27. DEPENDPATH += gui
    28. SOURCES += gui/mainwindow.cpp
    29. HEADERS += gui/mainwindow.h
    30.  
    31. //>>>>> gui/mainwindow.h
    32.  
    33. #ifndef MAINWINDOW_H
    34. #define MAINWINDOW_H
    35.  
    36. #include <QMainWindow>
    37.  
    38. class MainWindow : public QMainWindow
    39. {
    40. Q_OBJECT
    41.  
    42. public:
    43. MainWindow(QWidget *parent = 0);
    44. ~MainWindow();
    45. };
    46.  
    47. #endif // MAINWINDOW_H
    48.  
    49. //>>>>> gui/mainwindow.cpp
    50.  
    51. #include "mainwindow.h"
    52.  
    53. MainWindow::MainWindow(QWidget *parent)
    54. : QMainWindow(parent)
    55. {
    56. }
    57.  
    58. MainWindow::~MainWindow()
    59. {
    60. }
    To copy to clipboard, switch view to plain text mode 
    Works fine, even if you remove the "gui/" from the #include in main.cpp.

Similar Threads

  1. Replies: 5
    Last Post: 22nd November 2012, 04:46
  2. Replies: 2
    Last Post: 13th January 2011, 19:36
  3. About tree organization...
    By Patrick Sorcery in forum Newbie
    Replies: 2
    Last Post: 3rd September 2010, 08:43
  4. Replies: 3
    Last Post: 1st May 2010, 20:43

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.