Change and update geometry mesh at runtime - qt

This question and answer, in Oct 2015, implies it is possible to change Qt3D mesh and update it:
Question
I want to use Qt3d in a situation that involves dynamic runtime
changes of geometric data on the application side.
What is the best way to dynamically change a mesh for an entity?
I'd rather do all this on the C++ side, but QMesh doesn't seem to
provide an API for changing the mesh data.
I looked at some other examples of making a custom QAbstractMesh class
and QAbstractMeshFunctor. It looks like I could possibly make a custom
mesh type to do what I need but I have a question. If I keep a
reference to the QMeshDataPtr that I make from the functor, can I
simply modify the mesh data whenever I want and the entities that
reference it will update automatically?
Answer
The API for this has changed a little in 5.6. The geometric data is
now contained in one or more QBuffer objects and is referenced by one
or more QAttributes that describe the data layout in the buffers. The
QAttributes are rendered by adding them to a QGeometryRenderer
component.
You can either update the above objects on the main thread and call
update() or as before you can also use a functor to have the backend
generate the dynamic data.
Now, my question is about calling update(). Exactly what section of Qt3D API is referred to?

There is a test available at Qt installation directory on my Linux machine:
/home/{user}/Qt5.12.6/5.12.6/Src/qt3d/tests/manual/custom-mesh-update-data-cpp/
which I discovered by following this link when searching Google for qt3d mesh update keywords.
The above test is using Qt3DRender::QBuffer API to update mesh data:
void QBuffer::updateData(int offset, const QByteArray &bytes)
Updates the data by replacing it with bytes at offset.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
Code looks like this:
Qt3DRender::QBuffer *vertexDataBuffer;
// ...
QByteArray updateData;
// ...
vertexDataBuffer->updateData(pos,updateData);
// ...

Related

How to create a SlotOfQImage in QT Rust?

I'm trying to define a custom QT Slot/Signal for passing a QImage.
I'm using qt_widgets crate which has many predefined slot types like SlotOfQIcon. However it doesn't have one for QImage. I've looked at the source code which is generated with this tool: https://github.com/rust-qt/ritual.
Is it possible to do this in regular Rust project without running complex multistage build involving code generation?
List of implemented slots for reference:
https://docs.rs/qt_widgets/0.3.0/x86_64-pc-windows-msvc/qt_widgets/
I've tried to copy paste generated code for other widgets and modify it, but it doesn't look like a right approach. I don't want to regenerate the whole library just for this case. Are there any simpler options?

Localization in QtQuick from top to bottom

After several weeks of on-and-off research, I still haven't found a thorough guide on how to perform translation/localization in QtQuick (as in, using the QML language, not C++ or Python).
In general, I'm asking what are the steps to properly localize a project as much in QtQuick as possible, with minimal or preferably no C++.
More specifically, there are a good number of holes I need to fill in my understanding of how QtQuick handles localization.
So far, I've:
Appended QT_TR_NOOP() to all of my translatable strings for translation at runtime
Added my file containing all strings to my .pro file using lupdate_only{SOURCES += LanguageStrings.qml}
Generated translation files using QtLinguist
However, I intend to implement an option for dynamically changing the language, and the only example I've seen regarding translation which wasn't entirely in C++ essentially created an instance of the project for each language, rather than changing the strings at runtime.
So, how do I change the language at runtime? Is there a variable I can set? Is it pulled from system locale? I haven't seen a solid answer on this.
Any ideas?
You can do this with minimal C++ (at least I think this is minimal). I've done this in the past using the locale of the system the app is installed on like this (directly in main()):
QGuiApplication app(argc, argv);
QTranslator translator;
if(translator.load(":/translations/myapp_" + QLocale::system().name())) {
app.installTranslator(&translator);
} else {
qDebug() << "Unable to load translation";
}
The translations need to be in the resource system for the above to work. You can of course trigger the above based on user input from QML (e.g. in the settings of your app) at run-time. Here is some example code to do this (https://wiki.qt.io/How_to_do_dynamic_translation_in_QML). I am not aware of a QML-only way to do this.
I tried something else which works, too. You can have your UI in a Loader Element and simply use the setSource functions of that element after the translator changed. I quickly put together a small example, which also includes an example of how to change UI elements outside the Loader, if that is needed (https://github.com/Conntac/qtExamples).

How to display points with QT3D?

Qt3D makes it very easy to display some mesh primitives:
m_torus = new Qt3DExtras::QTorusMesh();
but I would just like to display a collection of points. I haven't seen anything like
m_points = new Qt3DExtras::QPoints();
Is there a way to do this without writing lower level OpenGL?
Don't know if this is what you're looking for but check out Qt3DRender::QGeometryRenderer. I use it in a project to display map lines in a 3D scene.
There is a method to define how the vertex buffer data shall be rendered (where I use Qt3DRender::QGeometryRenderer::LineStrip instead of Qt3DRender::QGeometryRenderer::Points):
Qt3DRender::QGeometryRenderer::setPrimitiveType(Qt3DRender::QGeometryRenderer::Points);
AFAIK, There are no simple primitives like lines or points available in Qt3D 2.0, because there is just no one-size-fits-it-all solution. If you are lucky, someone will step up and add something to extras, else you have to write your solution yourself.
Qt Interest Mailing List Nov 2016 - Lines in Qt3D
There is however, a pcl point cloud renderer project on github!

Qt OpenGL data synchronization / Model/View implementation

I am trying to develop an application with Qt 5.5 and OpenGL. The basic work of the application will be to load simple objects, modify their positions in a scene and save them together with other attributes (material, name, parents/child relations etc...).
The only thing I am struggling about for a week now is that I really don't know how I should take on the problem of synchronizing data. Let's say I have some kind of SceneGraph class which takes care of all SceneObjects. Those SceneGraphs should be rendered in a SceneView-Widget which can be used to modify it's Objects via transformations. Now how would I tell every SceneView that an Object changed it's position?
I thought of the Model/View architecture for a moment but I am not really sure how this implementation should look like.
What would be the best way to handle Objects like that in different Windows/Widgets but still have one single piece of data?
SceneObject:
Holds the mesh-information (verticies, uvs, etc..)
Has a name (QString)
Has a material
Has a transform storing position, rotation and scaling information
(Important: these datatypes should be synchronized in all views)
SceneGraph:
Contains different SceneObjects and is passed to SceneViews
SceneView:
The QWidget responsible for drawing the Scene correctly in any QWindow.
Has it's own camera to move around.
Handles UserInput and allows transformation of SceneObjects.
You could use the signal and slot to observe position updates of SceneObjects and process them in SceneView.

Complex initialization in Custom QML type

I am creating a C++ class that will be registered as a QML type. I want to run some non-trivial logic when the object is initialized. I don't want to put this logic in the constructor because that is bad practice. In a standard C++ class I would usually create a Startup() function with this logic and call it just after initializing the object, but I have no control over this as objects are initialized in QML.
How should I implement this custom initialization logic for a custom QML type?
For those who want the details. I am making a QAbstractListModel that keeps track of all .txt files in a directory. When it is created it will scan the directory (passed in via property) and update its internal collection with the names of all .txt files in that directory.
Edit1: After looking at Qt's example projects I found that many of them actually do all initialization logic in the constructor, including things like setting up DB connections and doing an initial DB query and query parsing. One need only search for "database" from the Qt Creator Welcom->Examples screen to see these samples. I would appreciate it if someone found and explained a better way.

Resources