I was trying to centralize a variable from .pro file into my project by trying to define it and print in another .qml file
.pro file
VERSION=2.4
DEFINE APPVERSION=VERSION;
.qml file
text: qsTr(APPVERSION)
this seems to give me the above subjective error. Must there be any other way.
DEFINE works for C++, it is not for qml so a possible solution is to convert the DEFINE to string and then export it to qml using setContextProperty:
*.pro
VERSION = 2.4
DEFINES += APPVERSION=$$VERSION
main.cpp
// ...
#define QUOTE_(x) #x
#define QUOTE(x) QUOTE_(x)
// ...
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("APPVERSION", QUOTE(APPVERSION));
// ...
*.qml
text: APPVERSION
Related
I have just started using QtCreator (and C++ after 15 years away from it). I want to do some linear algebra stuff so I have included the library Eigen in my project file as follows (I have placed the Eigen library directly in my project for the moment):
INCLUDEPATH = "/home/Software/QtProjects/MyProject/eigen/"
My source file is:
#include <QCoreApplication>
#include <iostream>
#include <Eigen/Dense> % 'Eigen/Dense' file not found
using Eigen::MatrixXd; % use of undeclared identifier 'Eigen'
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MatrixXd m(2,2); % unknown type name 'MatrixXd'
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = 8;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
return a.exec();
}
This code works and outputs the values of the matrix m in the console. However, in the QtCreator IDE, I have several error messages which I have included above as comments on the lines at which they appear.
So is there some way to fix this and make QtCreator stop showing these lines as errors when the code is actually working fine?
Try without quotation marks; and with += instead of =; INCLUDEPATH += /home/Software/QtProjects/MyProject/eigen/.
Go to Build > Run qmake to validate changes in the pro file.
INCLUDEPATH in qmake project file doesn't work
Qt creator Adding external library
How to add include path in Qt Creator?
On Linux, run
sudo apt-get install libeigen3-dev
Then add this to your .pro file
INCLUDEPATH += /usr/include/eigen3
And then run qmake
I am using the nlohmann json (https://github.com/nlohmann/json) library (1 single json.h file) with Qt. I realize Qt has it's own json functionality, so assume that any third party header may be used. Qt generates a whole lot of issues/warnings and I would like to try suppress issues/warnings for just the json.h file.
[EDIT] Interestingly the json third party library has been updated from v3.3.0 to v3.4.0 and all warnings gone (probably best outcome). Nonetheless, being able to suppress third party warnings would still be great.
I tried the suggestions here, by putting the .h in a sub directory as well as adding the sub directory to the INCLUDEPATH. $$PWD will apparently give the current directory of the .pro file. But there is no change after doing this.
INCLUDEPATH += $$PWD/thirdparty
QMAKE_CXXFLAGS += -isystem $$PWD/thirdparty/
The warnings are easy to reproduce by the simple hello world example below:
#include <QCoreApplication>
#include <QDebug>
#include "json.h"
using namespace nlohmann;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "hello world";
return a.exec();
}
[EDIT]
Still hoping for some help. From the comments I have tried wrapping the inclusion, which didn't work, with what is seen below:
#pragma warning(push, 0)
#include "json.h"
#pragma warning(pop)
I am using qmake but thought that maybe gcc solutions might apply, seems they don't.
Any help would be greatly appreciated.
Original one : QFile file("/home/casper/QuickRecorder/about_content.txt"); (Work)
I have tried :
"about_content.txt"
"/about_content.txt"
"~/about_content.txt"
"./about_content.txt"
"QuickRecorder/about_content.txt"
"/QuickRecorder/about_content.txt"
"~/QuickRecorder/about_content.txt"
"~/QuickRecorder/about_content.txt"
No one works.=[
My Questions
What path can I use?
If I register the file "about_content.txt" into Resource, how can I read it into text browser?
The following is the entire code :
About::About(QWidget *parent) :
QDialog(parent),
ui(new Ui::About)
{
ui->setupUi(this);
this->setFixedSize(this->width(),this->height());
QFile file("/home/casper/QuickRecorder/about_content.txt");
if ( !file.open(QIODevice::ReadOnly) )
QMessageBox::information(0, "Error", file.errorString());
QTextStream content(&file);
ui->aboutContentBrowser->setText(content.readAll());
}
Reference : QT C++ GUI Tutorial 27- How to read text file and display file to a textbrowser or textEdit
Thank you for your help.
If you place your file about_content.txt inside build directory you can use:
about_content.txt
./about_content.txt
If you must open file right from this path /home/casper/QuickRecorder/about_content.txt
Use absolute path as you wrote in question.
If you want use relative path
Take a look at differrence of relative path vs absolute path. So if you place your file for example to /home/casper/QuickRecorder/about_content.txt and you know exactly that your build lirectory is /home/casper/app-build -- you can use relative path ../QuickRecorder/about_content.txt
Take a look at QDir class. It already consists lots of useful methods.
If you want to place your file to resources
That procedure is pretty simple. Just add resource you your project and add file to your resource according to The Qt Resource System. For example, you can add your file about_content.txt to .qrc file and use it in your program this way: QFile file(":/about_content.txt");.
You can get the path of the user's home directory using the static function QDir::homePath().
QString homePath = QDir::homePath();
QFile file(homePath + "/QuickRecorder/about_content.txt");
...
I have very basic QT application (just create to explain my problem).
So here I go :) I have two folders, f1 and f2, and they are on same level (have same folder for parent). In f1 I have source code from my project, and in f2 another project.
For sake of this example, let's say that in f1 I have only common.h and common.cpp, and in f2 I have:
Test.pro
SOURCES = main.cpp
INCLUDEPATH += "..//f1//"
main.cpp
#include <common.h>
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug()<<CalculateMD5("test");
}
When I try to build this project (Test.pro) I get following error:
f2/main.cpp:7: undefined reference to `CalculateMD5(QString)'
What am I doing wrong ? How should I include code from another project ?
I need CalculateMD5 function to be global.
Here you can download whole example (1kb):
http://www.xx77abs.com/test.rar
Thanks !!
For the simplest change, I think you need to change your .pro file to this:
Test.pro
SOURCES = main.cpp ../f1/common.cpp
INCLUDEPATH += "..//f1//"
If you have other code that will also be linking in common.cpp, then a better change would be to make a .pro file in f1 that generates a library, to be linked in to other applications.
But just manually adding common.cpp to your list of sources should do the trick.
I am using Qt 4.5 in Windows XP. My pro file has the variable VERSION = 1.0. Now i need to read this variable and get its value (1.0) from the source code. So that I don't have to maintain another variable for version inside my source code. I just read from the pro file and update it. So that the value remains consistent all over my project. Is it possible? Any pointers regarding this are welcome..
Use somethings like this:
DEFINES += VERSION=\\\"$$VERSION\\\"
This will define a macro that you can use in C source code. Get rid of the backslashes and quotes if you want a number, not a string.
I'll elaborate on this a bit.
In the YourApp.pro:
VERSION = 0.0.0.1
DEFINES += APP_VERSION=\\\"$$VERSION\\\"
In the main.cpp:
#include <QApplication>
QCoreApplication::setApplicationVersion(QString(APP_VERSION));
Wherever else in your sources, e.g. in the imaginary controller.cpp:
#include <QApplication>
QString yourAppVersion = QCoreApplication::applicationVersion();
somehow, when I tried qDebug() << QString(APP_VERSION); in a class.cpp not in main.cpp. has an error "C2065" APP_VERSION: undeclared identifier". but when I tried in main.cpp that worked.