Home c++ I can not understand how Qpainterpath works in Qt

I can not understand how Qpainterpath works in Qt

Author

Date

Category

I understand the rice in Qt, I try to draw my graphic figure using the QpainterPath . To do this, create a class of Class PuzzlePiece: Public QGraphicspathitem . In it all logic on drawing. The graphic object will have the size p_size . I create an object qpainterpath PP;

pp.moveto (-p_size / 2, -p_size / 2);
pp.Lineto (-P_SIZE / 16, -P_SIZE / 2);
PP.ARCTO (-P_SIZE / 8, -P_SIZE / 2 -P_SIZE / 8,
         P_SIZE / 8, P_SIZE / 8, 270, -90.0);
PP.ARCTO (-P_SIZE / 8, -P_SIZE / 2 -P_SIZE / 8 -P_SIZE / 16,
         P_SIZE / 4, P_SIZE / 4, 180, -180.0);
PP.ARCTO (0, -P_SIZE / 2 -P_SIZE / 8,
         P_SIZE / 8, P_SIZE / 8, 0, -90.0);
pp.Lineto (P_SIZE / 2, -P_SIZE / 2);

Only part of the figure (only 3 arc circumference) is drawn at this stage. However, when adding another line pp.Lineto (P_SIZE / 2, P_SIZE / 2); adds the remaining part (two horizontal lines).
It looks like this:

Please tell me why the drawing is inconsistently and what does it depend on? Thank you.

Full PUZZZLEPIECE.CPP code:

#include "Puzzlepiece.h"
  #Include & lt; Qpainter & gt;
  #Include & lt; Qdebug & gt;
  Puzzlepiece :: Puzzlepiece (Connectorposition North, ConnectorPosition East,
               ConnectorPosition South, ConnectorPosition West) {
    sides [0] = north;
    sides [1] = East;
    sides [2] = South;
    sides [3] = West;
    PPIX = QPIXMAP ("C: / Users / Public / Pictures / Sample Pictures / Chrysanthemum.jpg");
    P_SIZE = 200;
    Qpainterpath PP;
    CONSTRUCTPP (PP);
    SetPath (PP);
  }
  // Void Puzzlepiece :: Constructshape (Qpainterpath & amp; pp) {
  // pp.moveto (-p_size / 2, -p_size / 2);
  // SWITCH (sides [0]) {
  // Case None:
  // pp.Lineto (P_SIZE / 2, -P_SIZE / 2);
  // Break;
  // Case Out:
  // pp.Lineto (-P_SIZE / 16, -P_SIZE / 2);
  // PP.ARCTO (-P_SIZE / 8, -P_SIZE / 2 -P_SIZE / 8,
  // P_SIZE / 8, P_SIZE / 8, 270, -90.0);
  // pp.arcto (-p_size / 8, -p_size / 2 -p_size / 8 -p_size / 16,
  // P_SIZE / 4, P_SIZE / 4, 180, -180.0);
  // pp.arcto (0, -p_size / 2 -p_size / 8,
  // P_SIZE / 8, P_SIZE / 8, 0, -90.0);
  // pp.Lineto (P_SIZE / 2, -P_SIZE / 2);
  // Break;
  // Case in:
  // pp.Lineto (-P_SIZE / 16, -P_SIZE / 2);
  // pp.arcto (-P_SIZE / 8, -P_SIZE / 2,
  // P_SIZE / 8, P_SIZE / 8, 90, 90.0);
  // pp.arcto (-P_SIZE / 8, -P_SIZE / 2 -P_SIZE / 16,
  // P_SIZE / 4, P_SIZE / 4, 180, 180.0);
  // pp.arcto (0, -p_size / 2,
  // P_SIZE / 8, P_SIZE / 8, 0, 90.0);
  // pp.Lineto (P_SIZE / 2, -P_SIZE / 2);
  // Break;
  //}
  // SWITCH (Sides [1]) {
  // Case None:
  // pp.Lineto (P_SIZE / 2, P_SIZE / 2);
  // Break;
  // Case Out:
  // pp.Lineto (P_SIZE / 2, -P_SIZE / 16);
  // pp.arcto (P_SIZE / 2, -P_SIZE / 8,
  // P_SIZE / 8, P_SIZE / 8, 180, -90.0);
  // pp.arcto (P_SIZE / 2 -P_SIZE / 16, -P_SIZE / 8,
  // P_SIZE / 4, P_SIZE / 4, 90, -180.0);
  // pp.arcto (P_SIZE / 2, 0,
  // P_SIZE / 8, P_SIZE / 8, 270, -90.0);
  // pp.Lineto (P_SIZE / 2, P_SIZE / 2);
  // Break;
  // Case in:
  // pp.Lineto (P_SIZE / 2, -P_SIZE / 16);
  // PP.ARCTO (P_SIZE / 2 -P_SIZE / 8, -P_SIZE / 8,
  // P_SIZE / 8, P_SIZE / 8, 0, 90.0); 
// PP.ARCTO (P_SIZE / 2 -P_SIZE / 8 -P_SIZE / 16, -P_SIZE / 8,
  // P_SIZE / 4, P_SIZE / 4, 90, 180.0);
  // pp.arcto (P_SIZE / 2 -P_SIZE / 8, 0,
  // P_SIZE / 8, P_SIZE / 8, 270, 90.0);
  // pp.Lineto (P_SIZE / 2, P_SIZE / 2);
  // Break;
  //}
  // Switch (Sides [2]) {
  // Case None:
  // pp.Lineto (-P_SIZE / 2, P_SIZE / 2);
  // Break;
  // Case Out:
  // pp.Lineto (P_SIZE / 16, P_SIZE / 2);
  // pp.arcto (0, P_SIZE / 2,
  // P_SIZE / 8, P_SIZE / 8, 90, -90.0);
  // PP.ARCTO (-P_SIZE / 8, P_SIZE / 2 -P_SIZE / 16,
  // P_SIZE / 4, P_SIZE / 4, 0, -180.0);
  // pp.arcto (-P_SIZE / 8, P_SIZE / 2,
  // P_SIZE / 8, P_SIZE / 8, 180, -90.0);
  // pp.Lineto (-P_SIZE / 2, P_SIZE / 2);
  // Break;
  // Case in:
  // pp.Lineto (P_SIZE / 16, P_SIZE / 2);
  // PP.ARCTO (0, P_SIZE / 2 -P_SIZE / 8,
  // P_SIZE / 8, P_SIZE / 8, 270, 90.0);
  // PP.ARCTO (-P_SIZE / 8, P_SIZE / 2 -P_SIZE / 8 -P_SIZE / 16,
  // P_SIZE / 4, P_SIZE / 4, 0, 180.0);
  // PP.ARCTO (-P_SIZE / 8, P_SIZE / 2 -P_SIZE / 8,
  // P_SIZE / 8, P_SIZE / 8, 180, 90.0);
  // pp.Lineto (-P_SIZE / 2, P_SIZE / 2);
  // Break;
  //}
  // SWITCH (Sides [3]) {
  // Case None:
  // pp.Lineto (-P_SIZE / 2, -P_SIZE / 2);
  // Break;
  // Case Out:
  // pp.Lineto (-P_SIZE / 2, P_SIZE / 16);
  // pp.arcto (-P_SIZE / 2 -P_SIZE / 8, 0,
  // P_SIZE / 8, P_SIZE / 8, 0, -90.0);
  // pp.arcto (-p_size / 2 -p_size / 8 -p_size / 16, -p_size / 8,
  // P_SIZE / 4, P_SIZE / 4, 270, -180.0);
  // PP.ARCTO (-P_SIZE / 2 -P_SIZE / 8, -P_SIZE / 8,
  // P_SIZE / 8, P_SIZE / 8, 90, -90.0);
  // pp.Lineto (-P_SIZE / 2, -P_SIZE / 2);
  // Break;
  // Case in:
  // pp.Lineto (-P_SIZE / 2, P_SIZE / 16);
  // pp.arcto (-P_SIZE / 2, 0,
  // P_SIZE / 8, P_SIZE / 8, 180, 90.0);
  // PP.ARCTO (-P_SIZE / 2 -P_SIZE / 16, -P_SIZE / 8,
  // P_SIZE / 4, P_SIZE / 4, 270, 180.0);
  // pp.arcto (-P_SIZE / 2, -P_SIZE / 8,
  // P_SIZE / 8, P_SIZE / 8, 90, 90);
  // pp.Lineto (-P_SIZE / 2, -P_SIZE / 2);
  // Break;
  //}
  //}
  Void PuzzlePiece :: ConstructPP (QPAINTERPATH & AMP; PP) {
    pp.moveto (-p_size / 2, -p_size / 2);
    pp.Lineto (-P_SIZE / 16, -P_SIZE / 2);
    PP.ARCTO (-P_SIZE / 8, -P_SIZE / 2 -P_SIZE / 8,
             P_SIZE / 8, P_SIZE / 8, 270, -90.0);
    PP.ARCTO (-P_SIZE / 8, -P_SIZE / 2 -P_SIZE / 8 -P_SIZE / 16,
             P_SIZE / 4, P_SIZE / 4, 180, -180.0);
    PP.ARCTO (0, -P_SIZE / 2 -P_SIZE / 8,
             P_SIZE / 8, P_SIZE / 8, 0, -90.0);
    pp.Lineto (P_SIZE / 2, -P_SIZE / 2);
    pp.Lineto (P_SIZE / 2, P_SIZE / 2);
  }
  Void Puzzleter * Painter, Const QstyleOptionGraphicsItem * Option, QWidget * Widget) {
    Painter- & gt; setclippath (path ());
    Painter- & gt; setPen (QT :: Black, 3));
    QRect B_Rect = BoundingRect (). Torect ();
    Painter- & gt; drawpixmap (b_rect.x (), b_rect.y (), pixmap ());
    Painter- & gt; drawpath (path ());
  }
  QPIXMAP PUZZLEPIECE :: PIXMAP () {
    Return PPIX;
  }
  Void Puzzlepiece :: SetPixmap (Qpixmap & amp; PIX) {
    PPIX = PIX;
    Update ();
  }

Full PUZZZLEPIECE.H code:

#ifndef puzzlepiece_h
  #Define Puzzlepiece_h.
  #include & lt; QGraphicspathitem & gt;
  Class Puzzlepiece: Public QGraphicspathitem
  {
  Public:
    Enum ConnectorPosition {None, Out, In}; 
Puzzlepiece (ConnectorPosition, ConnectorPosition, ConnectorPosition, ConnectorPosition);
    QPIXMAP Pixmap ();
    void setpixmap (qpixmap & amp;);
  Private:
    INT P_SIZE;
    QPIXMAP PPIX;
    ConnectorPosition Sides [4];
    void constructpp (qpainterpath & amp; pp);
    void constructshape (qpainterpath & amp;);
    Void Paint (Qpainter * Painter,
         Const QStyleOptionGraphicsItem * Option,
         Qwidget * widget = nullptr);
  };
  #endif // Puzzlepiece_h

main.cpp:

# include & lt; qapplication & gt;
#include "ConfigurationDialog.h"
#Include & lt; Qdebug & gt;
#Include & lt; QGraphicsView & GT;
#Include "Puzzlepiece.h"
Int Main (int argc, char * argv [])
{
  Qapplication A (ARGC, ARGV);
  QGraphicsView View;
  QGraphicsScene Scene;
  Scene.Additem (New Puzzlepiece (Puzzlepiece :: In, Puzzlepiece :: Out,
                 Puzzlepiece :: In, Puzzlepiece :: Out));
  View.Setscene (& amp; Scene);
  view.show ();
// ConfigurationDialog C;
// C.Show ();
  Return a.exec ();
}

Answer 1, Authority 100%

Code helped you have a mistake in the drawing method.

painter- & gt; setclippath (path ());
Painter- & gt; setPen (QT :: Black, 3));
...
Painter- & gt; drawpath (path ());

The first action of the drawing you limit the drawing area with a puzzle circuit and only then draw this circuit with a thickness of the line 3. In fact it turns out that, since the line thickness 3 pixels you see only part of your contour. When you add a string with a drawing of a vertical line, then the overall circuit size changes and you see those parts that could not see before.

Proper drawing will look like this:

void puzzlepiece :: paint (QStyleoptionGraphicsItem * option, QWidget * Widget)
{
  Painter- & gt; setPen (QT :: Black, 3));
  Painter- & gt; drawpath (path ());
  Painter- & gt; setclippath (path ());
  QRect B_Rect = BoundingRect (). Torect ();
  Painter- & gt; drawpixmap (b_rect.x (), b_rect.y (), pixmap ());
}

First draw the contour, then we put the cutting along the contour and draw a pixmap there.
Try)

p.s. Try to enable the anti-aliasing option for Paintera, the draw will be on such a pixel

painter- & gt; setRenderhint (Qpainter :: Antialiasing);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions