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. How to draw triangle points in QtChart?
Forum Updated to NodeBB v4.3 + New Features

How to draw triangle points in QtChart?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 5.5k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    NeeMoo
    wrote on last edited by
    #1

    Hello everyone,

    I want to draw some points with triangle symbol, but the marker shape only support circle and rectangle shape.
    I can create a triangle shape as QImage and send it to the QBrush of QScatterSeries, but if I want to draw I border, I still can only draw a circular or rectangular border.
    Is there anyway I can draw a triangle point with triangle border?
    0_1510718066716_pic.png

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      If your QImage based triangle does the job, then why not update that QImage with a new triangle with the border painted ? It's essentially a question of setting a pen and a brush to your requested colours.

      Hope it helps

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

      N 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        If your QImage based triangle does the job, then why not update that QImage with a new triangle with the border painted ? It's essentially a question of setting a pen and a brush to your requested colours.

        Hope it helps

        N Offline
        N Offline
        NeeMoo
        wrote on last edited by
        #3

        @SGaist Hi SGaist, Thank you for your answering.
        Maybe I didn't make it clear, I want to draw some triangles with border, and others without border, so I have to use setBorderColor or set Pen of QScatterSeries to add the border in some cases.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          If your QImage based triangle does the job, then why not update that QImage with a new triangle with the border painted ? It's essentially a question of setting a pen and a brush to your requested colours.

          Hope it helps

          N Offline
          N Offline
          NeeMoo
          wrote on last edited by
          #4

          @SGaist I rewrited the source code of QtChart and now it works! Thank you all the same!

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What did you modify ?

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

            N 1 Reply Last reply
            0
            • Vinod KuntojiV Offline
              Vinod KuntojiV Offline
              Vinod Kuntoji
              wrote on last edited by
              #6

              @NeeMoo ,

              void Widget::paintEvent(QPaintEvent *event){
              QPainter painter(this);
              QPen pen;
              pen.setWidth(4);
              pen.setColor(Qt::red);
              painter.setBrush(Qt::blue);
              painter.setPen(pen);
              QPointF *points = new QPointF[3];
              points[0] = QPointF(100,50);
              points[1] = QPointF(50,150);
              points[2] = QPointF(150,150);
              painter.drawPolygon(points,3);
              }

              C++, Qt, Qt Quick Developer,
              PthinkS, Bangalore

              1 Reply Last reply
              0
              • SGaistS SGaist

                What did you modify ?

                N Offline
                N Offline
                NeeMoo
                wrote on last edited by
                #7

                @SGaist I rewrited the ScatterChartItem and ScatterSeries, added a TriangleMarker:
                void ScatterChartItem::createPoints(int count)
                {
                for (int i = 0; i < count; ++i) {

                    QGraphicsItem *item = 0;
                
                    switch (m_shape) {
                    case QScatterSeries::MarkerShapeCircle: {
                        item = new CircleMarker(0, 0, m_size, m_size, this);
                        const QRectF &rect = item->boundingRect();
                        item->setPos(-rect.width() / 2, -rect.height() / 2);
                        break;
                    }
                    case QScatterSeries::MarkerShapeRectangle:
                        item = new RectangleMarker(0, 0, m_size, m_size, this);
                        item->setPos(-m_size / 2, -m_size / 2);
                        break;
                	case QScatterSeries::MarkerShapeTriangle:
                		item = new TriangleMarker(0, 0, m_size, m_size, this);
                		item->setPos(-m_size / 2, -m_size / 2);
                    default:
                        qWarning() << "Unsupported marker type";
                        break;
                    }
                    m_items.addToGroup(item);
                }
                

                }

                class TriangleMarker : public QGraphicsPolygonItem
                {

                public:
                TriangleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent)
                : QGraphicsPolygonItem(QPolygonF() << QPointF(x, y + h) << QPointF(x + w, y + h) << QPointF(x + w / 2.0f, y), parent),
                m_parent(parent)
                {
                setAcceptHoverEvents(true);
                setFlag(QGraphicsItem::ItemIsSelectable);
                }

                protected:
                void mousePressEvent(QGraphicsSceneMouseEvent *event)
                {
                QGraphicsPolygonItem::mousePressEvent(event);
                m_parent->markerPressed(this);
                m_parent->setMousePressed();
                }
                void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
                {
                QGraphicsPolygonItem::hoverEnterEvent(event);
                m_parent->markerHovered(this, true);
                }
                void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
                {
                QGraphicsPolygonItem::hoverLeaveEvent(event);
                m_parent->markerHovered(this, false);
                }
                void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
                {
                QGraphicsPolygonItem::mouseReleaseEvent(event);
                m_parent->markerReleased(this);
                if (m_parent->mousePressed())
                m_parent->markerSelected(this);
                m_parent->setMousePressed(false);
                }
                void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
                {
                QGraphicsPolygonItem::mouseDoubleClickEvent(event);
                m_parent->markerDoubleClicked(this);
                }

                private:
                ScatterChartItem *m_parent;
                };

                But the legend of that maker shape is not correct, so it should have a little work on legend.

                J 1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Nice !

                  Did you consider contributing your changes ?

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

                  N 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Nice !

                    Did you consider contributing your changes ?

                    N Offline
                    N Offline
                    NeeMoo
                    wrote on last edited by
                    #9

                    Yeah of course! But I don't know how to contribute my code, I'm a beginner of programming, haha, could you give me some advice?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Sure, the starting point is here. Take your time to go through it. The first time setup might take some time but following the documentation it's not that complicated.

                      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
                      • N NeeMoo

                        @SGaist I rewrited the ScatterChartItem and ScatterSeries, added a TriangleMarker:
                        void ScatterChartItem::createPoints(int count)
                        {
                        for (int i = 0; i < count; ++i) {

                            QGraphicsItem *item = 0;
                        
                            switch (m_shape) {
                            case QScatterSeries::MarkerShapeCircle: {
                                item = new CircleMarker(0, 0, m_size, m_size, this);
                                const QRectF &rect = item->boundingRect();
                                item->setPos(-rect.width() / 2, -rect.height() / 2);
                                break;
                            }
                            case QScatterSeries::MarkerShapeRectangle:
                                item = new RectangleMarker(0, 0, m_size, m_size, this);
                                item->setPos(-m_size / 2, -m_size / 2);
                                break;
                        	case QScatterSeries::MarkerShapeTriangle:
                        		item = new TriangleMarker(0, 0, m_size, m_size, this);
                        		item->setPos(-m_size / 2, -m_size / 2);
                            default:
                                qWarning() << "Unsupported marker type";
                                break;
                            }
                            m_items.addToGroup(item);
                        }
                        

                        }

                        class TriangleMarker : public QGraphicsPolygonItem
                        {

                        public:
                        TriangleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent)
                        : QGraphicsPolygonItem(QPolygonF() << QPointF(x, y + h) << QPointF(x + w, y + h) << QPointF(x + w / 2.0f, y), parent),
                        m_parent(parent)
                        {
                        setAcceptHoverEvents(true);
                        setFlag(QGraphicsItem::ItemIsSelectable);
                        }

                        protected:
                        void mousePressEvent(QGraphicsSceneMouseEvent *event)
                        {
                        QGraphicsPolygonItem::mousePressEvent(event);
                        m_parent->markerPressed(this);
                        m_parent->setMousePressed();
                        }
                        void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
                        {
                        QGraphicsPolygonItem::hoverEnterEvent(event);
                        m_parent->markerHovered(this, true);
                        }
                        void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
                        {
                        QGraphicsPolygonItem::hoverLeaveEvent(event);
                        m_parent->markerHovered(this, false);
                        }
                        void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
                        {
                        QGraphicsPolygonItem::mouseReleaseEvent(event);
                        m_parent->markerReleased(this);
                        if (m_parent->mousePressed())
                        m_parent->markerSelected(this);
                        m_parent->setMousePressed(false);
                        }
                        void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
                        {
                        QGraphicsPolygonItem::mouseDoubleClickEvent(event);
                        m_parent->markerDoubleClicked(this);
                        }

                        private:
                        ScatterChartItem *m_parent;
                        };

                        But the legend of that maker shape is not correct, so it should have a little work on legend.

                        J Offline
                        J Offline
                        JKappes
                        wrote on last edited by
                        #11

                        @NeeMoo Nice! I'm also a beginner right now, so could you explain where exactly the data-point is in relation to the triangle? If I'm interpreting your code right, I think it's in the bottom left corner?
                        Because I would like to have two kinds of TriangleMarker, one pointing up with the data-point at the top corner and one pointing down with the data-point at the bottom corner. To do that I would just have to make two TriangleMarker classes and change the 3 QPoints that make up the QPolygonF, right?

                        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