Qt auto generated form provides wrong path to source code files - c++

I have added qcustomplot.h/.c files to my Qt project. They are located in: "QT_PROJECT_DIR/QCustomPlot/".
Every time I use the designer in Qt Creator and build I get this error in ui_mainwindow.h:
error: ../../qcustomplot.h: No such file or directory
#include "../../qcustomplot.h"
This is of course true as it is located in: "QT_PROJECT_DIR/QCustomPlot/"
How do I change how Qt Designer auto generate this path?
If it helps, here is my .pro:
#-------------------------------------------------
#
# Project created by QtCreator 2014-01-12T00:44:44
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets widgets opengl printsupport
TARGET = Test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
QCustomPlot/qcustomplot.cpp
HEADERS += mainwindow.h \
QCustomPlot/qcustomplot.h
FORMS += mainwindow.ui
CONFIG += mobility
MOBILITY =

Your include seems to be wrong:
#include "../../qcustomplot.h"
Since you do not add the QCustomPlot folder to your INCLUDEPATH, you would need to include the file as follows:
#include "QCustomPlot/qcustomplot.h"
Alternatively, you could change the INCLUDEPATH as follows:
INCLUDEPATH += $$QT_PROJECT_DIR/QCustomPlot/
and then you could include the header without the ugly ../../ relative reference as follows:
#include "qcustomplot.h"
It is a rule of thumb to avoid such relative include paths in the source and headers files themselves because the structure can change at any time, or may be different on a different machine, et al. It is better resolved by the buildsystem and include paths.

I met the same problem and have solved it.
It was caused by the path of qcustomplot object in xxx.ui.
Step 1: Open the ***.ui file which contains the qcustomplot item.
Step 2: Right click the qcustomplot item
Step 3: Double click the path of QCustomPlot, then change it to QCustomPlot/qcustomplot.cpp from ../../customplot.
Finally, the header of ui_***.h can auto include QCustomPlot/qcustomplot.cpp, instead of ../../qcustomplot.h.
My native language isn't English. If you find grammar mistake in my reply, please inform me. Let me correct it.

Related

How do you link libraries in Qt?

I have a project that I've written in VS2017 that has a lot of static libraries and I've got to the point where I want to start refining the gui. To make sure I can use Qt I made a test subdir program using the tips in https://www.toptal.com/qt/vital-guide-qmake, the https://wiki.qt.io/SUBDIRS_-_handling_dependencies example, the https://github.com/rainbyte/example-qmake-subdirs and https://github.com/219-design/qt-qml-project-template-with-ci/tree/6af6488d74e1dc97a8cef545080074055a696e9a projects as well as several of the similar questions here on SO.
This consists of a subdir project called "project" with 2 sub-projects "app" and "library". These are both simple QtApplications created using the New Subproject wizard, with all the files except for main.cpp removed from "app", everything left in except for main.cpp in "library", the point being to make sure mainwindow.h in "library" is referenced from main.cpp in "app". Despite trying every example I can find I'm still getting the "'mainwindow.h' file not found" error.
According to all the examples I could find you should only need to add a few lines (#) to the wizard-produced .pro files and add an additional .pri file to the "library" project;
Directory structure;
project/
project.pro
app/
app.pro
main.ccp
library/
library.pro
mainwindow.h
mainwindow.ccp
mainwindow.ui
library.pri
project.pro;
TEMPLATE = subdirs
TARGET = project ##
SUBDIRS += \
app \
library
app.depends = library ##
app.pro;
TEMPLATE = app ##
TARGET = app ##
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
SOURCES += \
main.cpp
include(../library/library.pri) ##
library.pro;
TEMPLATE = lib ##
TARGET = library ##
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
CONFIG += shared ##
CONFIG += lib_bundle ##
SOURCES += \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
DISTFILES += \
library.pri ##
and the added library.pri file;
LIBTARGET = library
BASEDIR = $${PWD}
INCLUDEPATH *= $${BASEDIR}/include
LIBS += -L$${DESTDIR} -llibrary
I know this has been asked many times already but I can find no definitive solution, or rather no-one seems to have bothered to post their working solution. I'd really appreciate any help with this, I'd love to be able to use Qt and escape the purgatory of win32 ui creation.
You have this:
INCLUDEPATH *= $${BASEDIR}/include
But you don't have directory named include anywhere, it seems. So probably remove the /include part from above.

QGIS cannot find headers file

I'm trying to create a custom QGIS desktop application with Qt 5.9.1
I have downloaded OSGeo4W (x64) geospatial software set and the Github C++ QGIS samples but I'm facing an error at compile time:
Cannot open include file: 'qgsmapcanvas.h': No such file or directory
Actually none of the above headers files are under OSGeo4W folder:
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgssinglesymbolrenderer.h>
#include <qgsmaplayerregistry.h>
#include <qgsrasterlayer.h>
#include <qgsmapcanvas.h>
But..shouldn't they be in dll libraries (eg qgis_core, qgis_gui ... ) ?
Anyway, this is my *.pro file:
TEMPLATE = app
TARGET = qgis_example4
QT = sql network svg gui core xml
LANGUAGE= C++
INCLUDEPATH += "C:\\OSGeo4W64\\include"
LIBS += -L"C:\\OSGeo4W64\\apps\\qgis\\bin" -lqgis_core -lqgis_gui -lgdal_i -lgeos_c
DEFINES+=CORE_EXPORT=__declspec(dllexport)
DEFINES+=GUI_EXPORT=__declspec(dllexport)
CONFIG += qt gui exceptions stl warn_on debug thread
RESOURCES += resources.qrc
FORMS += mainwindowbase.ui
HEADERS = mainwindow.h
SOURCES = main.cpp \
mainwindow.cpp
PS: there is a similar question on stackiverflow but I repeated it because the answers given did not give the solution
Solved: the needed headers file could be found within the "qgis-dev" branch of the project.
OSGeo4W : For users that cannot use OSGeo4W for some reason or just prefer it there is also a weekly snapshot of qgis-dev from OSGeo4W as standalone installer
After the installation you can find header in:
C:/Program Files/QGIS 2.99/apps/qgis-dev/include
So this is my .pro file
TEMPLATE = app
TARGET = qgis_example4
QT = sql network svg gui core xml
LANGUAGE= C++
INCLUDEPATH += "C:/Program Files/QGIS 2.99/include"
INCLUDEPATH += "C:/Program Files/QGIS 2.99/apps/qgis-dev/include"
LIBS += -L"C:/Program Files/QGIS 2.99/apps/qgis-dev/bin" -lqgis_core -lqgis_gui
DEFINES+=CORE_EXPORT=__declspec(dllexport)
DEFINES+=GUI_EXPORT=__declspec(dllexport)
CONFIG += qt gui exceptions stl warn_on debug thread
RESOURCES += resources.qrc
FORMS += mainwindowbase.ui
HEADERS = mainwindow.h
SOURCES = main.cpp \
mainwindow.cpp
Although the files are now present, I still not able to run QGIS samples. Maybe I'll move to other sample or I'll do my custom basic application.

Opencv sample in Qt crashes immediately with the error 'The program has unexpectedly finished'

I tried to create a simple Opencv application in Qt and upon running, the whole application is crashed. If I comment out the opencv related codes from the project, the project runs just fine.
What I did:
Downloaded the Qt 5.5.0 VS2013 64bit version
Downloaded the OpenCV 3.0
Downloaded the Cmake 3.3.2
There were already prebuilt binaries in when I extracted the Opencv package, But all tutorials on the net wanted me to recompile the source codes so did I. The package contains:
build
sources
created a new folder named mymade to hold the binaries next to the other two directories. So it now looks like this:
build
mymade
sources
Steps:
Fired up CMake, specified the source folder from the extracted files
and specified mymade as the output for binaries.
checked all options that had a opencv in their names, plus Qt! and configured it and subsequently generated the files.
This is the resulting contents:
So all is done and I now need to build the binaries. I opened OpenCV.sln and compiled the release and debug binaries. The dlls are placed inside bin directory, and the lib files are placed inside lib folder.
Now it's the time to configure the Qt projects .pro file, so I used the header files from build directory, and for libs I used the lib folder from mymade folder. This is the first configuration that I came up with, which compiles without any linker issues, but crashes just immediately.
The initial changes in project file :
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-06T14:04:20
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = OpenCVTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
INCLUDEPATH += L://Apps_Installation_Folder//opencv//build//include
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//*.lib
FORMS += mainwindow.ui
Doing this in project file as it was suggested by answers like this didn't do any good either:
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-06T14:04:20
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = OpenCVTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
INCLUDEPATH += L://Apps_Installation_Folder//opencv//build//include
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_core300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_highgui300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_imgcodecs300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_ml300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_objdetect300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_photo300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_shape300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_hal300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_flann300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_features2d300.lib
FORMS += mainwindow.ui
This fails as well:
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-06T14:04:20
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = OpenCVTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
INCLUDEPATH += L://Apps_Installation_Folder//opencv//build//include
LIBS +=-L"L://Apps_Installation_Folder//opencv//mymade//lib//Release"
LIBS += -lopencv_core300 -lopencv_highgui300 -lopencv_imgcodecs300 -lopencv_ml300 -lopencv_objdetect300 -lopencv_photo300 -lopencv_hal300 -lopencv_shape300 -lopencv_flann300 -lopencv_features2d300
FORMS += mainwindow.ui
So what is the problem here?
Update
As it was pointed out by #Miki, Qt couldn't find the opencv dlls (the configs above were OK but), And to solve this problem, one only needs to add the directory containing dlls to the PATH variable, there you are good to go and everything should be fine now.
From the comments to question it turns out that the program didn't find OpenCV dll.
You can:
Copy the required dll in the same folder of the .exe
Add the folder containing the dll to the PATH. You need to restart Qt Creator for changes to take effect.

Qt Including Files in Different Directories

So I've read a few posts and articles about how to go about doing this and one option was to do:
#include "../file/file.h"
But when I use this method, it still cannot find the file. The other way was to add the root of the project to the include path.
So here is an example directory:
main_dir
- dir2
-dir3
-header.h
-dir4
-dir 5
-source.cpp
So if I am trying to include header.h from source.cpp, how do I do this?
My .pro file
#-------------------------------------------------
#
# Project created by QtCreator 2015-02-17T12:52:00
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = project1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
dir4/dir 5/source.cpp \
HEADERS += mainwindow.h \
dir2/dir3/header.h
FORMS += mainwindow.ui
There are many ways to solve the problem.
Use #include "../../dir2/dir3/header.h" in source.cpp.
If you can add main_dir to the list of include directories, (-I<main_dir_path>), then you can use #include "dir2/dir3/header.h" in source.cpp.
If you can add the toplevel directories under main_dir -- main_dir/dir2 and main_dir/dir4 -- to the list of include directories, then you an use #include "dir3/header.h" in source.cpp.

QtCreator unable to find Qxt headers on Linux?

I'm working on a Qt5/QtQuick/QML application that's supposed to use QxtGlobalShortcut for hotkey control when the application is hidden or out of focus. I've been developing it using QtCreator on Linux, and I'm not entirely familiar with Linux development so I may have missed something simple.
First off, I tried cloning the master branch of the Qxt git repo from here. But for some reason ./configure failed to create a makefile according to an error that I was getting from make and make install. I'm not very experienced in building other people's projects/libraries from source, and the output I was getting from ./configure wasn't specific enough for me to figure out what was going on..
So instead, I decided to grab libqxt-dev from (X)ubuntu's APT repo. After it finished installing I added these lines to the bottom of my QtCreator project file:
INCLUDEPATH = usr/include
CONFIG += qxt
QXT += core gui
After setting up a basic global shortcut based totally off the Qxt documentation's example. I got a few compilation errors. First of all, the Qxt seem to use:
#include <QxtGlobalShortcut>
But QtCreator couldn't find that file, so I changed it to this:
#include <qxt/QxtGui/QxtGlobalShortcut>
Now it could find the Global Shortcut class header, and QtCreator stopped grumbling at me. Unfortunately, upon compilation QtCreator shot out this error message:
/usr/include/qxt/QxtGui/qxtglobalshortcut.h:28: error: qxtglobal.h: No such file or directory
I think this is caused by a problem in my project's INCLUDE path or something, but even having tried changing my projects INCLUDE path to this:
INCLUDEPATH = usr/include
usr/include/qxt/QxtCore
usr/include/qxt/QxtGui
CONFIG += qxt
QXT += core gui
I still get the same error message that QxtGlobal.h (which is being #included in QxtGlobalShortcut.h) can't be found..
So, I'm really not sure what I've done wrong, and I'm out of ideas about how to fix this.
Hopefully someone can help me understand what step I've skipped so that I can continue coding! Thanks!
EDIT: Here's what I have for my entire Qt project .pro file:
TEMPLATE = app
QT += qml quick
SOURCES += main.cpp \
Gamepad.cpp \
Script.cpp \
System.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
Gamepad.h \
Script.h \
System.h
LIBS += -L/usr/lib -lSDL2
CONFIG += qxt
QXT += core gui
Everything before LIBS was automatically generated by QtCreator for my project. I added the LIBS, CONFIG, and QXT elements as per the user instructions for SDL2 and Qxt.
This is a big problem here:
INCLUDEPATH = usr/include
1) You are using relative path from the current working directory, so not /usr/include from the root of the filesystem.
2) You are deleting everything in the INCLUDEPATH because you set rather than append with +=.
3) It is unnecessary to add that line anyway since /usr/include will be looked up by default.
4) Even if it was not, you have #include "qxtglobal.h" instead of #include <qxtglobal.h>.
I would suggest to delete that line and then it should just work.