I've currently got a QT5.5 subdirs project setup, containing so far a core library, a QT GUI app and soon in the future a tests project (probably using unittest++).
I'm following the guide for setting up the project structure: http://dragly.org/2014/03/13/new-project-structure-for-projects-in-qt-creator-with-unit-tests/
So far, the directory structure is as follows:
/Project
src/
app/
app.pro (app project, depends on core.pro)
main.cpp
mainwindow.cpp
mainwindow.h
core/
core.pro (lib project)
class.h
class.cpp
tests/ (future test project location
Project.pro (subdirs)
defaults.pri
The following is my defaults.pri file:
INCLUDEPATH += $$PWD/src
SRC_DIR = $$PWD
However, I'm not sure if I'm getting the #include directives right.
When using the #include, I have to do #include "../core/class.h". I'm not sure if this is the best way to go?? My understanding was because it's a lib, I just reference the header file as if it was within the lib, rather than location on disk?
Would namespaces solve this problem?
If you add /Project/src as include dir for your project (-I on most compilers), you can include it with:
#include <core/class.h>
Related
I just started with Qt and some issues came up. I'm sure it must be a simple solution but I just can't find it.
I have two Projects, ProjectOne and ProjectTwo. I'd like you use the class foo from ProjectOne in main.cpp from ProjectTwo. When I run my Programm The Files are copied/referenced into ProjectTwo, but when i try to include foo in my main.cpp (#include "foo.h") i recieve the following error:
> main.cpp:3: error: C1083: Cannot open include file: 'foo.h':
> No such file or directory
Here's my Structure:
-ProjectOne
-ProjectOne.pro
-Headers
-foo.h
-Source
-foo.cpp
-Other files
-ProjectOne.pri
-ProjectTwo
-ProjectTwo.pro
-ProjectOne
-ProjectOne.pri
-Headers
-foo.h
-Sources
-foo.cpp
-Sources
main.cpp
Here's what i edited on my .pro and .pri files
**ProjectOne.pri**
INCLUDEPATH += $$PWD
SOURCES += $$PWD/foo.cpp
HEADERS += $$PWD/foo.h
**ProjectTwo.pro:**
include(../ProjectOne/ProjectOne.pri)
QT += core
SOURCES += foo.cpp
HEADERS += foo.h
I'm using Qt Creator 3.1.2 on Windows 7. My Programming Language is C++ and I'm compiling with VisualStudio 10 Express.
Any help is greatly appreciated!
When including a file that isn't directly in you project folder you need to include it with full or relative path.
i.e.
#include "bar/foo.h"
or
#include "../../bar/foo.h"
I've created form, saved it in project directory. And now i want to add some code.
So, I've created header file:
#ifndef SORTDIALOG_H
#define SORTDIALOG_H
#include <QtWidgets/QDialog>
#include <QtWidgets/QWidget>
#include "ui_sortdialog.h"
class SortDialog: public QDialog, public Ui::SortDialog
{
Q_OBJECT
public:
SortDialog(QWidget *parent=0);
void setColumnRange(QChar first, QChar last);
}
#endif // SORTDIALOG_H
during writing code Qt creator see ui_sortdialog.h, and i, for example, can see "Ui" namespace. But when i'n trying to compiler writes that ui_sortdialog.h wasn't found
C:\Qt\Qt5.1.1\Tools\QtCreator\bin\untitled2\sortdialog.h:8: error: ui_sortdialog.h: No such file or directory
#include "ui_sortdialog.h"
^
You created a form called sortdialog, right?
If you did it using Qt Creator, it was supposed to add the following line to your project's .pro file:
FORMS += sortdialog.ui
If there is no such line, add it to the .pro file.
When a project has .ui files, a command called uic is called as part of the build process. This uic ("ui compiler") is responsible for the generation of ui_sortdialog.h, in you case.
You rarely need to call it directly, running qmake prior to make should do it for you (if the aforementioned FORMS line is in you .pro file).
Qt sometimes experiences difficulties when the build directory is at the same folder as the *.pro file.
I suggest making sure that your build directory is one level higher in the directory structure than the project file.
The following directory structure is prone to error:
MyProj/proj.pro
MyProj/builds/
The following directory structure will avoid this issue:
MyProj/proj.pro
MyProjBuild/
For people who come here that don't have a .pro file and don't use qmake, but cmake:
Instead of:
add_executable(myprogram
gui.cpp
gui.ui
main.cpp
)
Use:
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
add_executable(myprogram
gui.cpp
gui.ui
main.cpp
)
Or:
qt5_wrap_cpp(UI_SOURCES
gui.cpp
)
qt5_wrap_ui(UI_HEADERS
gui.ui
)
add_executable(myprogram
main.cpp
${UI_SOURCES}
${UI_HEADERS}
)
FYI: The ui_gui.h header will be generated at ${CMAKE_CURRENT_BINARY_DIR}. Sources:
unable to include a ui_form header of QT5 in cmake
Qt 5 cmake fails with undefined reference to vtable on hello world with inc & src as subdirs
I had this problem.
Here is what I had to fix:
Make sure that sortdialog.cpp and sortdialog.ui are both in the pro file in the appropriate sections with the appropriate case (upper or lower exactly as in the file names).
I have my C++ project files and want to create an additional graphical user interface for these sourcefiles. I am using windows, MVSC2012 and Qt 5.1.1 with Qt Creator 2.8.1.
So here is what i have:
My QtCreator project folder, including the following auto-generated files
c:/creatorProject/creatorProject/main.cpp
c:/creatorProject/creatorProject/mainwindow.cpp
c:/creatorProject/creatorProject/mainwindow.h
c:/creatorProject/creatorProject/mainwindow.ui
c:/creatorProject/creatorProject/creatorProject.pro
c:/creatorProject/creatorProject/creatorProject.pro.user
Furthermore I the source files with the "logic" in a separated folder, e.g.
c:/programLogic/myFunctions.h
c:/programLogic/myFunctions.cpp
So I simply want to add these files to my QtCreator project so that I can e.g. include "myFunctions.h" and work with it.
My attempt: I used Qt Creator and added myFunctions.h respectively myFunctions.cpp by using "creatorProject >> right click >> add existing file..". After doing that my creatorProject.pro looks like this:
[...]
SOURCES += main.cpp\
mainwindow.cpp \
../../programLogic/myFunctions.cpp
HEADERS += mainwindow.h \
../../programLogic/myFunctions.h
Looks totally fine for me. Qt Creator even shows these files within the project explorer! However I have trouble using myFunctions.h within mainwindow.cpp.
#include "myFunctions.h" // Include can not be found
#include "../../programLogic/myFunctions.h" // Include is found but I get linker errors since myFunctions.cpp is not compiled?!
What is wrong within my setup?
You need to add the INCLUDEPATH also in your .pro file.
Something like:
INCLUDEPATH += "C:/programLogic"
Maybe I'm missing something, but it seems really stupid to me that the only tutorial provided for QtTest framework has you testing the QString class.
The typical use case for unit testing is....testing classes you wrote yourself, but there is no mention on how to reference your classes in a different project for testing in the tutorial and google has failed me as well(and I really doubt copy pasting classes is a good way to do it).
I even thumbed through 3 different Qt books with no mention of QtTest.
You can add include paths to other project directories in your .pro file like so:
INCLUDEPATH += <directory>
Then it should be able to find the headers that you are including.
Edit: Based on comment
That's another story altogether. Undefined reference usually means you are missing a dependency. This can usually be resolved with one of two things.
The simplest is to include the missing source file:
INCLUDEPATH += ../myotherproject/
SOURCES = main.cpp ../myotherproject/missingsource.cpp
Perhaps the better solution is to expose reusable code by compiling it as a library and linking to it. E.g. a .DLL or .LIB on Windows and .SO or .A on Linux.
INCLUDEPATH += ../myotherproject/
win32:LIBS += ../myotherproject/linkme.lib
Can you show us the specific errors you are getting?
I suggest that you put all sources and headers which both your main application project and your unit test project need into one .pri (.pro include) file. Put this file in the main project. Then include this file in both projects.
Note that whenever adding a new class to the main project, QtCreator automatically appends the SOURCES += and HEADERS += lines to the .pro file, but you want them to be in the .pri file, so you need to move them afterwards manually. I think that there is no solution to tell QtCreator where to put them.
Main project:
myproject.pro
myproject.pri
main.cpp
someclass.h
someclass.cpp
myproject.pro:
QT += ...
TARGET = ...
...
SOURCES += main.cpp # "private" to this project
include(myproject.pri) # needed in unit test
myproject.pri:
SOURCES += someclass.cpp
HEADERS += someclass.h
Unit test project:
unittest.pro
main.cpp
test.h
test.cpp
unittest.pro:
QT += ...
TARGET = ...
...
SOURCES += main.cpp test.cpp
HEADERS += test.h
# include the classes from the main project:
INCLUDEPATH += ../myproject/
include(../myproject/myproject.pri)
I'm quite a newbie with C++ and maybe that's a very stupid question, but how do one include a header from a static linked library?
I've created a static library in Qt Creator with the following .pro file:
QT -= gui
TARGET = Foobar
TEMPLATE = lib
CONFIG += staticlib
SOURCES += thefoobar.cpp \
sub/subbar.cpp
HEADERS += thefoobar.h \
sub/subbar.h
compiled it and put the resulting libFoobar.a into the "extstaticlibs" folder of my target project.
In my target projects .pro file i've added the following lines:
LIBS += -L$$PWD/extstaticlibs/ -lFoobar
INCLUDEPATH += $$PWD/extstaticlibs
The target project compiles without problems. But when I try to include the header thefoobar.h in one of my code files:
#include "thefoobar.h"
it always results in an error:
error: thefoobar.h: No such file or directory
Any suggestions for the correct syntax would be very much appreciated.
Kristoffer
Check where you have placed your "thefoobar.h" header file . Place it in the "extstaticlibs/" folder .
If I follow your description correctly, you ONLY put the static library into your extstaticlibs directory.
You need to carry over your thefoobar.h file too. If you follow the common structure you could make:
extstaticlibs/include <- thefoobar.h goes here
extstaticlibs/lib <- libFoobar.a goes here
You then need to modify your project file like this:
LIBS += -L$$PWD/extstaticlibs/lib -lFoobar
INCLUDEPATH += $$PWD/extstaticlibs/include
Of course you can all throw it in one directory, if you want, but it may be helpful to sort things out in the beginning.