Qt QMake - Define a string using replace function 'eval' - qt

I'm trying to refactoring some part of my .pro files.
To do that, I'm using 'eval' & 'export' QMake functions. More precisely, I'm trying to define a string to use it in source code.
eval(DEFINES += TEST_DEFINE=\\\"MyString\\\")
eval(export(DEFINES))
When I try to use TEST_DEFINE in source code, TEST_DEFINE is no longer a string...
Ex:
qDebug() << "Test : " << TEST_DEFINE;
Compilation Output:
use of undeclared identifier 'MyString'
And if I try without using eval, it's working properly:
DEFINES += TEST_DEFINE=\\\"MyString\\\"
Any ideas ? :)

Related

QMake 5.15 don't understand wildcard adding files [duplicate]

I have a .pro file which looks like:
SOURCES += myfolder/source1.cpp \
myfolder/source2.cpp
HEADERS += myfolder/header1.h\
myfolder/header2.h
FORMS += myfolder/form1.ui\
myfolder/form2.ui
And everything works great. However, if I try to use an asterisk to include all the files, i.e.:
SOURCES += myfolder/*.cpp
HEADERS += myfolder/*.h
FORMS += myfolder/*.ui
qmake throws a file-not-found-error:
WARNING: Failure to find: myfolder\*.cpp
[...]
:-1: error: No rule to make target `myfolder/*.cpp', needed by `release/source1.o'. Stop.
In both cases, Qt-Creator can find the files.
Is there a way to use the asterisk? It's annoying to type the files manually.
Thank you!
[EDIT: Qt 4.8.4, Windows 7, Qt-Creator 2.6.1. Sry for forgetting this thought it isnt needed.]
[EDIT: Found solution: http://qt-project.org/forums/viewthread/1127 . Thank you anyway!]
In qmake 3.0, at least, it's possible to use something like:
SOURCES = $$files(*.cpp, true)
HEADERS = $$files(*.h, true)
The true argument will cause the files function to recursively find all files matching the pattern given by the first argument.
At first, using asterisk is bad practice - despite that qmake allows it, QtCreator cannot edit such *.pro correctly on adding new, renaming or deleting file. So try to add new files with "New file" or "Add existing files" dialogs.
QMake has for loop and function $$files(directory_path: String). Also append files to SOURCES or HEADERS variable respectively.
Brief example, which adds all files, but not directories, to variable FILES (not affect build or project tree):
files = $$files($$PWD/src)
win32:files ~= s|\\\\|/|g
for(file, files):!exists($$file/*):FILES += $$file
If you want to check if file is *.cpp, try to use contains($$file, ".cpp").
files = $$files($$PWD/src)
win32:files ~= s|\\\\|/|g
for(file, files):!exists($$file/*):contains($$file, ".cpp"):SOURCES += $$file

Issue with space in qt pro file

I want to add macro (make constant) of the application name in qt pro file and use it later in code. For example:
QMAKE_TARGET_NAME = Foo Bar
DEFINES += APP_NAME=\\\"$$QMAKE_TARGET_NAME\\\"
When I use: qDebug() << APP_NAME it shows only "Foo". How to escape space in qt pro file? Thanks in advance for your help.
DEFINES += APP_NAME=$$shell_quote($$QMAKE_TARGET_NAME)
or if the " should be part of the define, then
DEFINES += APP_NAME=$$shell_quote(\"$$QMAKE_TARGET_NAME\")

How to change PMD's Copy Paste Detector (CPD) report output

I'd like to modify CPD to only spit out the Found a X line (Y tokens) duplication in the following files: ... when generating a report, i.e. suppress the source lines of code. I have the /src/ files and attempted to modify SimpleRenderer.java in /src/net/sourceforge/pmd/cpd/ by commenting out
String source = match.getSourceCodeSlice();
if (trimLeadingWhitespace) {
String[] lines = source.split("[" + PMD.EOL + "]");
int trimDepth = StringUtil.maxCommonLeadingWhitespaceForAll(lines);
if (trimDepth > 0) {
lines = StringUtil.trimStartOn(lines, trimDepth);
}
for (int i=0; i<lines.length; i++) {
rpt.append(lines[i]).append(PMD.EOL);
}
return;
}
However the report has not changed. I'm a bit of a Java novice, so keep that in mind. Do I need to rebuild the pmd-4.2.x in Eclipse somehow?
There are different ways to achieve this:
Without modifying PMD/CPD at all by using egrep. You can e.g. post-filter the report:
bin/run.sh cpd --minimum-tokens 100 --files src --encoding UTF-8 \
| egrep "^Found a |^Starting at line "
This would output now only the lines which start with "Found a " or "Starting at line ".
Modifying PMD/CPD to adjust the report format. I would however suggest, to implement this modified report format as a separate format, e.g. naming it "text_without_sources", rather than changing the default format. You would then call CPD with bin/run.sh cpd --format text_without_sources ....
In that case, you'll need to build PMD from sources. PMD uses Maven to build (you can use eclipse during development - but the package is built with maven). After a mvn clean package in the top-directory of the cloned sources from https://github.com/pmd/pmd you'll find the binary in the directory pmd-dist/target/.
Look at how the reports are integrated in CPDConfiguration.java - you can add your own version of the SimpleRenderer.
Create a feature-request at https://sourceforge.net/p/pmd/bugs/

QOpenGLShaderProgram: is possible to make error output nice?

I'm implementing some numerical algorithms on GPU via OpenGL and Qt.
But i am not very familiar with it.
I want to extract some functions from my current shader to some "shader library" and use it in my other shaders by string interpolation. It not hard to implement but i don't know how handle shader's compile errors
I use following code to compile shader
QOpenGLShaderProgram *shaderProgram = new QOpenGLShaderProgram();
if (!shaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment,fragmentShaderFileName)) {
qDebug() << "Failed to compile fragment shader";
//..........
When some compile error appears Qt print following message (an example)
QOpenGLShader::compile(Fragment): 0:331(9): error: syntax error, unexpected NEW_IDENTIFIER, expecting ',' or ';'
*** Problematic Fragment shader source code ***
//my shader source code
Is possible to catch error line number and use it to build my own error message? (with highlighted line)
According to the Qt documentation, you can use QOpenGLShaderProgram::log():
Returns the errors and warnings that occurred during the last link()
or addShader() with explicitly specified source code.
You can then parse the resulting string to build your own error message.

Qt - Using asterisk (*) in .pro-File with directories

I have a .pro file which looks like:
SOURCES += myfolder/source1.cpp \
myfolder/source2.cpp
HEADERS += myfolder/header1.h\
myfolder/header2.h
FORMS += myfolder/form1.ui\
myfolder/form2.ui
And everything works great. However, if I try to use an asterisk to include all the files, i.e.:
SOURCES += myfolder/*.cpp
HEADERS += myfolder/*.h
FORMS += myfolder/*.ui
qmake throws a file-not-found-error:
WARNING: Failure to find: myfolder\*.cpp
[...]
:-1: error: No rule to make target `myfolder/*.cpp', needed by `release/source1.o'. Stop.
In both cases, Qt-Creator can find the files.
Is there a way to use the asterisk? It's annoying to type the files manually.
Thank you!
[EDIT: Qt 4.8.4, Windows 7, Qt-Creator 2.6.1. Sry for forgetting this thought it isnt needed.]
[EDIT: Found solution: http://qt-project.org/forums/viewthread/1127 . Thank you anyway!]
In qmake 3.0, at least, it's possible to use something like:
SOURCES = $$files(*.cpp, true)
HEADERS = $$files(*.h, true)
The true argument will cause the files function to recursively find all files matching the pattern given by the first argument.
At first, using asterisk is bad practice - despite that qmake allows it, QtCreator cannot edit such *.pro correctly on adding new, renaming or deleting file. So try to add new files with "New file" or "Add existing files" dialogs.
QMake has for loop and function $$files(directory_path: String). Also append files to SOURCES or HEADERS variable respectively.
Brief example, which adds all files, but not directories, to variable FILES (not affect build or project tree):
files = $$files($$PWD/src)
win32:files ~= s|\\\\|/|g
for(file, files):!exists($$file/*):FILES += $$file
If you want to check if file is *.cpp, try to use contains($$file, ".cpp").
files = $$files($$PWD/src)
win32:files ~= s|\\\\|/|g
for(file, files):!exists($$file/*):contains($$file, ".cpp"):SOURCES += $$file

Resources