Results 1 to 2 of 2

Thread: Qt5 - C++ - QImage - NOT scaling right

  1. #1
    Join Date
    Mar 2020
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Unhappy Qt5 - C++ - QImage - NOT scaling right

    I'm getting confused by the way of drawImage and scaledToHeight (or any kind of scaling) is working. Could any of you help me to understand what's going on here?

    So I have the fallowing code:

    Qt Code:
    1. auto cellX = this->parentWidget()->width() / 100;
    2. auto cellY = this->parentWidget()->height() / 100;
    3.  
    4. QImage icon(dir.absoluteFilePath(m_viewModel.icon));
    5. QImage scaled = icon.scaledToHeight(cellY * 40, Qt::SmoothTransformation);
    6. painter.drawImage(cellX * 20, cellY * 20, scaled);
    To copy to clipboard, switch view to plain text mode 
    Now if I understand it correctly this should work as the following:

    QImage QImage::scaledToHeight(int height, Qt::TransformationMode mode = Qt::FastTransformation) const

    Returns a scaled copy of the image. The returned image is scaled to
    the given height using the specified transformation mode.

    This function automatically calculates the width of the image so that
    the ratio of the image is preserved.

    If the given height is 0 or negative, a null image is returned.
    and also

    void QPainter::drawImage(int x, int y, const QImage &image, int sx = 0, int sy = 0, int sw = -1, int sh = -1, Qt::ImageConversionFlags flags = Qt::AutoColor)

    This is an overloaded function.

    Draws an image at (x, y) by copying a part of image into the paint
    device.

    (x, y) specifies the top-left point in the paint device that is to be
    drawn onto. (sx, sy) specifies the top-left point in image that is to
    be drawn. The default is (0, 0).

    (sw, sh) specifies the size of the image that is to be drawn. The
    default, (0, 0) (and negative) means all the way to the bottom-right
    of the image.
    So in other words, scaledToHeight is going to return a new image scaled according to that specific height and drawImage is going to draw that specific image started from point X, Y which I'm going to mention down to the end of the image (because it's -1 and -1 default)

    VNqLF.png
    rwDZK.png
    Dbo3K.png

    QUESTION:

    As you could see already my scaled image is strictly dependent on the position of drawImage, why is that? How can I scale and draw my image properly? Or in other words WHY if I will position my image at 0 0 or not will affect how my image looks like?

    CODE:

    I have my Widget Class which looks something like this:
    Qt Code:
    1. class AC_SpeedLevelController : public QWidget {
    2. Q_OBJECT
    3.  
    4. protected:
    5. void paintEvent(QPaintEvent *event) override;
    6.  
    7. private:
    8. AC_ButtonViewModel m_viewModel{};
    9. public:
    10. explicit AC_SpeedLevelController(QWidget *parent);
    11.  
    12. void setupStyle(const AC_ButtonViewModel &model) override;
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 

    My paintEvent is going to look like:
    Qt Code:
    1. void AC_SpeedLevelController::paintEvent(QPaintEvent *event) {
    2. QWidget::paintEvent(event);
    3. QPainter painter(this);
    4.  
    5. QDir dir(qApp->applicationDirPath());
    6. dir.cd("icons");
    7. auto cellX = this->parentWidget()->width() / 100;
    8. auto cellY = this->parentWidget()->height() / 100;
    9.  
    10. QImage icon(dir.absoluteFilePath(m_viewModel.icon));
    11. QImage scaled = icon.scaledToHeight(cellY * 20, Qt::SmoothTransformation);
    12. painter.drawImage(0, 0, scaled);
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,570
    Thanks
    256
    Thanked 728 Times in 718 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qt5 - C++ - QImage - NOT scaling right

    Qt Code:
    1. QDir dir(qApp->applicationDirPath());
    2. dir.cd("icons");
    3. auto cellX = this->parentWidget()->width() / 100;
    4. auto cellY = this->parentWidget()->height() / 100;
    5.  
    6. QImage icon(dir.absoluteFilePath(m_viewModel.icon));
    7. QImage scaled = icon.scaledToHeight(cellY * 20, Qt::SmoothTransformation);
    To copy to clipboard, switch view to plain text mode 

    You don't want to do this in a paint event. Do it in a resize event and store the scaled image as a member variable. It's a complete waste of time to re-scale the image every time you draw it when the window size hasn't changed. It just slows your GUI down. And why are you repeatedly reading the icon from the file. Read it once and save it in a member variable.

    As for your code, I don't know what you are trying to do. You are scaling your image so the height is 40% of the parent window's height, then you are drawing it at a position 20% of both width and height. What are you expecting to see?

    Why are you using the parent widget and not the current widget (this) itself? Is the parent widget what you think it is?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 5
    Last Post: 16th May 2011, 21:15
  2. Replies: 51
    Last Post: 26th November 2010, 13:24
  3. Replies: 4
    Last Post: 8th May 2009, 12:04
  4. Scaling Painter without scaling the coordinate sytem
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 7th January 2008, 21:30
  5. Replies: 3
    Last Post: 15th March 2006, 11:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.