Qt c++ Add module - c++

I have a problem with c++ and Qt.
I am working on a Linux Mint system and I installed Qt in my user folder. This results having the include folder here:
/home/username/Qt5.3.1/5.3/gcc_64/include/
Now, whenever I try do run the commands:
qmake -project
qmake
make
I get an error for the modules, it says for example:
QApplication: No such file or directory
I tried a workaround by adding the line Qt+=Widgets in the .pro file but this did not work.
So I manually copied QApplication from:
/home/username/Qt5.3.1/5.3/gcc_64/include/QtWidgets
to its parent folder:
/home/username/Qt5.3.1/5.3/gcc_64/include/
and this worked, but it is kinda "brutally" forced.
How could I solve this?
QT += core gui opengl widgets
TEMPLATE = app
TARGET = cw
INCLUDEPATH += /home/username/Qt5.3.1/5.3/gcc_64/include/

Try to include path, by adding INCLUDEPATH += <your path> in your .pro project file.
In project file you should add Qt+=widgets instead of Qt+=Widgets.
Add #include <QtWidgets> on top of your main.cpp.

Related

Where do I put the QT5 clone in this project

This is my first time every using C++ so please go easy on me. I have about 7000 hours of Python experience so I'm not completely clueless. I'm trying to read the code written for the Collatinus software found here. collatinus. It seems that the initial file is this:
VERSION = "11.2"
DEFINES += VERSION=\\\"$$VERSION\\\"
DEFINES += MEDIEVAL
TEMPLATE = app
TARGET = collatinusd
INCLUDEPATH += . src
DEPENDPATH += .
DESTDIR = bin
OBJECTS_DIR= obj/
MOC_DIR = moc/
QMAKE_DISTCLEAN += $${DESTDIR}/collatinus
CONFIG += console
CONFIG -= app_bundle
CONFIG += release_binary
QT += core
QT -= gui
QT += xmlpatterns
QT += network
I've downloaded qt from qt and got the open source qt5. Now I cannot figure out where to put the file. I have tried putting it in the same folder as the above mentioned code. I have also tried putting in the folder marked src. In the src folder there are many files which use QT but it seems like the files are supposed to be taken out of the QT folder. For example in this file we have the syntax on line 28
#include <QDebug>
The QDebug file is in the QT folder. But when I put the qt folder in either the topmost folder or the src folder I get the error message:
fatal error: 'QtCore' file not found
#include <QtCore>
So the file structure is as follows:
/collatinus-daemon
collatinus.pro (and other files)
//src
flexion.cpp (and other files)
So where do I put the qt folder? Also, I renamed it qt from qt5 since the syntax had the line:
QT += core
###################
Ok, I've got the QT creator up and running. Here is the pro file
QT += network widgets
QT += core
QT -= gui
TARGET = Client_C11
VERSION = "1.0"
#CONFIG += console
#CONFIG -= app_bundle
CONFIG += release_binary
TEMPLATE = app
SOURCES += src/client_main.cpp
OBJECTS_DIR= obj/
MOC_DIR = moc/
unix:!macx:DESTDIR = bin
macx:{
# Commandes spéciales pour déployer l'application sur Mac.
# J'ignore s'il faut l'équivalent pour Linux ou Windows.
# Philippe. Octobre 2016
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.8
ICON = collatinus_bw.icns
deploy.commands = macdeployqt Client_C11.app
QMAKE_EXTRA_TARGETS += deploy
}
Here are some screenshots of my folders:
And here is the error message I'm getting:
Here also is the code for the client main
#include <QCoreApplication>
#include <iostream>
#include <QtWidgets>
#include <QtNetwork>
class QTcpSocket;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString req = "";
if (argc > 1)
{
int i = 1;
while (i < argc)
{
QString suite(argv[i]);
req += " " + suite;
i++;
}
}
else req = "-?"; // pour afficher l'aide.
QTcpSocket * tcpSocket = new QTcpSocket();
tcpSocket->abort();
tcpSocket->connectToHost(QHostAddress::LocalHost, 5555);
QByteArray ba = req.toUtf8();
tcpSocket->write(ba);
tcpSocket->waitForBytesWritten();
tcpSocket->waitForReadyRead();
ba = tcpSocket->readAll();
tcpSocket->disconnectFromHost();
tcpSocket->close();
QString rep(ba);
std::cout << rep.toStdString();
a.quit();
}
I don't understand what I'm supposed to do with qmake and cmakelists
Where do I put the QT5 clone in this project
You don't :)
Qt installations up to 5.14 are not relocatable. That means that once Qt is installed, if you move it to another path, it'll break. Just don't mess with it: once installed, you leave it alone, and it'll work just fine.
Qt source code has to be built before it can be used. I presume that you downloaded the source code and want to stick it into the project and build the two that way. It's not designed to work that way at all.
Qt build requires several other tools to be installed (iirc python, ruby, perl), and other optional dependencies to get the full feature set, and if it fails for whatever reason, figuring it out is unnecessary effort initially. So it's best to start with pre-built Qt.
I've downloaded qt from qt and got the open source qt5. Now I cannot figure out where to put the file.
Downloading "qt" doesn't mean much, since everyone means something else by that. If you've downloaded the source code, then delete it - last thing you want is to mess with compiling Qt right now.
Normally, Qt is installed using an installer, so what you'd download is an executable installer, and use that to select the Qt components you want to install. On Windows, pick the mingw-based Qt version, since that also installs the build environment (compilers) if you don't have them already. Otherwise you'd use the MSVC-based version if you got MSVC installed on Windows.
On Unix, you'd want to install Qt using the "native" package manager - one that came with your linux distribution, or macports (really preferred) on MacOS.
Once Qt is installed, you'd use Qt Creator IDE to open the collatinus project (its .pro file). And everything will "just work" from that point onwards - it'll let you build it and run whatever executable targets it produced. Of course you can build from the command line, but for a beginner it's just an extra layer of complication and unnecessary.
In any case, the .pro file you refer to would be processed by qmake to generate the build system that builds the project. qmake itself is the means you use to select what Qt version you build with: there's one qmake per each Qt installation. So, after qmake has ran, you'd make the thus-configured build, and there'll be no problems with finding Qt headers. It's the job of qmake in that case to set everything up so that the compiler will be told where to find Qt. You are not expected to have to mess with it manually.
If there's a CMakeLists.txt file in the project, you'd probably prefer to use that instead of qmake, since cmake is a widely used tool with lots of knowledge available online, whereas qmake is now obsolete. Still, older projects may only supply a .pro file that needs to be used with qmake and not cmake.
Your question doesn't nearly provide enough detail for a more focused answer - please tell us exactly what you did, and what Qt elements you installed (whether using Qt Installer program, or using a unix package manager).

UI_project.h not found for QT project

I made a QT GUI project in VS 2019. It ran perfectly through VS. Copied the entire project files directory to Linux partition. Ran qmake -project in the directory containing the 'test2.sln' file to create a 'test2.pro' file. Opened the '*.pro' file through QT Creator. It imported everything fine. But, when building it it errors out saying "ui_test2.h file not found".
The test2.pro contains:
TEMPLATE = app
TARGET = test2
INCLUDEPATH += .
DEFINES += QT_DEPRECATED_WARNINGS
HEADERS += test2/test2.h test2/x64/Release/uic/ui_test2.h
FORMS += test2/test2.ui
SOURCES += test2/main.cpp test2/test2.cpp test2/x64/Release/rcc/qrc_test2.cpp
RESOURCES += test2/test2.qrc
What should I do to fix this?
1) remove folder with build (rm -r nameOfFolder)
2) rename your UI file (test2.ui -> mainwindow.ui) for example
3) check out name of own class and name of qt class in UI file (you can open it via vim or nano)
After that try to rebuild your project!
I hope you'll have done this with good results!

Qt 5.3. QtWidgets: No such file or directory #include <QtWidgets>

I want to compile Qt example. I get error QtWidgets: No such file or directory #include
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets - does not help
QT += widgets - does not help
INCLUDEPATH += /opt/Qt/5.3/Src/qtbase/include/ - does not help
Qt 5.3. Ubuntu 14.04 x64.
You need to double check that you completed all these steps:
Module installed
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
You re-run the Qt 5 qmake.
Having said that, I would like to remind you that including the whole module is not a good idea as it includes all the widgets related things. Try to narrow it down to the headers that you really need.
As you noticed Qt directory structure changed between Qt4 and Qt5. QWidget header moved to a QtWidgets directory. Try adding
INCLUDEPATH += /opt/Qt/5.3/Src/qtbase/include/QtWidgets
If that does not help try finding the header manually using
find /opt/Qt/5.3/Src/qtbase/ -name QWidget
and and the directory it is in to INCLUDEPATH
Edit based on comment from Final Contest.
I agree that workarounds usually are a bad idea. To test where QT your installation looks for qt5 headers and libraries. Create a minimal project.
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget w;
w.show();
app.exec();
}
Generate a project and add QT += widget
/opt/Qt/5.3/Src/qtbase/bin/qmake -project
Project file
######################################################################
# Automatically generated by qmake (3.0) Thu Jul 10 13:05:17 2014
######################################################################
TEMPLATE = app
TARGET = so_qtwidgets
INCLUDEPATH += .
QT += widgets
# Input
SOURCES += main.cpp
Generate a make file
/opt/Qt/5.3/Src/qtbase/bin/qmake
The interesting parts widget flag adds:
In my case -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui to INCPATH
-DQT_WIDGETS_LIB to DEFINES variable.
-lQt5Widgets -lQt5Gui to libs.
The only part which should differ is the paths to QtWidgets and QtGui. If these a wrong the I would try reinstalling Qt.
Check what your .pro file looks like before you run "make". I found that the command "qmake -project" auto generated a .pro file that caused this same error. I now compiled my qt project via the following commands and the error went away:
qmake my_project.pro
make
This all looks very much like the wrong way round and I did have the same problem temporarily with 5.6 but the answer could be a whole lot simpler.
If you're loading a lot of examples you may arrive at the editor or whatever you were at last, first. If the example's been loaded for the first time it'll need to be 'configured' which is under the projects side-tab which should present you with 'Configure' rather than 'Build & Run'. That it doesn't always jump straight there is a flaw, but then so's the inclusion of examples with no support by default (Desktop OpenGL and iOS for two).
Until that's done it'll not resolve any dependencies outside the immediate project as the libraries used depend on which compiler/target is used (eg. MSVS, GNUCC, MinGW, 32/64bit).

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.

how to add zlib to an existing qt installation

How can I add zlib to an existing installation of Qt. I m pretty new in this so please give me detailed description!
Thanks for your help in advance!
zlib is contained in the core Qt libraries. If you want to use the zlib functions in a Qt program, you only have to include zlib.h which is in src/3rdparty/zlib. See e.g. the implementation of QByteArray in src/corelib/tools.
If you want to use quazip, just add the library to your project. It is based on the Qt libraries. Take care to build the correct qyazip library that corresponds to your Qt installation.
You get the correct include path by adding the following line to your project file:
INCLUDEPATH += $$[QT_INSTALL_PREFIX]/src/3rdparty/zlib
For Qt5, see Thorbjørn's comment: it is sufficient to use #include <QtZlib/zlib.h>.
The current answer is only valid for Qt4. Since Qt5 the zlib header file is stored in a different directory. Using the qmake property QT_INSTALL_HEADERS you can add to your .pro file:
INCLUDEPATH += $$[QT_INSTALL_HEADERS]/QtZlib
This works e.g. to build quazip, if you add it to quazip.pro
The property $$[QT_INSTALL_HEADERS] points to QTDIR/qtbase/include/ within which lies QtZlib/zlib.h.
Without changing the includepath you have to change every include-statement to #include <QtZlib/zlib.h> as commented by Thorbjørn.
If you want to use zlib for compression/uncompression, use qCompress/qUncompress.
At least some people here want to build Quazip, which requires zlib.
Here's how I did it on windows with quazip 0.4.3.
First in the quazip.pro I changed SUBDIRS to contain only:
SUBDIRS=quazip
Then I downloaded zlib binaries and source from:
http://www.winimage.com/zLibDll/zlib125dll.zip [binaries]
http://www.winimage.com/zLibDll/zlib125.zip [source]
both links came from http://zlib.net
Then in the subfolder quazip/quazip.pro I added:
INCLUDEPATH += <path to zlib source>
in the win32 {} section I commented this line:
# *-msvc*: QMAKE_LFLAGS += /IMPLIB:$$DESTDIR\\quazip.lib
and I modified the LIBS line to this:
*-msvc*: LIBS += -lzlibwapi -L<path to zlib binaries>/dll32
I also modified in zip.c and unzip.c the
#include "zlib.h"
to become:
#include <zlib.h>
After that I build this to Release mode and got a DLL out.
Then in the project to use this, I added the following config:
INCLUDEPATH += <quazip source path>
INCLUDEPATH += <zlib source path>
LIBS += -L<quazip source path>\quazip\release -lquazip
And that builds and works, but only in Release mode for the test app. In Debug mode i get assertion errors and it fails.