Important: Please read the Qt Code of Conduct -

Qt3d points and lines

  • Hi all,

    I've just started messing around with Qt3d (without QML) and wondered if it is possible to simply draw a line between two points in 3-dimensional space.
    Also, is there an easy way to set the bottom of a, for example, Qt3DExtras::QConeMesh() to point 1 and let the top end at point 2 (automatically set the length and angle between two points, so to speak).

    guy incognito

  • So I figuered out how to draw simple lines with the help of


    and Vertex/Index buffer. Now I got some flat lines. I would like to give them some more "volume" to make them look like tubes.
    Anyone got an approach?

  • Moderators

    Well, tubes are meshes, aren't they, so perhaps check out the QMesh class or the QCylinderMesh.

  • @kshegunov

    I don't quite get how to make a cylinder mesh behave like a line or make a line look like a cylinder.

  • Moderators

    I don't understand the question. A line is a mathematical abstraction, it has no volume and no surface area (just like a point). If one needs to draw a line one uses a realistic object that represents the line - e.g. a rectangle in 2D, or a cuboid or cylinder in 3D. Also I don't understand how does a line is supposed to behave ...?

  • @guy-incognito could you share the code for drawing lines ? thanks!

  • @kshegunov

    Let' s say i want to draw a triangle. I define the vertices p1 (0,0,0), p2 (2,0,0), and p3(1,2,0) and draw the lines between them. That's what i currently do. Now I want to draw a triangle with tubes instead of lines, so that it sort of looks like the triangle music instrument. I think thats called tubing and extrusion in OpenGL.

    It was my question if there is a convenient way in qt3d to do this. Maybe my lines could be replaced with cylinders, so the bottom surface starts at (0,0,0), the top surface ends in (2,0,0) etc. (thats what i meant with "behave like a line").

  • Moderators

    Look at this thread's example code. It should be enough to get you started.

  • @kshegunov

    Thanks, the x,y and z axis look exactly like what I want. The only problem is, that QTransform for the QCylincerMesh is pretty unconvenient (as far as I see).


    Take a look at this code. I used it to get an idea how to create custom meshes. You can adapt it to make it draw lines instead of TetraHedrons pretty easily.

  • This has been my PITA also. I have just found an example in qt5.6 that was dropped in all further releases but it provides a working set of code that creates a custom entity with your own set of vectors / normals / colors. If you look at this in

    I wish Qt (KDAB is actually doing the Qt3D stuff) would spend a bit of time and put together some documented examples at the level of what their qGraphicsItem module was. Or at a minimum put some details to the qt function documentation. It is very frustrating to try and figure things out when there is no documentation or good functional examples on the objects we are supposed to use.

  • @Steve-Brunton
    I am trying to draw a line in qml. I used the custom_mesh_qml for checking how it works in qt 5.9. but I encountered an error in the main.cpp file in the line 'engine.aspectEngine()->setData(data);' like setData not found. Can you help me on this. Thank you.

  • Lifetime Qt Champion

    @MatPaul Why don't you post the actual error message?
    I guess the compiler tells you that there is no such method: setData.
    I guess aspectEngine() returns and QQmlAspectEngine does not have setData() method.

  • @jsulm
    I had used the code from this link

    And run that on qt5.9
    I think the setData() functionality is removed in 5.9. is there a new function which does the similar behaviour

  • Try this piece of code to draw lines using pipes. It assumes that your camera up vector is the Y axis:
    double w, h, d, l, tx, ty, tz, lxz, anglex, angley;
    w = to.GetX() - from.GetX();
    h = to.GetY() - from.GetY();
    d = to.GetZ() - from.GetZ();
    l = sqrt(ww + hh + dd);
    lxz = sqrt(w
    w + d*d);
    tx = from.GetX() + w/2;
    ty = from.GetY() + h/2;
    tz = from.GetZ() + d/2;
    anglex = acos(h/l)*180/3.14159265;
    angley = acos(d/lxz)180/3.14159265;
    QVector3D QVx(static_cast<float>(1), static_cast<float>(0), static_cast<float>(0));
    QVector3D QVy(static_cast<float>(0), static_cast<float>(1
    ((w<0)?-1:1)), static_cast<float>(0));

    // Cylinder shape data
    Qt3DExtras::QCylinderMesh *cylinder = new Qt3DExtras::QCylinderMesh();
    // CylinderMesh Transform
    Qt3DCore::QTransform *cylinderTransform = new Qt3DCore::QTransform();
    cylinderTransform->setRotation(Qt3DCore::QTransform::fromAxesAndAngles(QVx,static_cast<float>(anglex), QVy, static_cast<float>(angley)));
    cylinderTransform->setTranslation(QVector3D(static_cast<float>(tx), static_cast<float>(ty), static_cast<float>(tz)));
    Qt3DExtras::QDiffuseSpecularMaterial *cylinderMaterial = new Qt3DExtras::QDiffuseSpecularMaterial();
    // Cylinder
    m_cylinderEntity = new Qt3DCore::QEntity(m_rootEntity);


  • @AlanWasHere
    what do you mean by : static_cast<float>(1((w<0)?-1:1)) please?

  • Lifetime Qt Champion

    @halima What exactly are you asking?
    static_cast casts from one type to another:

    static_cast<T>(W) - this casts from W to T

    So, in this case it casts from int to float.
    Or do you want to know what


    If so then see

  • @jsulm ok I got my answer, Thanks :D

  • Hi! I wrote resolution like @AlanWasHere, but a little clearer

    void addLine(QEntity *parentEntity, const QVector3D &srcPos, const QVector3D &targPos)
        auto edgeEntity = new QEntity{parentEntity};
        auto cylinder = new QCylinderMesh{edgeEntity};
        auto len = srcPos.distanceToPoint(targPos);
        auto transPoint = targPos - srcPos;
        auto xAngle = atan(sqrt(pow(transPoint.z(), 2) + pow(transPoint.x(), 2)) / transPoint.y()) / M_PI * 180;
        auto yAngle = (transPoint.x() == 0 && transPoint.z() == 0) ? 0 : atan(transPoint.x() / transPoint.z()) / M_PI * 180;
        auto transform = new Qt3DCore::QTransform{edgeEntity};
        transform->setTranslation((srcPos + targPos) / 2);
        auto material = new QPhongMaterial{edgeEntity};

Log in to reply