Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Array or list for drawing multiple shapes



  • Hi,

    I am building a Qt painting program for my master's thesis and have a technical hurdle.

    The user can select a couple of drawing modes (freehand, rectangle, ellipse, line, and so forth) and create shapes.
    The shapes are stored in a QList<Shape> and the PaintEvent iterates through the QList to draw all of them (just like in the undo example).

    However, I want to give each Shape it's own inherited class like Line, Rectangle, Ellipse, so I can give them specific functions like getRect, getLine, etc. (in particular I need a special function to detect when the mouse is hovering over the line to select it, but that's another issue).

    The QList<Shape> I am currently using will not let me add anything other than Shape objects, so I can't add these types of objects to the list.

    The way the PaintEvent is coded is it uses a switch to get the type of the current indexed list item, and then based on that it determines what to draw like (pseudo-code):
    case Rectangle:
    painter.drawRect(shapeList.at(var)->getRect());
    break;
    case Ellipse:
    painter.drawEllipse(shapeList.at(var)->getEllipse());
    break;

    etc.

    But it will simply give me an error that for example getLine is not a member of Shape and so forth... even though there is a Line object at that index.

    I'm probably doing this all the wrong way though, so my question basically is: how can I give each shape it's own class but preferably keep all these objects in a single list/container to draw from, while still being able to call their individual functions?


  • Lifetime Qt Champion

    Hi
    you must use pointers to the object to be allowed to use them
    polymorph.

    QList<Shape*>

    then you also insert subclasses of Shape. Like Line etc.

    It's not possible to use object directly (non *) as object slicing would happen.
    http://stackoverflow.com/questions/274626/what-is-object-slicing

    I assume you understand virtual function and that Shape could have a Paint function and the subclasses also have a paint function and you can just call Qlist[0]->Paint() and the right paint will be called with no need for switch case pr type. If you use pointers. :)
    see here
    http://www.cplusplus.com/doc/tutorial/polymorphism/


  • Lifetime Qt Champion

    Hi,

    Out of curiosity, what is your thesis subject and why do you need to implement a painting program for it ? It's to know if there's could be a better/simpler way to get what you need.



  • This post is deleted!


  • @SGaist My subject is design of graphical user interfaces. I already picked out the design of a painting program so I can't change any details about that.

    It looks like I need to do some reading about polymorphism. That is a concept I clearly don't understand well enough yet. Thanks for the pointer, mrjj :)


  • Lifetime Qt Champion

    Rather than going up-front with building your own GUI for a painting program, I'd recommend taking a look at what is currently existing like Krita or KolourPaint. Painting is a vast subject that can cover many aspects. Just take a look at Qt's examples on the subject.

    You should also take the time to look at QtQuick for the GUI design part.


Log in to reply