Why doesn't qmake add "QT += widgets" to *.pro files automatically? - qt

I am trying to build a simple "Hello World" example in QT5.
I am compiling the code using the below steps
qmake-qt5 -project
qmake-qt5
make
I am getting the below error
main.cpp:1:24: fatal error: QApplication: No such file or directory
#include <QApplication>
^
compilation terminated.
When I read QT forums, its mentioned to add "QT += widgets" to the *.pro file. After doing this the code is compiled.
Question: Why do I need to add "QT += widgets" to the *.pro file manually ? Why doesn't qmake do it automatically ?
Note: I am using Ubuntu
Code
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}

The QT variable in the .pro file is used to specify the modules which are used in the project.
And as the qmake manual mentions:
By default, QT contains both core and gui, ensuring that standard GUI applications can be built without further configuration.
The Qt Widgets module is not linked by default and has thus to be specified in the .pro file with QT += widgets.

Related

Qt cannot include ui header file

I am try to build a simple qt project. For the project, the source file main.cpp is created using a general c++ editor, and the .ui file is created using qt design form, and they both are placed in the same directory. Then a .pro file is created, which reference both the main.cpp and .ui file.
The .pro file is here:
TEMPLATE = app
TARGET = gotocell
SOURCES += main.cpp
FORMS += gotocelldialog.ui
QT += widgets
And the main.cpp is:
#include <QApplication>
#include <QDialog>
#include "ui_gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Ui::GoToCellDialog ui;
QDialog *dialog = new QDialog;
ui.setupUi(dialog);
dialog->show();
return app.exec();
}
This follows the example code: gotocell1
But this line of codes
#include "ui_gotocelldialog.h"
causes the error: no such file or directory.
I am using QtCreator4.5. It is said that QtCreator will automatically generate the header files for the created ui how to create header file. What is going wrong here?
The generated source file is created and stored within your build folder. It does not get added to your project tree view as part of QtCreator. You can find your build folder directory by looking under Tools->Options->Build&Run.
I usually add the generated files to my own project tree view as I usually like to inspect it, but since is auto-generated from the ui file, it is not committed to version control.

How to install Box2D-qml with mingw32

My environment:
Qt 5.3.1 + Mingw32
Windows 7
I tired these:
Extract box2d-qml in C:\Qt\Qt5.3.1\5.3\mingw482_32\imports and rename it as Box2D
Open cmd and cd into Box2D's directory .
Input qmake in cmd, no output
Input mingw32-make, no error output.
Input mingw32-make install, no error output. and Box2D.2.0 was generated under C:\Qt\Qt5.3.1\5.3\mingw482_32\qml directory.
Then i create empty quick project and add import Box2D 2.0 into qml.
Moving the mouse cursor on import Box2D 2.0,QtCreator pop up a message:
But when i run it got error:
QQmlApplicationEngine failed to load component
qrc:///main.qml:3 plugin cannot be loaded for module "Box2D": ?v???O?C?? 'C:/Qt/Qt5.3.1/5.3/mingw482_32/qml/Box2D.2.0/Box2D.dll' ????? Qt ?????????????C?u???????g?p?????????B (?f?o?b?N???????[?X?????C?u?????????g?p???邱??????????)
The main Cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
You have 2 options to use the plugin:
Installing into $QTPATH/qml (or $QTPATH/imports) folder as system-wide QML plugin. In this case you just have to do next steps:
download the source from Github and open it with QtCreator (don't put it into system folder, put the source into some user folder)
Go to Projects tab / Run / Add deploy step
add install to Make arguments
go to Edit tab, right click on the project and select Run qmake
now build the project (don't forget to set Release profile)
right click on the project and select Deploy;
The plugin dll (include all you need, Box2D code etc) will be installed into qt folder.
.
Compiling the plugin as part of your project. In this case you just have to do next steps:
download the source from Github and put the plugin source folder into your project folder (myproject/qml-box2d/, for example)
add the line into your .pro file: include(qml-box2d/box2d_lib.pri)
add this code into your main.cpp file:
#include <box2dplugin.h> // <-- this line
int main(int argc, char *argv[])
{
...
Box2DPlugin plugin; // and these
plugin.registerTypes("Box2D"); // 2 lines
...
}
Rerun qmake and recomplile the project. Now you can use Box2D items in your QML files.

Qt linker errors Qmake + Makefile

I want to build a Qt project outside Qt Creator, so I'm using qmake to generate a Makefile on the following project file:
TEMPLATE = app
TARGET = test
INCLUDEPATH += . \
/usr/include/qt5/QtWidgets/
QMAKE_CXXFLAGS += --std=c++11
# Input
SOURCES += test.cc
Which was generated also by qmake, bar the c++11 flag and the second include path. The Makefile contains link paths to the Qt library
LIBS = $(SUBLIBS) -L/usr/X11R6/lib64 -lQt5Gui -L/usr/lib/x86_64-linux-gnu -lQt5Core -lG L -lpthread
What's weird in the above is that I don't have a /usr/X11R6 folder. Instead, libQt5Gui.so is located in /usr/lib/x86_64-linux-gnu, so I'm a little puzzled where the X11R6 comes from.
Anyway, this is my linker output:
test.cc:(.text.startup+0x20): undefined reference to `QApplication::QApplication(int&, char**, int)'
test.cc:(.text.startup+0x25): undefined reference to `QApplication::exec()'
test.cc:(.text.startup+0x2f): undefined reference to `QApplication::~QApplication()'
test.cc:(.text.startup+0x43): undefined reference to `QApplication::~QApplication()'
The above is the result of building the following source:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
return app.exec();
}
When I try to build the project file in Qt Creator, the same error appears. Am I missing libraries? Or is something configured erroneously?
(I'm on Ubuntu 14.04, and I just installed the qtcreator package from the repo's, assuming that all development libraries would be installed along with it.)
As stated in the docs, you need to include the widget library to use QApplication.
Add this to your project file:
QT += widgets
If you're not going to build a GUI app, use QCoreApplication instead. It doesn't have that dependency.

Qt 5.3.1: Why do I need to set include path and libs for widgets

I'm on Windows 8.1. I want to build Qt apps from the command line like the Unix trooper I usually am. Well, actually, I want things to be a little more automated! After wondering why qmake would not generate the proper Makefiles and hand-editing them, I finally realized I could just add on to the Qt variable in the Qt .pro file. (Been since about Qt 3-something that I've Qt'ed, so finally the light-bulb went on :)
This is the simplest of applications to help refresh the dead brain cells.
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
The .pro file:
######################################################################
# Automatically generated by qmake (3.0) Sat Aug 23 16:38:04 2014
######################################################################
TEMPLATE = app
TARGET = HelloQt
INCLUDEPATH += .
#why do I need these
INCLUDEPATH += "C:\Qt\Qt5.3.1\5.3\msvc2013_64\include\qtwidgets"
LIBS += "C:\Qt\Qt5.3.1\5.3\msvc2013_64\lib\Qt5widgets.lib"
#why does the following line do nothing
qmake: Qt += widgets
CONFIG -= X86
# Input
SOURCES += hello.cpp
Why do I need to specify the widgets INCLUDEPATH and LIBS to get a successful build?
As far as I can tell, Qt += widgets is not a valid addition to the Qt variable.
Why not? If I don't specify the Qt5Widgets include and libs, and I run nmake, I get errors related to not finding the include for QApplication and/or linker errors.
With those two lines in the .pro file everything works fine. Has anybody run into this sort of thing, am I missing something?
The line should look more like QT += core gui widgets. This will set the INCLUDEPATH and LIBS.

How to create Qt GUI application from dll called by win32 console application?

(I am not native English so Sorry if I make any grammar mistake)
I am new to Qt, started 2 days ago, and had only few experiences of WINAPI and MFC.
I am thinking of...
First, creating Qt GUI application from its dll
Second, my static library file(*.lib) will call this dll file using QLibrary.
Lastly, my console application will have its lib and its header file, and dll to create Qt GUI Application.
and my console application does not have .pro file, just created on visual studio and has only .vcproj and .sln file.
Here is the source code:
http://cfile239.uf.daum.net/attach/037B654151C4FF8D2D0EB7
I copied dll, lib and its header file into its win32 console application.
and when I compile console, I get this error message.
fatal error C1083: Cannot open include file: 'QApplication': No such file or directory
I know it is absolutely right. (cuz my lib uses QLibrary and its lib and header file is included in win32 console application..)
well, actually I do not want to have .pro file including QApplication class into my console application to fix this problem.
Is there any possible way to fix it while avoiding having .pro file?
or should I create .pro and set it to have QT library?
Thank you for reading it. :D
You should indicate your compiler Qt's path.
If you use vs,choose tools->option->project and solutions,VC++ Directories,set your Qt header files path,lib path,bin path, and set Qt's bin path to environment variable PATH.
In your static library project, try this:
create.h
#pragma once
void createQt(int argc, char* argv[]);
create.cpp
#include<QtGui/QWidget>
#include<QtGui/QApplication>
#include "create.h"
#pragma comment(lib,"qtguid4.lib")
void createQt(int argc, char* argv[])
{
QApplication app(argc,argv);
QWidget w;
w.show();
app.exec();
};
in you console program:
.cpp file:
#include "stdafx.h"
#include "create.h"
#pragma comment(lib,"CreaeQt.lib")
int main(int argc, char* argv[])
{
createQt(argc,argv);
return 0;
}

Resources