Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Complex 2D graphics in QtQuick via C++
QtWS25 Last Chance

Complex 2D graphics in QtQuick via C++

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 4 Posters 5.5k 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.
  • A Offline
    A Offline
    Alexey
    wrote on last edited by
    #1

    Hi,
    my goal is to implement custom QML element in C++. The purpose of this element is to draw rather complex 2D graphics (curves, polygons and polylines, text labels). And I also need to support mouse events handling (for example, editing polyline with mouse).

    In previous version of Qt I could reimplement method paint(QPainter *painter, ...) of QDeclarativeItem and use QPainter to perform graphics and text drawing, but in Qt 5 this approach is deprecated (see QQuickPaintedItem docs).

    Currently I see two possibilites (described in Qt 5 docs):

    1. subclass QQuickItem
    2. write C++ QML plugin

    Could you, please, advise me which of this two possibilities is more preferrable and suitable for my purposes (if any)?

    Thanks in advance!

    1 Reply Last reply
    0
    • podsvirovP Offline
      podsvirovP Offline
      podsvirov
      wrote on last edited by
      #2

      I think you'd better write your plugin.
      How to create plugins for QML you can read "here":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-modules-cppplugins.html.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mutaphysis
        wrote on last edited by
        #3

        If you want to expose visual elements from C++ to QML you will end up subclassing QQuickItem anyway.

        Whether you do that from your own application or from a plugin depends on the usecase, if you don't need the components in other application, then just registering the components from within your application is way easier to manage. A plugin is just overhead in most cases.

        There are also other ways to get complex 2D graphics to QML:

        You could implement a custom "QQuickImageProvider":http://qt-project.org/doc/qt-5.0/qtquick/qquickimageprovider.html and expose graphs this way.

        Also you can draw directly in QML using the "QuickCanvas":http://qt-project.org/doc/qt-5.0/qtquick/quick-canvas.html.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Alexey
          wrote on last edited by
          #4

          [quote author="mutaphysis" date="1360702369"]If you want to expose visual elements from C++ to QML you will end up subclassing QQuickItem anyway.

          There are also other ways to get complex 2D graphics to QML:

          You could implement a custom "QQuickImageProvider"
          Also you can draw directly in QML using the "QuickCanvas"[/quote]

          Thank you, mutaphysis! I'm trying to use QQuickItem - I've managed to draw lines and curves, but I can not figure out how can I draw a text with QQuickItem! Do you know?

          I need to support editing graphics, so, I think, QQuickImageProvider is not appropriate. And I need to do graphics from C++, so Canvas is not appropriate too.

          [quote author="Konstantin Podsvirov" date="1360680199"]I think you'd better write your plugin.
          [/quote]

          Thank you, Konstantin! Does plugin provides a possibility to draw a text?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chrisadams
            wrote on last edited by
            #5

            Hi,

            See QQuickPaintedItem for this use case.
            http://qt-project.org/doc/qt-5.0/qtquick/qquickpainteditem.html

            Cheers,
            Chris.

            1 Reply Last reply
            0
            • podsvirovP Offline
              podsvirovP Offline
              podsvirov
              wrote on last edited by
              #6

              Creating a plug-in (aka module) allows you to easily re-use your components.
              The base class, use "QQuickPaintedItem":http://qt-project.org/doc/qt-5.0/qtquick/qquickpainteditem.html.
              Override the "QQuickPaintedItem::paint":http://qt-project.org/doc/qt-5.0/qtquick/qquickpainteditem.html#paint.
              You can draw text with the "painter->drawText":http://qt-project.org/doc/qt-5.0/qtgui/qpainter.html#drawText

              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 Extensions
              • Unsolved