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. QGraphicsItem::shape
Forum Updated to NodeBB v4.3 + New Features

QGraphicsItem::shape

Scheduled Pinned Locked Moved General and Desktop
22 Posts 2 Posters 16.0k 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.
  • J Offline
    J Offline
    Jupiter
    wrote on last edited by
    #1

    hi,

    I want to set a QPainterPath containing an ellipse as shape of a Pixmap Item.

    @QPainterPath p;
    p.addEllipse(0, 0, 50, 50);
    return p;@ is the code of my PainterPath.

    it is also clipped because i set QGraphicsItem::ItemClipsToShape .

    However, it looks very strange !http://www.image-load.net/users/public/images/MTIhXoLlBA.png(Screenshot)!

    Why does it have such a jagged border?

    any suggestions to fix it?

    I already tried @setRenderHint(QPainter::Antialiasing);@

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jupiter
      wrote on last edited by
      #2

      i forgot to mention, Im using raster paint engine on MacOS Lion

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sigrid
        wrote on last edited by
        #3

        Are you handling the item in the same coordinate space? I.e. are you doing clipping based on item coordinates or on scene coordinates? If you are mixing the two it could cause the problem you are having.

        If mixing of the two is not the problem, then I would need more information on what exactly you are doing. Can you provide a small, complete example that shows the problem? That would make it easier to investigate what the cause of the problem can be.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jupiter
          wrote on last edited by
          #4

          i just set the shape in the item, and set the flag ItemClipsToShape in the same item. so AFAIK its both in the item coordinate system

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jupiter
            wrote on last edited by
            #5

            ok when i set the same path as clipPath to the painter in the paint method, its not a problem. it looks like it should. but that doesn't solve my problem, since i don't have collision detection on the shape then

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sigrid
              wrote on last edited by
              #6

              I am unable to reproduce this problem with the example below. Does my example reproduce the problem for you? If not, can you modify it so that it does? Which version of Qt are you using and on which platform?

              @
              #include <QtGui>

              class GraphicsItem : public QGraphicsPixmapItem
              {

              public:
              GraphicsItem()
              {
              QPixmap pix("logo.png");
              setFlags(flags() | QGraphicsItem::ItemClipsToShape);
              setPixmap(pix);
              }
              QPainterPath shape () const
              {
              QPainterPath p;
              p.addEllipse(0, 0, 50, 50);
              return p;
              }};
              class GraphicsView : public QGraphicsView
              {
              Q_OBJECT
              public:
              GraphicsView()
              {
              QGraphicsScene *myScene = new QGraphicsScene(this);
              setScene(myScene);
              GraphicsItem *myItem = new GraphicsItem();
              myScene->addItem(myItem);
              setRenderHint(QPainter::Antialiasing);

              }

              };

              #include "main.moc"

              int main(int argc, char** argv)
              {
              QApplication app(argc, argv);
              GraphicsView window;
              window.show();
              return app.exec();

              }
              @

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jupiter
                wrote on last edited by
                #7

                ok i can't reproduce it either with your code, i have to find the difference.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jupiter
                  wrote on last edited by
                  #8

                  sigrid what exactly do you mean with handling the item in the same coordinate space? that is always given, when i implement the shape method isn't it?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sigrid
                    wrote on last edited by
                    #9

                    It is usually a given if you work with only the item functions, but you could be taking coordinates from the scene and not translating them to the item for example and then that could cause problems.

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jupiter
                      wrote on last edited by
                      #10

                      no i don't do that.

                      also if i use the path in the paint method, and clip manually to it using (QPainter::fillPath) it works fine. but why is it a problem using the shape method?

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        sigrid
                        wrote on last edited by
                        #11

                        I need to see an example in order to see what goes wrong in that case for you. Can you modify the example I sent previously so that it reproduces the issue you are having?

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          Jupiter
                          wrote on last edited by
                          #12

                          i tried, but didn't work :)

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            Jupiter
                            wrote on last edited by
                            #13

                            hi sigrid.

                            i finally managed to change your code to show the same problem.

                            @#include <QtGui>

                            class GraphicsItemParent :public QGraphicsItem {
                            public:
                            GraphicsItemParent() {
                            setFlag(ItemClipsChildrenToShape);
                            }

                            QRectF boundingRect() const {
                                return QRectF(0, 0, 200, 200);
                            }
                            
                            void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
                                painter->setPen(Qt::black);
                                painter->drawRect(boundingRect());
                            }
                            

                            };

                            class GraphicsItem : public QGraphicsItem
                            {

                            public:
                            GraphicsItem(QColor c, QGraphicsItem* parent = 0):QGraphicsItem(parent)
                            {
                            setFlags(flags() | QGraphicsItem::ItemClipsToShape);
                            setFlag(ItemIsMovable);
                            color = c;
                            }

                            QRectF boundingRect() const {
                            return QRectF(0,0, 100, 100);
                            }
                            void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
                            painter->setRenderHint(QPainter::Antialiasing);
                            painter->fillPath(shape(), color);
                            }

                            QPainterPath shape () const
                            {
                            QPainterPath p;
                            p.addEllipse(50, 50, 50, 50);
                            return p;
                            }
                            QColor color;
                            };
                            class GraphicsView : public QGraphicsView
                            {
                            Q_OBJECT
                            public:
                            GraphicsView()
                            {
                            QGraphicsScene myScene = new QGraphicsScene(this);
                            setScene(myScene);
                            GraphicsItemParent
                            parent = new GraphicsItemParent();
                            myScene->addItem(parent);
                            GraphicsItem *myItem1 = new GraphicsItem(Qt::red, parent);
                            GraphicsItem *myItem2 = new GraphicsItem(Qt::green, parent);
                            myItem1->setPos(50, 50);
                            myItem2->setRotation(15);
                            setSceneRect(0, 0, 100, 100);
                            scale(3, 3);
                            setRenderHint(QPainter::Antialiasing);

                            }

                            };

                            #include "main.moc"

                            int main(int argc, char** argv)
                            {
                            QApplication app(argc, argv);
                            GraphicsView window;
                            window.show();
                            return app.exec();

                            }@

                            as you can see, i only changed to draw a colored circle instead of an image (same behavior with image). as well as i added a parent item.

                            now the both circles are jagged, as well as the green circle is clipped to the parents shape with jags because of its rotation.

                            What is the best work around to get it painted probably?

                            thanks

                            Nazgul

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              Jupiter
                              wrote on last edited by
                              #14

                              oh i forgot to say, that I'm also scaling the view. but imo that shouldn't make the difference.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                sigrid
                                wrote on last edited by
                                #15

                                I do not see that problem here when running your example on Windows using Qt 4.7.3. Which Qt version are you using? Do you only see this problem on Mac? Can you send a screenshot of what you see when running the example you sent?

                                1 Reply Last reply
                                0
                                • J Offline
                                  J Offline
                                  Jupiter
                                  wrote on last edited by
                                  #16

                                  I'm using Qt 4.7.4 on Mac. I currently don't have a windows machine to test it there.

                                  Here is the screen:

                                  !http://www.image-load.net/users/public/images/IzBG3bbCdy.png(screenshot)!

                                  1 Reply Last reply
                                  0
                                  • J Offline
                                    J Offline
                                    Jupiter
                                    wrote on last edited by
                                    #17

                                    as you can see, it becomes also jagged, when it clips to the rectangle

                                    1 Reply Last reply
                                    0
                                    • J Offline
                                      J Offline
                                      Jupiter
                                      wrote on last edited by
                                      #18

                                      ok, i now tried with Qt 4.7.3 on Mac and got the same results as with qt 4.7.4.

                                      then i tried on a windows machine (Windows Vista) also with qt 4.7.3 .

                                      On windows there was a regular clipping like intended. no jags at all, like you experienced too.

                                      is it a Qt Bug?

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        sigrid
                                        wrote on last edited by
                                        #19

                                        This sounds like a Qt bug, I suggest you report it in "Jira":https://bugreports.qt.nokia.com//secure/Dashboard.jspa and include the example that reproduces the problem and the screenshots as well.

                                        1 Reply Last reply
                                        0
                                        • J Offline
                                          J Offline
                                          Jupiter
                                          wrote on last edited by
                                          #20

                                          ok i created one.

                                          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