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. Qt5 - C++ - QImage - NOT scaling right

Qt5 - C++ - QImage - NOT scaling right

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 894 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.
  • V Offline
    V Offline
    Vildnex
    wrote on last edited by Vildnex
    #1

    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:

    auto cellX = this->parentWidget()->width() / 100;
    auto cellY = this->parentWidget()->height() / 100;
    
    QImage icon(dir.absoluteFilePath(m_viewModel.icon));
    QImage scaled = icon.scaledToHeight(cellY * 40, Qt::SmoothTransformation);
    painter.drawImage(cellX * 20, cellY * 20, scaled);
    

    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)

    enter image description here
    enter image description here
    enter image description here

    QUESTION:

    As you could see already my scaled image is strictly depended 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?

    UPDATE:

    I have my Widget Class which looks something like this:

    class AC_SpeedLevelController : public QWidget {
    Q_OBJECT
    
    protected:
        void paintEvent(QPaintEvent *event) override;
    
    private:
        AC_ButtonViewModel m_viewModel{};
    public:
        explicit AC_SpeedLevelController(QWidget *parent);
    
        void setupStyle(const AC_ButtonViewModel &model) override;
    
    };
    

    My paintEvent is going to look like:

    void AC_SpeedLevelController::paintEvent(QPaintEvent *event) {
        QWidget::paintEvent(event);
        QPainter painter(this);
    
        QDir dir(qApp->applicationDirPath());
        dir.cd("icons");
        auto cellX = this->parentWidget()->width() / 100;
        auto cellY = this->parentWidget()->height() / 100;
    
        QImage icon(dir.absoluteFilePath(m_viewModel.icon));
        QImage scaled = icon.scaledToHeight(cellY * 20, Qt::SmoothTransformation);
        painter.drawImage(0, 0, scaled);
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Vildnex said in Qt5 - C++ - QImage - NOT scaling right:

      auto cellX = this->parentWidget()->width() / 100;

      C basics: int = int/int

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      V 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @Vildnex said in Qt5 - C++ - QImage - NOT scaling right:

        auto cellX = this->parentWidget()->width() / 100;

        C basics: int = int/int

        V Offline
        V Offline
        Vildnex
        wrote on last edited by Vildnex
        #3

        @Christian-Ehrlicher sure... and what has to do this to my issue?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Example of int division: 240 / 100 = 2

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

          V 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            Example of int division: 240 / 100 = 2

            V Offline
            V Offline
            Vildnex
            wrote on last edited by
            #5

            @SGaist sure... but why is that important to my question? If you look at the images that I give as an example the second and third image are exactly the same in terms of size, the only difference it's in terms of the initial position. But although the difference is just in terms of position as you can see in one example my image will be cut in half and on the other will be just fine.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #6

              What is this AC_SpeedLevelController?
              Seems that it has not got enough size to draw the full image from the start point in that first two situations.
              Try to print the value of cellX, cellY, rect(), event->rect()

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Since the paint() always starts at 0,0 I would guess the QWidget is moved around somewhere else - the scaling is independent from the upper left position of the start of the drawing.
                Putting some debug output about the image size etc. will surely help here.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                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