How to rotate a content of QPixmap without changing size



  • Hello!

    Is there a way, to rotate the content of a QPixmap without changing the size of the new QPixmap?

    I have a PNG image showing the symbol of a cargo ship (see below). Depending on the heading of the ship I want to rotate the symbol.

    I did the following:
    @QPixmap* pixmap = new QPixmap("ship_cargo_vessel_no_rotation.png");
    QTransform transform;
    QTransform trans = transform.rotate(45);
    QPixmap *transPixmap = new QPixmap(pixmap->transformed(trans));@

    The problem is that the size of pixmap is 172x172 but the transformed transPixmap 244x244.
    Is there a way only to rotate the ship but not the QPixmap? The size of the rotated image should be the same and should look like as follows:

    !http://www.rettung-ebreichsdorf.at/ship_cargo_vessel_no_rotation.png(Image not rotated)!
    !http://www.rettung-ebreichsdorf.at/ship_cargo_vessel_rotation.png(Image rotated)!

    By the way, the complete transformation should be of course as fast as possible as the application is time critical and running on an embedded system.

    Regards,
    Rooney



  • You have to scale the pixmap first (in this case: square root of 2) and AFTER rotate it. Because the transformed() returns the smallest pixmap containing all transformed points of the original pixmap.



  • Scaling the pixmap changes its size.
    In your case, as you always know for sure that the actual content fits into 172x172, regardless of the angle, I would do like this: rotate the pixmap, which creates an oversized image and then crop the result, based on the center point:

    @
    QPixmap orig;
    QPixmap rotated = orig.rotated(45);

    int xoffset = (rotated.width() - orig.width()) / 2;
    int yoffset = (rotated.height() - orig.height()) / 2
    rotated = rotated.copy(xoffset, yoffset, orig.width(), orig.height());
    @



  • [quote author="Volker" date="1327495977"]Scaling the pixmap changes its size.[/quote]

    Hi, Volker!
    Just question, don't want a debate, so:
    My version works with matrices in the backbuffer. I think this is the fastest version (am I right?). Your version seems to me a bit slower. My version, when go back to the frontbuffer, will use the SAME pixmap (dimension). Which pixmap is used in your version?



  • @Volker: Works great!

    @broadpeak:
    Did the following...
    @QPixmap* pixmap = new QPixmap("ship_cargo_vessel_no_rotation.png");
    QTransform transform;
    QTransform trans = transform.rotate(45);
    pixmap = new QPixmap(pixmap->scaled(sqrt(pixmap->size().width()), sqrt(pixmap->size().height())));
    QPixmap *transPixmap = new QPixmap(pixmap->transformed(trans));@

    If I display transPixmap there are only a few pixels of the orginal left. This might be the problem by using scale.
    Is there something wrong in my code?



  • No, this won't be good.
    You have to work with the QTransform object, not on the QPixmap object itself.

    Maybe:
    @
    QTransform transform;
    QTransform trans = transform.scale(sqrt(pixmap->size().width()), sqrt(pixmap->size().height()));
    trans = transform.rotate(45);
    QPixmap(pixmap->transformed(trans));
    @

    Hope this helps.



  • @broadpeak

    actually, there is no rotate() method on a QPixmap, so on will have to use a QTransform anyways :)

    If you call pixmap.scaled() with a width an height of sqrt(172) == 13.114 you get a pixmap of size 13x13 pixels. It will never again grow afterwards, the vessel will hardly be visible afterwards. This is surely not the optical effect that rooney wants.

    So the only option I see - besides using pre-calculated pixmaps - is to rotate the pixmap and crop the unnecessary parts afterwards.



  • Hello,

    I am trying to just develop a UI on my android acer tab without any functionality.
    I have use Qtquick and Qt Gui to develop the UI.
    In both cases, my image gets cut or doesn't appear when I rotate my tab.
    I have tried few custom programs and it works just fine when I rotate my tab screen but facing problems with images.
    I am very new to coding and Qt creator.
    Would be really helpful if anyone can help me out with this.

    Thanks a lot in advance,
    Mansi


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.