Unreal Engine 4 + Qt5 integration - c++

I'm a bit new in UE and i'm searching a way to make UE works with Qt for some of my projects ( like robotic simulation / sea simulation ), btw I don't want to use Slate.
I'm currently working on Windows with VS2015.
Is anyone knows where to start, I mean, where to start looking for flags or lib adds ?
Or a better solution should be to add Qt as a static library ? as this.

if you use C++ for unreal & qt, this won't help you.
But if you use Python for unreal & qt :
You can add the unreal_qt module to your project. It works with unreal 5, and 4. The readme has step by step instructions on how to use it, with a python qt example.
Quickstart
Add the unreal_qt folder in your python path. See unreal docs
Use the following code snippet to create sample.py and add it to unreal python path.
# 1. SETUP - this step can automatically run on editor startup when added to your init_unreal.py
import unreal_qt
unreal_qt.setup()
# 2. CREATE WIDGET - create your qt widget
# every widget you make after setup won't block the editor & have unreal styling
from PySide2.QtWidgets import QLabel, QWidget, QVBoxLayout
w = QWidget()
layout = QVBoxLayout()
w.setLayout(layout)
layout.addWidget(QLabel("Hello World!"))
# 3. WRAP WIDGET - (optional) manage garbage collection, add darkbar, stay on top
unreal_qt.wrap(w)
# 4. SHOW WIDGET - if using stay on top this needs to run after the wrap stage
w.show()
import script in unreal with the Python terminal to run it.
import sample

Related

Fail to import QML module using CMake

I'm currently building a minimalist app following this CMake architecture:
-root
--QmlModule
---Component1.qml
---Component2.qml
--App1
---main.cpp
---main.qml
--App2
---main.cpp
---main.qml
I use "qt6_add_qml_module" to create a QML module at "QmlModule" level as a STATIC library.
qt_add_library(myComponentTarget STATIC)
qt6_add_qml_module(myComponentTarget
URI QmlModule
VERSION 1.0
QML_FILES
Component1.qml
Component2.qml
RESOURCES
logo.png)
Then, at App1 (and App2) level, a link to the module is done using "target_link_libraries". "qt6_add_qml_module" does some work behind the scenes in order to expose the module trough an automatically generated plugin named "your_component_URIplugin". More details about this here.
add_executable(App1Exe
main.cpp)
qt6_add_qml_module(App1Exe
URI App1
VERSION 1.0
QML_FILES
main.qml)
target_link_libraries(App1Exe
PRIVATE
myComponentURIplugin)
At Root level, I overload QML_IMPORT_PATH in order to link to the build folder and add all subdirectories.
set(QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qmlModule)
add_subdirectory(QmlModule)
add_subdirectory(App1)
add_subdirectory(App2)
I run CMake without any errors, and open App1/main.qml file.
On my import QmlModule, the module can't be found:
module "lupinComponentsplugin" is not installed
How to make my module visible from my Apps ?
What step am I missing ?
I'm currently doing something similar.
I have created a demo app where I import modules. The modules provide QML and C++ items to the main app. Check the comments in the CMAKE files to find out how this works.
Here is the link:
https://gitlab.com/basic53/DemoApp
Feel free to comment on this.
Another tip: If qt_add_qml_module is not working properly, sometimes it is necessary to remove the whole build folder and update the QML code model. You can check the generated files to see, if your plugin has been created with all its types.
CMake itself was fine, this was a runtime error and not a link error.
This issue was raised because the QQmlApplicationEngine wasn't finding path towards my module's QMLDIR.
In the end, the only thing missing was an additional import path ":/" in QQmlEngine:
QQmlApplicationEngine engine;
engine.addImportPath(":/");

How to use QML_ELEMENT with cmake

The doc shows I can use QML_ELEMENT macro to create QML types from C++ by adding some variables in qmake's .pro file. But I'm using cmake
Update (Qt 6.2+)
As of Qt 6.2, qt_add_qml_module is a single command for building qml modules that should take care of virtually everything, replacing amongst others the old qt6_qml_type_registration command.
Old answer (Qt 6.0/6.1)
Now that Qt 6.0 is out this is supported, albeit poorly documented. What you need now is:
set_target_properties(foo PROPERTIES
QT_QML_MODULE_VERSION 1.0
QT_QML_MODULE_URI Foo
)
qt6_qml_type_registration(foo)
you can then do in qml:
import Foo
and you'll have access to types that have QML_ELEMENT and friends. Notes:
Two files are created in the build output folder, <project>_qmltyperegistrations.cpp and <project>.qmltypes, if your imports are failing you can look at those to see which types are missing. I found that I needed to do full recompiles sometimes after adding/removing registered types.
Qt examples have been migrated to cmake, so take a look at e.g. Examples/Qt-6.0.0/quick/tableview/gameoflife to see it in action
There are now pro2cmake.py and run_pro2cmake.py files in the Qt sources at Qt/6.0.0/Src/qtbase/util/cmake. They are mentioned on this Readme page, you can find them here, haven't tried it myself.
Edit (Qt 6.x)
This answer was originally posted for Qt 5.15. Now that Qt 6 is available, and if you are using Qt 6, refer to the answer from #Adversus.
Original answer (Qt 5.x)
From what I can see, CONFIG += qmltypes, which is required to use QML_ELEMENT, is not yet supported in CMake by looking at the documentation, even for the master branch.
And the efforts to provide a python .pro to cmake converter are for Qt6, not merged, and not functional as far as I can tell, by testing them from util on the wip/cmake branch (the CMakeLists.txt didn't have relevant information).
You can see that the actual conversion script does test for qmltypes presence in CONFIG, but it doesn't seem to map to anything usable for CMake.
Solution
Instead of using QML_ELEMENT and CONFIG += qmltypes, which is brand new from Qt 5.15 (latest when writing this), and not supported at this time with CMake, use the good old qmlRegisterType from C++:
#include "YouCustomCppClass.h"
int main(int argc, char** argv) {
// Let you import it with "import ModuleName 1.0" on the QML side
qmlRegisterType<YouCustomCppClass>("ModuleName", 1, 0, "YourQmlComponent");
//Create your QML view or engine
}
This won't require anything specific on the project file side, as long as your code/plugin executes qmlRegisterType statement.
You can refer to Qt's documentation, same page as yours, but for Qt 5.14 instead of latest, which describes exactly that: Writting QML extensions for C++ | Qt 5.14

Create folders/subfolders in an qt creator project

In my Qt Project, I want to create some subfolders like Audio or Video.
Something like this:
Project\Media
Project\Media\Audio
Project\Media\Audio\Video
How I can do this?
Is there an way to add a new folder in Qt Creator? Or do I need to create this manually via the explorer?
Currently the only way is to add or create file that is inside of the directory - then the directory will be also created in Projects view of the QtCreator. This is not really that bad, since you can create directory while adding a new class file to the project. When the file is added to the project, also all its parent directories will be reflected in the Projects view.
In qt you use mkdir
bool QDir::mkdir ( const QString & dirName ) const
which will create a subdirectory called (variable dirName)
You could also use cmake which allows you to generate makefiles for every platform you need, itsteand of writing them manually. See code examples here

qmlscene replace c++ module

I have a qml import statment that is something like
import com.my.namespace.logic
and this is from a c++ registered class
qmlRegisterType<ApplicationController>("com.my.namespace.logic", 1, 0, "ApplicationController");
Now I want to be able to use qmlscene to run this with special testing logic. So I made a folder structure like
▾ testingdata/
▾ com/
▾ my/
▾ namespace/
ApplicationController.qml
LoginController.qml
MasterController.qml
qmldir
I made sure that my qmldir's first line was
module logic
Then tried to run
qmlscene -I testingdata main.qml
but it will always complain that my namespace isn't registered. Has anyone tried to do this?
What I did is I made another project that includes all the headers and source plus some extended classes so that it could run as a separate executable that doesn't connect to any online services.

Why can't I import my own Qt module?

So, I'm creating an app in QML, and have created a custom component. To be better organized, I am placing all of my custom components in a "com" subdirectory. I did some research, and found that I needed QML_IMPORT_PATH and a custom qmldir file to create a module so I could import my controls into my project.
My components path is E:/Qt Projects/MyApp/qml/MyApp/com
Inside, I created a qmldir.txt file containing the following:
Button 1.0 Button.qml
Where "Button.qml" is the name of my custom component (in the same directory).
//Button.qml
import QtQuick 1.1
Rectangle {
width: 100
height: 50
}
Next I went to "MyApp.Pro" and appended the following:
QML_IMPORT_PATH = E:/Qt Projects
Finally, I went to MyApp.qml and added:
import MyApp.qml.MyApp.com 1.0
Yet, I am still getting a "module not found" error on that line. When it shows the error during mouseover, it displays a list of the current import paths, and E:/Qt Projects wasn't one of them. I've cleaned, rebuilt my project, ran qmake and everything. Still not working. Is my syntax wrong? Why won't it find my module? I'm a Qt newbie so forgive me if this is a stupid question. Any help is appreciated. Thanks in advance.