QT: ui_* files not generated - c++

I know it is a topic already touched by a lot of user, but I don't find a valid solution; I have two form files created with QtDesigner:
interfaccia_test.ui
interfaccia.ui
I launch qmake -project command obtaing the following .pro file:
TEMPLATE = app
TARGET = qtgeo
INCLUDEPATH += . include
# Input
HEADERS += interfaccia.h include/localizzazione.hpp
FORMS += interfaccia.ui interfaccia_test.ui
SOURCES += interfaccia.cpp main.cpp src/localizzazione.cpp
src/SimpleSIFT.cpp
so i give qmake command but it doesn't generate ui_interfaccia.h and ui_interfaccia_test.h like I expected; then I try to make my project and I have
interfaccia.h:19:28: fatal error: ui_interfaccia.h: File o directory not found
#include "ui_interfaccia.h"
^
with interfaccia.h my file that use the GUI I made.

For any given project, you should ever only use qmake -project once. It's meant as a starting point if you have a bunch of files and want to get a project template. This template is then meant to be modified by a human being - you.
The normal way to build a qmake-based Qt project would be:
qmake
make
The ui_xxx.h files are generated by make, not qmake. Here's a list of what the various tools do:
qmake -project Generates a .pro file template for you to modify to suit the project. This should never be used by your end users, or by you after the project is going. It's your job to keep the .pro file up-to-date.
qmake or cmake Generates the makefile for the build system.
make or ninja Builds the project, generating all the other files.
There are two additional points:
The qmake won't generate the ui_xxx.h file if you've included a file that wouldn't be generated. So, for example, if it'd generate a file called ui_Interfaccia.h, but you've included ui_interfaccia.h, then the file with the wrong name nor the file with the correct name get generated.
This matters even if you're building everything on a case-insensitive OS/filesystem.
You're including the file with a wrong name. The correct name is ui_ClassName.h, where ClassName is the name of the class from the .ui file (look at the first few lines), with the same capitalization.

Related

Adding JSON file to plugin qmake project

When writing plugin libraries with Qt one can attach a JSON file containing some meta data to it using the Q_PLUGIN_METADATA macro. This JSON file is then linked into the library, for later usage with QPluginLoader::metaData().
Unfortunately when building the plugin library the associated JSON file is not by default seen as a dependency for the library binary by qmake. When the JSON file is modified the plugin library project has to be rebuild (especially re-linked) manually to force the modified JSON file into the library binary.
What would be the proper way to mention the JSON file in the .pro file so that it is automatically linked in when it is modified?
I typically use the following to make the json file a dependency of the generated moc file that contains the corresponding code. Assuming the class where you specify Q_PLUGIN_METADATA is located in a header file called myclass.h, the qmake code is as follows:
DISTFILES += myclass.json
json_target.target = moc_myclass.o
json_target.depends += $$PWD/myclass.json
QMAKE_EXTRA_TARGETS += json_target
Note: You might have to use json_target.target = $$OBJECTS_DIR/moc_myclass.o instead, if OBJECTS_DIR has previously been defined. Check the generated Makefile to see if the path of the dependency matches the one of the corresponding target.
Well, you could just add the JSON file to resources: create some *.qrc file, add yours there and then write in the .pro file something like RESOURCES += plugin_data.qrc.-
There is also DISTFILES variable, but AFAIK it's Unix-only and does not solve your problem.
Tried myself and it never worked, still the recipe from documentation works: INCLUDEPATH += JSON_FILE_LOCATION_DIR. It's true that qmake caches builds sometimes, but they say adding to include path should do the trick and make a proper build.

cmake: read and compile dynamically-generated list of cpp files

I have a custom tool that processes a given list of IDL files and produces a number of .cpp and .h files as output. I want to add those files to the list of things to compile in my CMakeLists, and also model the dependencies those files have on the IDL.
To keep things simple, I will state that any change to any of the IDL files should trigger a regeneration of all cpp/h.
I have a custom command that takes care of running the generator tool and listing all the IDL files as dependencies.
My issue is getting the subsequent list of cpp/h files into cmake at build-time. It is not possible to infer from the name of the IDL files what cpp files will be generated. My generator tool will, however, output the list of generated files to a text file.
So my question is: how do I instruct cmake to "read from this text file and add the contents as extra source and header files to be compiled", also bearing in mind that the said text file only exists during a certain point of the build?
CMake needs to be able to infer the names of all .cpp files participating in the build at configure time. It is not possible to add files afterwards without re-running CMake.
One possible approach would be to use a two-phase CMake build: Instead of building the generated source files directly from your main project, you create a separate CMake project for building just the generated sources.
Then in your main CMake project you add a custom target that runs after the code generation and invokes CMake to both configure and build the generated files project.
The disadvantage here is that the generated files no longer appear as part of the main project. Also some trickery is required if you don't want to rebuild the generated sources every time - custom targets are always considered out-of-date, so you might want to use a script here that only runs CMake on the subproject if the generated files changed.
This is a few years late but this works just fine:
#run whatever tool that generates the cpp files
execute_process(COMMAND "./your_tool.sh")
#read files from files.txt and make a cmake 'list' out of them
file(READ "files.txt" SOURCES)
#found this technique to build the cmake list here:
#http://public.kitware.com/pipermail/cmake/2007-May/014236.html
#maybe there is a better way...
STRING(REGEX REPLACE ";" "\\\\;" SOURCES "${SOURCES}")
STRING(REGEX REPLACE "\n" ";" SOURCES "${SOURCES}")
#at this point you have your source files inside ${SOURCES}
#build a static library...?
add_library(mylib STATIC ${SOURCES})
There is a function that build the list directly from file:
file(STRINGS <filename> <variable> [<options>...])
source: https://cmake.org/cmake/help/v3.11/command/file.html

qt4 scons including a uic file in variant_dir

I'm using the qt4 tool for scons and having some troubles getting the .ui files to be handled correctly. I'm coming from a Cmake background with Qt and a beginner with scons.
In my SConstruct file I have
env.Uic4(Glob('*.ui'))
env.Program('test',Glob('*.cpp'))
The problem is that my source file can't find the resulting header files src/qt-test/sample_widget.cpp:3:23: error: ui_sample.h: No such file or directory. The header file is created, as is all moc processing done, thus I'm pretty sure everything is installed correctly and basically correct.
What I think is happening is because this is a recrusive SConstruct file, and the caller is using a variant_dir for the build. So possibly the problem is just getting the compiler to resolve headers in the build directory (and perhaps nothing to do with the qt4 tool). This was handled automatically in CMake (I think).
So how do I get this working (get the ui include file to be found)?
Use the CPPPATH construction variable to set the include paths as mentioned in the man pages:
http://scons.org/doc/production/HTML/scons-user/a4916.html
For example:
env.Append(CPPPATH = ['dir1', 'dir2'])
Brady

QAction: No such file or directory

I'm getting the error
QAction: No such file or directory
when I try to compile a project for plugin (C++ Library template). Weird, because I have a project for my app which also includes this header and there is no error. What might cause this?
For me I had some stale moc_ and ui_ files left over from compiling under a different version and configuration of Qt, so removing them solved the problem for me.
rm moc_* ui_* *.o
Make sure that you have the right include paths set up.
If you use QMake the *.pro should contain these settings if you want to include files from QtGui. They should be set by default but some templates may not set them.
CONFIG += qt
QT += gui
If you use another build system then make sure that you either use
#include <QtGui/QAction>
or you add $QTDIR/include/QtGui and not just $QTDIR/include to your include path

Qt unit test dependency problem

Hee,
I'm kinda new to Qt and i started to add UnitTests to my Qt project. Qt demands that i put my unit tests in another project, so i did.
But now i have dependent source files in my first project. I made my 'main project' a dependency of my 'test project'.
I cannot seem to include any of the '.h' files from my 'main project'. The unit test them self run properly as long as i do not use classes from my 'main project'.
I looked into the Qt documentation, but i cannot find the solution for my problem. Am i missing something?
Did you try to include your dependencies to .pro file of your test project?
HEADERS += ../MyHeader.h
SOURCES += ../MyHeader.cpp \
tst_myUnitTestName.cpp
You can try adding the path of your main program in the includes (in the testProject.pro file)
INCLUDEPATH += .. .
(or just add the path to the project itself)
if you do this, you will then need to change your includes (in the .cpp files) from "" to <>
#include < MyHeader.h >
Adding the headers to the .pro file will mostly allow you to have easy access to the file itself if you're using QtCreator (and qmake will complain if it doesn't find the files), but it will not solve the actual dependencies within each .cpp file.