Qt World Summit: Register Today!

Use qtDesigner for create a Cartesian plane

  • Hi everyone!

    I'm searching for a solution to create a Cartesian Plane, using qtDesigner.

    I find out QGraphicView and QGraphicScene for creating a "canvas" and the cartesian axes (and also all geometric shapes) but I don't know how to do this.

    I find this code for create a simple triangle
    @ QGraphicsScene* scene = new QGraphicsScene();
    scene->addLine(0, 0, 50, 0);
    scene->addLine(50, 0, 50, 50);
    scene->addLine(50, 50, 0, 0);
    QGraphicsView* view = new QGraphicsView(scene);@
    But I don't know how to connect this code to a QGraphicsView on a qtDesiner form.

  • Are you saying you want to use designer to draw QGraphicsScene elements manually?

  • No, I'm sorry... I did not explain well...
    I going to do better.

    I have a QMainWindow with a QLineEdit where i can input some point, line or triangle.
    I want to create a QPushButton that plot the input on a canvas.

    I'm used qtDesigner for create my form, and i prefer to continue to using it.
    Then i add to my form a QGraphicView and i try to add a QGraphicScene to it but i didn't.

    PS: I hope now you understand. I can't explain better because I can't speak english very well...

  • You can use designer to add your QGraphicsView widget, as you did. However, I believe you'll have to write the code yourself to add a slot which does the work you desire. Designer is a great tool for designing the layout of widgets, but it isn't intended to add the rest of the logic and other non-widget items.

  • Ok, is that I did.

    I add a QGraphicsView and a QPushButton using designer.
    Now I have to create a slot for the button that add a QGraphicScene to the graphicView.
    Can this be a example of the slot?
    @void mineSlots(){

    //get the object to plot here
    QGraphicsScene* scene = new QGraphicsScene();
    //exemple of an object to plot
    scene->addLine(0, 0, -50, 0);
    scene->addLine(-50, 0, -50, -50);
    scene->addLine(-50, -50, 0, 0);


  • yes, although as far as I know, the graphicsView does not take ownership of the scene. Thus, if the user presses the button again, the old scene doesn't get deleted.

    So either don't create a new scene every time the user presses the button (but instead, just add the points to the existing scene) or somehow store the scene and remove the old one after creating a new one ;)

  • If you create a new scene every time, you may run into a memory leak if you don't get rid of the old one first. You might consider creating the scene in the constructor, then just reusing it every time you need to.

  • Ok! Thanks a lot! I'm gonna try!

    If I have other problems i will ask! ;)

Log in to reply