libconfig used in Qt readFile failed - qt

firstly,I have downloaded the libconfig x64-windows via vcpkg like this:
PS E:\vcpkg> .\vcpkg install libconfig:x64-windows
then,I added the lib to my Qt project with three steps
1.add the headfile
#include "libconfig.h++"
2.specify the lib path in the pro file(I have tried two ways)
LIBS += -L$PWD -llibconfig++
#LIBS += libconfig++.lib
3.in the build debug folder,I added the libconfig++.dll
after doing those steps,project was been built successfully without any error.then I added some code like these:
libconfig::Config cfg;
try{
cfg.readFile("D:\test.cfg");
}
catch(const libconfig::FileIOException&filex) {
Q_UNUSED(filex);
qDebug()<<"error";
return;
}
qDebug()<<"success";
and the output was always :"error",I have done the everything and the Qt window was displayed successfully.Once I called readFile api,it occured an error .

I have got the answer to my issue. The file path in Windows must have the double backslash.
So I must switch:
cfg.readFile("D:\test.cfg");
to
cfg.readFile("D:\\test.cfg");

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

DEPLOYMENTFOLDERS setting in .pro file not working

I am creating a QML Qt5 project, and want to automatically copy my qml files into the build folder when I click build. I've edited my .pro file to include:
qmlFolder.source = qml
qmlFolder.target = .
DEPLOYMENTFOLDERS += qmlFolder
message("adding qml folder")
My project source folder structure is:
|
+-- build
+-- source
|
+--main.cpp
|
+--qml
|
+---main.qml
So when I click build I see the "adding qml folder" message in the Compile Output tab (in Qt creator) so I know make is reading the .pro file. But the build folder is missing ANY qml files. And I don't see any copy messages, or error message with build output.
Why isn't my DEPLOYMENTFOLDERS statement working? (I'm compiling under Linux in case that matters)
The DEPLOYMENTFOLDERS directive is not well documented but should work as noted in this SO question
It turns out that .pro feature is actually available in a deployment.pri file (which I failed to include). Once incuded, and adding the qtcAddDeployment line to the .pro file, the feature worked as expected.
The important part of the .pri file is:
defineTest(qtcAddDeployment) {
for(deploymentfolder, DEPLOYMENTFOLDERS) {
item = item$${deploymentfolder}
greaterThan(QT_MAJOR_VERSION, 4) {
itemsources = $${item}.files
} else {
itemsources = $${item}.sources
}
$$itemsources = $$eval($${deploymentfolder}.source)
itempath = $${item}.path
$$itempath= $$eval($${deploymentfolder}.target)
export($$itemsources)
export($$itempath)
DEPLOYMENT += $$item
}
}

QML_IMPORT_NAME seems to be an unknown variable, qml module not found

I'm trying to run this example with qt 5.15.1
When I declare QML_IMPORT_NAME, the variable seems to be unknown from qt (see the font's color
on the below screenshot) and when I import "com.mycompany.messaging" in my qml file, I have an error "QML module not found."
Edit:
After some investigations, the code runs as it should but I have this error in Qt Creator. If I want to edit qml file with the gui editor, I need to comment out all code related to the backend in text mode before otherwise, it fails to open the file.
What is the trick?
With this, I assumed I should have added
CONFIG += qmltypes
to .pro file. But since, I switched to cmake and did not find an equivalent, so I used the old method with:
qmlRegisterType<Person>("People", 1,0, "Person");
in main.cpp (see above link).
This is a known bug that is not fixed yet (https://bugreports.qt.io/browse/QTCREATORBUG-24987).
The reason for the error is that QtCreator requires the generated .qmltypes and metatypes.json files next to the application binary.
A workaround for this is to add the following to your pro file:
CONFIG(debug, debug|release) {
EXE_DIR = $${OUT_PWD}/debug
} else {
EXE_DIR = $${OUT_PWD}/release
}
CONFIG += file_copies
COPIES += qmltypes metatypes
qmltypes.files = $$files($${OUT_PWD}/$${TARGET}.qmltypes)
qmltypes.path = $${EXE_DIR}
metatypes.files = $$files($${OUT_PWD}/$${TARGET}_metatypes.json)
metatypes.path = $${EXE_DIR}

QtCreator: kit-specific precompiler macro definitions

I am using QtCreator 3.1.1 to build a cross-platform project, and so I arranged to have different compilation kits for targeting my desktop PC and my BeagleBoneBlack (BBB).
Now I would like to define some macro in qmake project file (.pro) which are specific only for a given kit.
In other words I would like do in my .pro file something like:
if(kit == BBB)
DEFINES += MY_BBB_MACRO
elseif(kit == Desktop)
DEFINES += MY_DESKTOP_MACRO
else
DEFINES += OTHER_MACRO
Is is possible? How can I do that?
I obtained some help on Qt forum (take a look here) about this problem...
Anyway the solution consists in using qmake built-in test functions.
Basically I've added some CONFIG directive in QtCreator's project management: in the following screenshot you can see for example you can see that I've added CONFIG+=BBB in the project configuration for BBB kit; in the same way I've added CONFIG+=AM335x and CONFIG+=Desktop to AM335x and Desktop kits, respectively...
Then, in my .pro file I've added something like:
and now in my source code I can use something like #ifdef PLATFORM_BBB, #ifdef PLATFORM_AM335X and #ifdef PLATFORM_DESKTOP for differentiating the program behavior depending on compilation kit.
I found another solution.
First, add additional arguments in Projects using CONFIG+=Variable name for kit.
And in .pro file, write some code like below.
Desktop {
message("Desktop is selected")
}
RPI {
message("rpi is selected")
target.path = /home/pi
INSTALLS += target
}
If you look at the general message tab, you can see that the setting works well.

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