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

plugin cannot be loaded for module "Box2D": Namespace 'Box2D' has already been used for type registration #122

Open
GugaDozero opened this issue Mar 28, 2018 · 11 comments

Comments

@GugaDozero
Copy link

GugaDozero commented Mar 28, 2018

I'm building a project that uses statically qmlbox2d.

I have copied the entire folder of qml-box2d into my project, added in the .pro file this:

include(qml-box2d/box2d-static.pri)

and then in the main.cpp:

...
#include "box2dplugin.h"
...
    Box2DPlugin plugin;
    plugin.registerTypes("Box2D");

It builds ok, but when running i get this error in the title and the qml file is not loaded.

The imports of my main.qml are:

import QtQuick 2.9
import Box2D 2.0
import "qml-box2d/examples/shared"
@ThomasVogelpohl
Copy link
Contributor

ThomasVogelpohl commented Mar 28, 2018

Hi,

I am loading the static lib like this:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <box2dplugin.h>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    Box2DPlugin plugin;
    plugin.registerTypes("Box2DStatic");

    engine.load(QUrl(QStringLiteral("qrc:/ui.qml")));

    return app.exec();
}

And I had to use the following import statement:

import Box2DStatic 2.0

I have prepared a repo which uses a static lib here:
https://github.com/ThomasVogelpohl/qml-box2d-examples

Please try and compile the repo. If you can not get your code to work, come back and show more code.

Greetings,
Thomas

@ThomasVogelpohl
Copy link
Contributor

ThomasVogelpohl commented Mar 28, 2018

Somehow my comment above is not showing the includes properly:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <box2dplugin.h>

@Skycoder42
Copy link

@ThomasVogelpohl You should use fencing to include code. Add ``` before and after your code statement and it should look as expected. See https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code

@GugaDozero
Copy link
Author

GugaDozero commented Mar 28, 2018

Well, the problem is gone, but can't perform the qml file yet.
I have copied the shared folder from examples into the qrc resource.

Here my main.qml code:

import QtQuick 2.9
import Box2DStatic 2.0
import "./shared"


Rectangle {
    property var jsonPyr: [{count: 10, color: 'blue', startx: 100, starty: 510}, {count: 9, color: 'brown', startx: 123, starty: 480}, {count: 8, color: 'blue', startx: 145, starty: 450}, {count: 7, color: 'green', startx: 168, starty: 420}, {count: 6, color: 'yellow', startx: 191, starty: 390}, {count: 5, color: 'brown', startx: 214, starty: 360}, {count: 4, color: 'blue', startx: 237, starty: 330}, {count: 3, color: 'green', startx: 260, starty: 300}, {count: 2, color: 'pink', startx: 283, starty: 270}, {count: 1, color: 'yellow', startx: 306, starty: 240}]

    id: root
    width: 800
    height: 600
    focus: true

    color: "black";

    World { id: physicsWorld }


    Component {
        id: boxChar

        Item {
            id: mario

            width: 30
            height: 80

            BoxBody {
                id: body

                world: physicsWorld
                target: mario
                bodyType: Body.Dynamic

                density: 1
                friction: 1
                restitution: 0.3

                width: mario.width
                height: mario.height
            }

            Image {
                anchors.fill: parent
                anchors.margins: -1
                source: "images/super_mario.png"
            }
        }
    }

    Column {
        id: rectCol


        Repeater {
            id: repeater1
            model: jsonPyr

            Row {
                id: rectRow
                property string rectColor: modelData.color
                property var rectIndex: index


                Repeater {
                    id: repeater2
                    model: modelData.count

                    Loader {


                        sourceComponent: Component {
                            id: comp

                            RectangleBoxBody {
                                width: 30
                                height: 30

                                bodyType: Body.Dynamic


                                MouseArea {
                                    anchors.fill: parent
                                    onClicked: { sourceComponent = undefined }
                                }

                                density: 1.5
                                friction: 0.2
                                restitution: 0.3

                            }
                        }

                        onLoaded: {
                            item.parent = root;
                            item.x = jsonPyr[rectIndex].startx + 47 * index;
                            item.y = jsonPyr[rectIndex].starty;
                            item.color = rectRow.rectColor;
                            item.border.color = 'white';
                            item.world = physicsWorld;

                        }


                    }
                }
            }
        }
    }




    Component.onCompleted: {
        var character = boxChar.createObject(root);
        character.x = 306;
        character.y = 180;
    }


   RectangleBoxBody {
        id: ground
        world: physicsWorld
        height: 40
        anchors {
            left: parent.left
            right: parent.right
            bottom: parent.bottom
        }
        friction: 1
        density: 1
        color: "grey"
    }
}

main.cpp code:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <box2dplugin.h>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    Box2DPlugin plugin;
    plugin.registerTypes("Box2DStatic");


    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

The qtcreator output was:

Starting /home/cientista/Jogo/Jogo...
QML debugging is enabled. Only use this in a safe environment.
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
The program has unexpectedly finished.
The process was ended forcefully.
/home/cientista/Jogo/Jogo crashed.

@bjorn
Copy link
Member

bjorn commented Mar 28, 2018

The original problem is one of the things I addressed in PR #121, in particular change 6b51541.

@ThomasVogelpohl
Copy link
Contributor

ThomasVogelpohl commented Mar 28, 2018

Hi Bjorn,
if I understand you correctly, adding:

qmlProtectModule("Box2D", 2);

should allow the static lib to be registered with:

plugin.registerTypes("Box2D");

without problem from the existing dynamic lib ?

@bjorn
Copy link
Member

bjorn commented Mar 28, 2018

@ThomasVogelpohl Yes, protecting the module prevents Qt from even searching for the plugin, so it will not get to the point of loading it and trying to make it register its types. See qmlProtectModule.

@ThomasVogelpohl
Copy link
Contributor

Neat, that would make things a lot easier with the static lib. Thanks for the hint.

@GugaDozero
Copy link
Author

GugaDozero commented Mar 28, 2018

Guys, I have adapted the code once again, this time the main.qml import are:

import QtQuick 2.9
//import Box2DStatic 2.0
import Box2D 2.0
import "./shared"

and the main.cpp code is:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <box2dplugin.h>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    /*
Box2DPlugin plugin;
    plugin.registerTypes("Box2DStatic");
*/

    Box2DPlugin plugin;
     plugin.registerTypes("Box2D");

       qmlProtectModule("Box2D", 2);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

This time the project builds and run, except by the fact that there's no output, none window was showed. What should I be doing wrong?

@GugaDozero
Copy link
Author

@ThomasVogelpohl will you update your repository with the new hint of @bjorn?

@ThomasVogelpohl
Copy link
Contributor

@GugaDozero yes, I will change it. Just need to find time.

This change makes my repo so similar to the stock qml-box2d, that it should be enabled in the stock repo anyway.

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

No branches or pull requests

4 participants