Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i create a new class with a .h and .cc to use it in .qml file?? #12240

Open
jorgitinlugo opened this issue Dec 20, 2024 · 1 comment
Open
Labels

Comments

@jorgitinlugo
Copy link

What I want is a guide to create custom classes with their functions, and then be able to import them and use their functions in a .qml file. I don't know where I have to add the source and header files for Qt to be able to detect them.

@DominicMarti
Copy link

Hi @jorgitinlugo

Well that depends a bit on what you want to do and what you want to control. Best thing is to compare it something that is already there and put it in the same place. You can check src/UI/toolbar, src/QmlControls or src/FlightMap/Widgets to see some examples.

Everything you want to be able to call from a qml file should be an Q_INVOKABLE. Using an instance of a class in the qml file then allows you to call your functions or access variables.

Now for your custom classes, you can put them in a custom folder. Linking it to make it accessible in your qml file is then a bit a of a pain (my opinion^^). Easiest is to follow on what you see in other files. I would use something like src/FlightDisplay/OnScreenGimbalController.qml as an example, where you see how to include, create an instance, and then access its properties.

In general: Make sure you included the header and cpp files in all relevant places, which specifically includes CMakeLists in your custom folder, potentially as well src/QGCApplication.cc. If it is part of the active vehicle, then as well in the vehicle files.

I hope this gives you head start. It always took my a while to add something completely new.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants