Using Qt with eclipse, #include <QSqlDatabase> leads to file not found - c++

So I have started a new Qt console project;
everything so far has been working fine. I have eclipse set up with the integration, but i'm not actually using it, i have just included the relevant Qt directories so that my #include <QtGui> calls work.
These have all worked perfectly so far.
i can compile it either using eclipse's build option, or by doing
qmake -project
qmake
make
from the command line. Both work.
the include statements i have; are:
#include <QtGui>
#include <QApplication>
In two different files.
If i add anotherone though, i.e. #include <QSqlDatabase> then eclipse recognises the include, and lets me initialise new variables, and use the QSqlDatabase classes. Everything seems fine - see here - eclipse screenshot
When i build the project though, it decides it can't see the files even though they are all there, see them here in the terminal too. And then eclipse puts red lines everywhere.
If i go into the directory, and attempt to do it all via the terminal, using the qmake -project; qmake; make then i get the same errors.
If however, I go into the .pro file created by qmake -project, and add the line QT += sql, and then do qmake; make, then all is fine and it works.
Why is qmake -project leaving out these files from the project file? I'd really rather not have to go in and manually add that line every time I build it...

Why is qmake -project leaving out these files from the project file?
As far as I know, qmake isn't supposed to replace human programmer. If you want to know WHY it doesn't detect SQL dependency, you can read qmake source code.
I'd really rather not have to go in and manually add that line every time I build it...
It sounds like you're trying to do something weird here. You don't need to call qmake -project before every build.
qmake -project generates initial project - *pro file. Once initial project has been generated, you can tweak it, and add options or some macro magic if you want. Then, using *.pro file, you generate script for your build system (vcproj for windows, makefiles for windows/*nix, nmake make files, etc).
Workflow goes like this:
qmake -project.
Repeat forever:
2.1. modify generated *.pro file to your liking.
2.2 qmake
2.3 Build using your build system. (nmake, make, visual studio, etc.)
To simply "build" all you have to do is #2.3. You call qmake (without -project) switch only if you modified *.pro file.
See qt documentation and tutorials for more info.

Related

Codelite not recognizing ns-3 files

I'm pretty new with C++ development, and I wanted to use an IDE. I downloaded codelite, and everything was working fine.
The issue
Recently I started development an App for the NetworkSimulator3 or Ns3, that runs with Waf(https://waf.io/). Using the codelite just for my app, and doing the build&Run on the terminal with waf seems wrong.
In the end, codelite is not recognizing a butch of files of ns3, and keep pointing errors.
What I tried to do
I tried adding all Ns3 files in a single workspace, together with my app files, in hopes that everything would work. Instead I still got a bunch of "No such file or directory" errors, although these files exist and are added on the IDE
On the import, Ns3 demands us to call it like this:
#import <ns3/packet.h>
Although the .h is inside another folder:
src/network/model/packet.h
There are a few wscripts files that probably work on these bindings, although I'm not 100% sure.
So I also tried:
Adding the .h in the same folder of my app
Creating a new virtual folder called ns3, and adding the headers there
Including the folder with the headers in the compiler's include paths
Including the folder with the headers in the linker like it was a library
Nothing worked so far
I just wanted to open the Ns3 on an IDE so I could work without several errors showing. Maybe even with some autocomplete?
Could you guys help me figure this out?
Edit
I also added a "Custom build", so I can execute and build my project using waf, but the IDE errors didn't disappear
On the import, Ns3 demands us to call it like this: #import <ns3/packet.h> Although the .h is inside another folder: src/network/model/packet.h
Headers imported with <ns3/header.h> are copied from src/module/(model/helper) to build/ns3 when you build ns-3. You need to add build/ as an include folder (e.g. -I/path/to/ns-3-dev/build), which will make <ns3/header.h> work correctly.
Not sure on the other issues, since I've never tried CodeLite.
I just wanted to open the Ns3 on an IDE so I could work without several errors showing. Maybe even with some autocomplete?
If I may recommend, try the CMake buildsystem. Clone the code and create a cmake cache folder (e.g. mkdir cmake_cache), then generate the CodeLite project (e.g. cd cmake_cache && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -G "CodeLite - Ninja" .., or -G "CodeLite - Unix Makefiles") and then open the project in cmake_cache with CodeLite. :)
Update: just read on their blog that exporting compile commands is required for code completion when using cmake.
https://codeliteide.blogspot.com/2014/02/codelite-ide-60-cmake-and-clang-code.html

How do I import my makefile project into QtCreator?

I have previously written a large C++ server and client project (containing multiple .cpp's and .h's which run with makefile). Now I want to modify the client to use a graphical user interface.
Should I import all of my codes to Qtcreator and compile them all in the software? How can I implement the makefile then?
Next, how can I modify my client so that the interfaction with the server is controlled from the graphical interface?
Create a Qt project (I guess, you need Qt Widgets application). Copy your existing files into project's directory. Import all these files to your project. Then look what you have to do in main() function. In Qt project it will be in the file main.cpp, and a minimal code for main() will be automatically created by Qt Creator. So, move the necessary code from your main() function there. Then bit by bit look which parts of your code will interact with Qt. Makefile will be created by Qt Creator. Files mainwindow.cpp and mainwindow.h relate to your main window's GUI.
QtCreator supports working with makefile projects. Basically it becomes a glorified text editor and directly invokes make for your project directory. Some features, like compiling a particular file might not work. See this SO question for more details. And then one can manually link/include Qt for your application's GUI.
Project configuration that is natively supported by QtCreator are qmake files. It converts directly into makefiles. This will provide the best QtCreator experience. So another option would be to convert your makefiles into qmake .pro/.pri files. This will require some effort.
I digress, but if you are going to go this way, I can also recommend CMake. QtCreator has on okay (and constantly improving) support for CMake. So do some other IDEs (CLion, VS). And you will always have the option to convert your CMake files into makefiles, Ninja files, VS solution, or even XCode project.

Can a project created with qmake be compiled without using qmake?

I have some source code that was built with qmake. Compiling it is supposed to be done with qmake qmakefile.pro.
I'd like to compile it without using qmake. Is that even possible? What would the steps be?
Edit: Here's the .pro file.
Yes, you can compile files without qmake.
You need to get any specific compiler settings from qmake and convert them to the other compiler as necessary.
Or you can convert the contents of the qmake.pro file into a makefile or your IDE's project settings.
If the qmake.pro is not a human readable text file, you will have to launch qmake and retrieve the settings. Otherwise search for a conversion tool.
While it is technically possible to translate the .pro file into something else, I would not advise it.
Why?
QMake does a lot of work behind the scenes.
In the case of the .pro file you referenced (http://pastebin.com/viv204Fv), QMake is invoking meta-tools (uic, moc) to create the code for the UI and adding build rules to compile it. You will need to manually code those in your build script.
-. QMake also solves issues of choosing the right compiler and linker options (including the right include files). You will need to determine them yourself and add them to your build script.
The project is already using Qt, so why not use Qt's build script generating tool, QMake? If you have installed a Qt development environment, you should already have qmake installed.

QMediaPlayer, QSound class - unable to #Include files (files are there)

I have been trying to play a simple audio file using:
http://qt-project.org/doc/qt-5/qmediaplayer.html and
http://qt-project.org/doc/qt-5/QSound.html
I am using Qt 5.3 but the problem is, when I type #include QMediaPlayer or QSound, QtCreator keeps underlining them with red because it can't find them. The thing is, the files are there. They are located in QtMultimedia/ and I have seen them. When I try to prefix QtMultimedia/QMediaPlayer or QtMultimedia/qmediaplayer.h it still can't find them.
If I type #include "QtMultimedia/" the auto-complete drop down in the text editor only shows QtMultimedia/QtMultimediaDepends. Normally you get a list of all the files in the folder but not with this one.
Looks like there is something wrong with the paths. How do I fix them? I am using a Windows machine.
Edit: I have included project settings - see screenshots.
I think, after you modified your .pro file, You didn't run the qmake.
build-> run qmake
qmake will generate the makefile you need to build the src code.
if you only change the .pro file, but didn't run it. Nothing is changed.
and that is reason, you create a new project and it is working.
Ok I figured it out. It's a problem with my Qt version.
My Qt 5.3 version is a custom compiled one. When I select the included version that came with the installer, QSound is found. When I change the kit to the custom compiled one, the file cannot be found. I must have forgotten to include some sort of multimedia option to the configure script during compilation.
Now the question is what is the missing option, I will need to do more reading...
Of course, comments and suggestions are welcome :)
You have to add the word 'multimedia' in your .pro file:
QT += core GUI
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets multimedia

How to automate Qt moc?

I have to run the following commands from Qt command prompt: qmake -project then make and this gives me the debug folder with the Moc file.
This is strangely the only way my PC will generate the moc_.cpp file.
So how can I automate the task of these commands so I don't have to use these commands again?
You should not run qmake -project multiple times. The -project option is meant to provide you a template project file for you to edit. An equivalent of what you are doing in an IDE would be creating a "New Project" every time you want to build. After you have the initial project, you should edit it manually, add files when you have new files, etc. If some header file changes, the generated Makefile will notice it and call moc on it to update the moc_*.cpp file automatically. So:
Run qmake -project when you start working on a project.
Run qmake when you want to generate Makefiles.
Run make when you want to build the project.
I guess you have two choices.
call qmake from a parent make process and do a multilevel build. ("Recursive make".)
directly run the meta-object compiler from rules in your makefile
If the second, this page on using the meta-object compiler may help.