Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved, sort of] How to draw a wireframe in Qt3D
Qt 6.12 LTS is out! See what's new in the release blog

[Solved, sort of] How to draw a wireframe in Qt3D

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello!

    This is (obviously) a Qt3D oriented post...I wasn't sure where to put this; if it's in the wrong place, let me know.

    I'm trying to teach myself Qt3D for an upcoming project. Part of what I need to do is to draw a wireframe of my geometry, however, I can't seem to get Qt3D to behave correctly for me. I've simplified this down to simply trying to draw one triangle. I subclass QQuickItem3D to create a new QML element, and use QGLBuilder and QGeometryData to construct a new QGLSceneNode. However, no matter what I set "setDrawingMode" to, I still end up with a white triangle in my scene, instead of the wanted wireframe.

    Anyone know anything about Qt3D that can help?

    EDIT: I can fix the problem by ignoring the QGLBuilder and the QGLSceneNode, and simply calling the "draw" method on the QGeometryData.

    Here's my code:

    Wireframe.h:
    @
    #ifndef WIREFRAME_H
    #define WIREFRAME_H

    #include "qquickitem3d.h"
    #include "qglscenenode.h"

    class Wireframe : public QQuickItem3D
    {
    Q_OBJECT
    public:
    Wireframe(QObject *parent = 0);
    ~Wireframe();

    protected:
    void drawItem(QGLPainter *painter);

    private:
    QGLSceneNode* myScene;
    };

    QML_DECLARE_TYPE(Wireframe)

    #endif
    @

    Wireframe.cpp:
    @
    #include "Wireframe.h"
    #include <QGeometryData>
    #include <QGLBuilder>

    Wireframe::Wireframe(QObject *parent)
    : QQuickItem3D(parent)
    {
    QGLBuilder builder;
    QGeometryData geometry;
    geometry.appendVertex(QVector3D(0,0,0));
    geometry.appendVertex(QVector3D(4,0,0));
    geometry.appendVertex(QVector3D(0,4,0));
    builder << geometry;
    myScene = builder.finalizedSceneNode();
    myScene->setDrawingMode(QGL::LineLoop);
    myScene->setDrawingWidth(10);
    myScene->setParent(this);
    }

    Wireframe::~Wireframe(){
    delete myScene;
    }

    void Wireframe::drawItem(QGLPainter *painter)
    {
    // Draw the geometry.
    myScene->draw(painter);
    }
    @

    main.cpp:
    @
    #include <QApplication>
    #include <QQuickView>
    #include <QDeclarativeEngine>
    #include "Wireframe.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    qmlRegisterType<Wireframe>("CustomComponents", 1, 0, "Wireframe");
    QSurfaceFormat f;
    f.setSamples(16);
    QQuickView view;
    view.setFormat(f);
    view.setResizeMode(QQuickView::SizeRootObjectToView);

    view.setSource(QUrl::fromLocalFile&#40;"../wireframe.qml"&#41;&#41;;
    

    #ifdef Q_OS_SYMBIAN
    view.setAttribute(Qt::WA_LockLandscapeOrientation, true);
    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    view.showFullScreen();
    #else
    if (QApplication::arguments().contains(QLatin1String("-maximize")))
    view.showMaximized();
    else if (QApplication::arguments().contains(QLatin1String("-fullscreen")))
    view.showFullScreen();
    else
    view.show();
    #endif

    return app.exec&#40;&#41;;
    

    }
    @

    wireframe.qml:
    @
    import QtQuick 2.0
    import CustomComponents 1.0
    import Qt3D 2.0

    Rectangle {
    width: 640
    height: 480
    color: "pink"
    Viewport {
    width: 640; height: 480
    Wireframe { id: wf }
    }
    }
    @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt
    • Unsolved