Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Mirroring a Pixmap Item around an axis

    General and Desktop
    qgraphicspixmap transform rotation c++ qt5
    2
    12
    1121
    Loading More Posts
    • 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.
    • G
      Giuseppe97 last edited by Giuseppe97

      In a game I'm developing I have a Mario class :

      class Mario : public QGraphicsPixmapItem
      

      and when right arrow key is pressed I want to change the texture of Mario item from the first picture to the second one below
      (every square in the picture is one pixel):

      def.png

      In order to achieve this, my idea was to mirror the first texture in the picture above around the green axis . I actually have tried to express this idea in the following code :

      if (e->key() == Qt::Key_Right)
      	mario->setPixmap(mario->pixmap().transformed(QTransform().scale(-1, 1)));
      
      

      but I obtain this situation :

      def2png.png

      What is a way to achieve a mirroring of the texture around an axis which is not the 'Z' one but the green one you see in the first picture I have posted?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        What do you get if you use the mirrored method ?

        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 Reply Quote 1
        • G
          Giuseppe97 last edited by Giuseppe97

          @SGaist Hi. That method applies to a QImage and in my case I have tried to apply it to the pixmap (returned by pixmap() )of the item but there is such an item because a QImage is a Qpixmape but not viceversa. Am I right?

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            I am not sure I am following you because your transformed call is also applied directly to your QImage.

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

            G 1 Reply Last reply Reply Quote 1
            • G
              Giuseppe97 @SGaist last edited by Giuseppe97

              @SGaist In this line of code

              mario->setPixmap(mario->pixmap().transformed(QTransform().scale(-1, 1)));
              

              where is QImage? I haven't understood where I should apply the mirrored method

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                My bad ! I mixed both classes.

                What I had in mind was to apply the mirroring or the original QImage and then load it in the item,

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

                G 1 Reply Last reply Reply Quote 0
                • G
                  Giuseppe97 @SGaist last edited by

                  @SGaist Do you have any other idea? I can't figure out by myself and I was hoping someone with more experience than me would have had some hints.

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    Well, why not use the setTransform method if your item directly ?

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

                    G 1 Reply Last reply Reply Quote 1
                    • G
                      Giuseppe97 @SGaist last edited by Giuseppe97

                      @SGaist ok so something like this

                      mario->setPixmap(mario->pixmap().transformed(setTransform()));
                      

                      and how can I achieve the mirroring towards that green axis?

                      1 Reply Last reply Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        No, I suggested to transform the item not it's content hence the link I provided to the QGraphicsItem::setTransform method.

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

                        G 1 Reply Last reply Reply Quote 1
                        • G
                          Giuseppe97 @SGaist last edited by Giuseppe97

                          @SGaist OK , now I am following you. So according to you in order to mirror mario ,through setTransform method, I should do
                          something like:

                          mario->setTransform()
                          

                          and I should chain two consecutive transformations (e.g TRANSLATE + SCALE(-1,1) ) by passing an opportune matrix to the setTransform method. Am I right?

                          If so, could you show me through a small piece of code how to chain a translation plus a scale operation?

                          1 Reply Last reply Reply Quote 0
                          • SGaist
                            SGaist Lifetime Qt Champion last edited by

                            Did you already read the Graphics View Overview in Qt's documentation ?

                            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 Reply Quote 1
                            • First post
                              Last post