How to install Box2D-qml with mingw32 - qt

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.

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.

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

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.

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.

Deploy Qt5 QML application

To test QML deployment I've created a very simple QML application. Here is the code:
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QFile>
int main(int argc, char **argv) {
QApplication app(argc, argv);
QQmlApplicationEngine engine;
QString path = app.applicationDirPath() + "/qml/main.qml";
if(QFile::exists(path))
engine.load(path);
else {
return 1;
}
return app.exec();
}
main.qml
import QtQuick 2.2
import QtQuick.Controls 1.2
ApplicationWindow {
id: mainWindow
title: "Test window"
width: 800
height: 600
visible: true
}
To be sure no development library was installed in the system, I've set up a virtual machine with a pure Windows XP installation. Then, I've followed instructions as described here and copied all Qt5*.dll into the program directory, as well as platforms/qwindows.dll and icu*52.dll. Dependency Walker confirmed that no broken dependencies were left, i.e. everything should have been correctly set up.
However, for some reasons, when I run my app I see nothing. Neither a window, nor an error message. Running from console also gives me no error. Despite this, I can see my app running in the Task manager, like it is running in background. Running the app on the development machine goes without problem: the app correctly starts and I can see its windows.
What am I doing wrong? How can I deploy a QML app to be sure it will work on any other - non development - machine?
If you use MinGW, then try to copy all folders from folders qml and plugins to directory with your program. Also copy libraries: icudt52.dll, icuin52.dll, icuuc52.dll, libgcc_s_dw2-1.dll, libstdc++-6.dll, libwinpthread-1.dll, Qt5Core.dll, Qt5Gui.dll, Qt5Network.dll, Qt5Qml.dll, Qt5Quick.dll, Qt5Svg.dll, Qt5Widgets.dll from bin
Eventually the directory will look like this:
Enginio
imageformats
platforms
Qt
QtGraphicalEffects
QtPositioning
QtQml
QtQuick
QtQuick.2
QtSensors
QtWebKit
QtWinExtras
icudt52.dll
icuin52.dll
icuuc52.dll
libgcc_s_dw2-1.dll
libstdc++-6.dll
libwinpthread-1.dll
Qt5Core.dll
Qt5Gui.dll
Qt5Network.dll
Qt5Qml.dll
Qt5Quick.dll
Qt5Svg.dll
Qt5Widgets.dll
YOUR_PROGRAM.exe
This way works on WindowsXP/Win7 where Qt was not installed.
This what i've figured out so far,
You can't just open a qml file in main.cpp, you have to put those qmls into a resource
qml.qrc:
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
Then main.cpp must load it from the resource
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
then build and check it works, then deploy as follows:
locate the releasse directory where your EXE lives
locate the directory where your QML lives
create a directory somewhere, say, deploy
then
cd deploy
windeployqt --release --qmldir <qml-dir-location> <exe-location>
NOTE: add location of windeployqt to PATH
eg. C:\Qt\Qt5.5.1\5.5\msvc2013\bin
You should use the deployment tool that comes with Qt. See Qt for Windows - Deployment, there is a section "the Windows deployment tool".
As mentioned by BaCaRoZzo, a general solution that worked pretty well for me is to follow this guide.
To sum it up, copy in an empty directory:
The release version of MyApp.exe, along with .dll you created for this projet
All the .dll files from \mingw48_32\bin\
All the folders from \mingw48_32\plugins\
(If you used QML) All the folders from \mingw48_32\qml\
First, during the test of your application, rename your qt folder so it is not found by $PATH, and double-click on MyApp.exe. This should run the program.
NB: It leads to a very big application, so you will need to delete some extra files and folders.
The easiest way is for the dll: you run the program, and while being run, you delete all the dll in your new project. Only the ones that are not used by MyApp.exe will be deleted. Efficient!
For the Qt folders, proceed by trials and errors.
You need to deploy the application, for this purpose I use the utility
cqtdeployer
This utility itself collects all the necessary dependencies of your application and you do not have to spend your time on it, or you can automate this process.
You can install from github releases (Windows)
or
from snapstore (Linux)
sudo snap install cqtdeployer
You can use as follows:
Windows:
%cqtdeployer% -bin myApp -qmake path/to/Qt/5.x.x/build/bin/qmake.exe -qmlDir path/to/my/qml/files/dir
Linux:
cqtdeployer -bin myApp -qmake path/to/Qt/5.x.x/build/bin/qmake -qmlDir path/to/my/qml/files/dir
path/to/Qt/5.x.x/build/bin/qmake - This is the way qmake is used to build your program.
path/to/my/qml/files/dir - this is the path directly to your qml file (which you wrote)
And Run application with sh script (Linux) or exe (Windows)
If you'll use the version from snap then make sure that you have all the permissions.
If you need use windows version just install application from installer
use windeployqt --qmldir f:\myApp\sources f:\build-myApp\myApp.exe command.
the first argument after qmldir is your qml folder location and the second argument is where your exe is placed. You would get deployable exe on any env.
For me this answer worked for a QtQuick 2 application (QML app), but I'm adding more details:
Build inside Qt as Release
Find the folder in the file explorer with the executable, usually on a sibling folder to your project folder
Copy executable to an empty folder
Start Qt CMD tool (mine is ), go to the folder with the executable and run:
windeployqt --release --qmldir C:\Qt\5.15.1\msvc2015_64\qml executable.exe
(change 5.15.1\msvc2015_64 for yours)
My answer was:
(1) On Qt Framework set the Release mode for my Application.
(2) After: Project -> Build directory and set a empty directory in the "C:/" folder (I'm development Windows 10). Example: My Folder: "Release" with subfolder "My_app", so the Build directory is "C:/Release/My_app"
(3) Click on "Start debuggin of the startup project"
(4) Create another folder on "C:/" directory (in my case callet "Executable" with subfolder "My_app").
(5) Copy "My_app.exe" of the folder "Release/My_app/release" to "C:/Executable/My_app").
(6) Open Qt mingw.
(7) On the terminal of the Qt MinGw go to folder "Release/My_app/release" through command "cd C:/Executable/My_app"
(8) Run the command "windeploy.exe My_app.exe --qmldir 'full_path_where_are_the_qml_files'" (e.g.: "C:/My_prj/qmlsource", where qmlsource is folder with my qml_files).
This generate all files to run My_app.exe. Just the copy all folder (My_prj folder).

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