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. Modify QPainterPath object
Forum Updated to NodeBB v4.3 + New Features

Modify QPainterPath object

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.1k Views 3 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.
  • A Offline
    A Offline
    Annabela_Cortez
    wrote on last edited by
    #1

    Hi,
    I have a class "Node" which is inherited from QGraphicsItem class, and there is a method called shape where I wanna use ellipse to draw node.

    QPainterPath Cvor::shape() const
    {
        QPainterPath temp;
        temp.addEllipse(-10, -10, 30, 30);
        return temp;
    }
    

    I want to make this ellipse bigger, but I can't make it. Size is very small and I think it's default. Is there any way to resize it?
    Thanks in advance

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

      Hi
      Make sure your
      https://doc.qt.io/qt-5/qgraphicsitem.html#boundingRect
      returns a rect big enough for the ellipse you want.
      Also note the info about calling prepareGeometryChange()
      as else you get odd redrawing.

      A 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        Make sure your
        https://doc.qt.io/qt-5/qgraphicsitem.html#boundingRect
        returns a rect big enough for the ellipse you want.
        Also note the info about calling prepareGeometryChange()
        as else you get odd redrawing.

        A Offline
        A Offline
        Annabela_Cortez
        wrote on last edited by
        #3

        @mrjj It's not working. I tried

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

          Hi,

          What about drawing an ellipse that takes into account the size of your item ?

          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
          • A Annabela_Cortez

            @mrjj It's not working. I tried

            KillerSmathK Offline
            KillerSmathK Offline
            KillerSmath
            wrote on last edited by
            #5

            @Annabela_Cortez

            1. How are you overriding these functions: Boundingrect, Shape, Paint ?

            Boundingrect - notifies the view about item area (necessary for redraw) and make your item supports click events.
            Shape - is used for more accurate collision detection or Clip Area (when QGraphicsItem::ItemClipsToShape flag is activated)

            1. Are you trying to redefine the node size at run time? If yes, have you tried to store a QRect item or a Size Variable? Because you could redefine the size and call prepareGeometryChanges ().
            YourItem::YourItem()
            {
               _size = 10; // radius
            }
            
            QRectF YourItem::boundingRect() const
            {
                return QRectF(-_size/2, -_size/2, _size, _size);
            }
            
            void YourItem::setSize(qreal size)
            {
                _size = size;
                prepareGeometryChange();
            }
            

            @Computer Science Student - Brazil
            Web Developer and Researcher
            “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

            A 1 Reply Last reply
            1
            • KillerSmathK KillerSmath

              @Annabela_Cortez

              1. How are you overriding these functions: Boundingrect, Shape, Paint ?

              Boundingrect - notifies the view about item area (necessary for redraw) and make your item supports click events.
              Shape - is used for more accurate collision detection or Clip Area (when QGraphicsItem::ItemClipsToShape flag is activated)

              1. Are you trying to redefine the node size at run time? If yes, have you tried to store a QRect item or a Size Variable? Because you could redefine the size and call prepareGeometryChanges ().
              YourItem::YourItem()
              {
                 _size = 10; // radius
              }
              
              QRectF YourItem::boundingRect() const
              {
                  return QRectF(-_size/2, -_size/2, _size, _size);
              }
              
              void YourItem::setSize(qreal size)
              {
                  _size = size;
                  prepareGeometryChange();
              }
              
              A Offline
              A Offline
              Annabela_Cortez
              wrote on last edited by
              #6

              @KillerSmath I tried, but still the same

              KillerSmathK 1 Reply Last reply
              0
              • A Annabela_Cortez

                @KillerSmath I tried, but still the same

                KillerSmathK Offline
                KillerSmathK Offline
                KillerSmath
                wrote on last edited by
                #7

                @Annabela_Cortez
                Did you read my first question ?

                How are you overriding these functions: Boundingrect, Shape, Paint ?

                We cannot help you without a knowledge of how you are implementing your solution.

                @Computer Science Student - Brazil
                Web Developer and Researcher
                “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                A 1 Reply Last reply
                2
                • KillerSmathK KillerSmath

                  @Annabela_Cortez
                  Did you read my first question ?

                  How are you overriding these functions: Boundingrect, Shape, Paint ?

                  We cannot help you without a knowledge of how you are implementing your solution.

                  A Offline
                  A Offline
                  Annabela_Cortez
                  wrote on last edited by Annabela_Cortez
                  #8
                  QRectF Cvor::boundingRect() const
                  {
                      qreal adjust = 2;
                      return QRectF(-10 - adjust, -10 - adjust,23 + adjust, 23 + adjust);
                  }
                  
                  QPainterPath Cvor::shape() const
                  {
                      QPainterPath temp;
                      temp.addEllipse(-10, -10, 30, 30);
                      return temp;
                  }
                  
                  
                  void Cvor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
                  {
                  
                      painter->setPen(Qt::NoPen);
                      painter->setBrush(Qt::darkGray);
                      painter->drawEllipse(-7, -7, 20, 20);
                  
                      QRadialGradient gradient(-3, -3, 10);
                      if (option->state & QStyle::State_Sunken) {
                  
                          gradient.setCenter(3, 3);
                          gradient.setFocalPoint(3, 3);
                          gradient.setColorAt(1, QColor(Qt::black).light(120));
                          gradient.setColorAt(0, QColor(Qt::darkRed).light(120));
                      }
                      else {
                          gradient.setColorAt(0, Qt::black);
                          gradient.setColorAt(1, Qt::darkRed);
                      }
                      painter->setBrush(gradient);
                      painter->setPen(QPen(Qt::black, 0));
                      painter->drawEllipse(-10, -10, 20, 20);
                  }
                  
                  KillerSmathK 1 Reply Last reply
                  0
                  • A Annabela_Cortez
                    QRectF Cvor::boundingRect() const
                    {
                        qreal adjust = 2;
                        return QRectF(-10 - adjust, -10 - adjust,23 + adjust, 23 + adjust);
                    }
                    
                    QPainterPath Cvor::shape() const
                    {
                        QPainterPath temp;
                        temp.addEllipse(-10, -10, 30, 30);
                        return temp;
                    }
                    
                    
                    void Cvor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
                    {
                    
                        painter->setPen(Qt::NoPen);
                        painter->setBrush(Qt::darkGray);
                        painter->drawEllipse(-7, -7, 20, 20);
                    
                        QRadialGradient gradient(-3, -3, 10);
                        if (option->state & QStyle::State_Sunken) {
                    
                            gradient.setCenter(3, 3);
                            gradient.setFocalPoint(3, 3);
                            gradient.setColorAt(1, QColor(Qt::black).light(120));
                            gradient.setColorAt(0, QColor(Qt::darkRed).light(120));
                        }
                        else {
                            gradient.setColorAt(0, Qt::black);
                            gradient.setColorAt(1, Qt::darkRed);
                        }
                        painter->setBrush(gradient);
                        painter->setPen(QPen(Qt::black, 0));
                        painter->drawEllipse(-10, -10, 20, 20);
                    }
                    
                    KillerSmathK Offline
                    KillerSmathK Offline
                    KillerSmath
                    wrote on last edited by
                    #9

                    @Annabela_Cortez
                    Shape has not directly effect on way which your item is painted. The only exception is when QGraphicsItem::ItemClipsToShape flag is activated like i mentioned in the above response.

                    If your goal is change the size of this node then say to the code Paint a bigger node instead of change the Shape value. (Replace YOUR_SIZE to your prefered size).

                    void Cvor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
                    {
                    
                        painter->setPen(Qt::NoPen);
                        painter->setBrush(Qt::darkGray);
                        painter->drawEllipse(-YOUR_SIZE/2 - 3, -YOUR_SIZE/2 - 3, YOUR_SIZE, YOUR_SIZE);
                    
                        QRadialGradient gradient(-3, -3, 10);
                        if (option->state & QStyle::State_Sunken) {
                    
                            gradient.setCenter(3, 3);
                            gradient.setFocalPoint(3, 3);
                            gradient.setColorAt(1, QColor(Qt::black).light(120));
                            gradient.setColorAt(0, QColor(Qt::darkRed).light(120));
                        }
                        else {
                            gradient.setColorAt(0, Qt::black);
                            gradient.setColorAt(1, Qt::darkRed);
                        }
                        painter->setBrush(gradient);
                        painter->setPen(QPen(Qt::black, 0));
                        painter->drawEllipse(-YOUR_SIZE/2, -YOUR_SIZE/2, YOUR_SIZE, YOUR_SIZE);
                    }
                    

                    @Computer Science Student - Brazil
                    Web Developer and Researcher
                    “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                    1 Reply Last reply
                    2

                    • Login

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