Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Array or list for drawing multiple shapes
QtWS25 Last Chance

Array or list for drawing multiple shapes

Scheduled Pinned Locked Moved General and Desktop
qlistqpaintevent
6 Posts 3 Posters 3.4k Views
  • 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.
  • 6 Offline
    6 Offline
    6ort
    wrote on 7 Oct 2015, 15:29 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 7 Oct 2015, 17:18 last edited by mrjj 10 Jul 2015, 17:23
      #2

      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/

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 7 Oct 2015, 20:04 last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        6 1 Reply Last reply 7 Oct 2015, 22:36
        0
        • 6 Offline
          6 Offline
          6ort
          wrote on 7 Oct 2015, 22:03 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • S SGaist
            7 Oct 2015, 20:04

            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.

            6 Offline
            6 Offline
            6ort
            wrote on 7 Oct 2015, 22:36 last edited by
            #5

            @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 :)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 7 Oct 2015, 22:45 last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              1/6

              7 Oct 2015, 15:29

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved