QLabel:缩放图像以放大和降低图片质量 [英] QLabel: Scaling Images for Zooming and Losing Picture Quality

查看:73
本文介绍了QLabel:缩放图像以放大和降低图片质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有这个功能,它可以很好地工作和缩放图像,并且可以根据用户界面的正确快捷方式缩小和扩展它.

Hey so I have this function which works and scales the image nicely and will shrink and expand it given the correct shortcuts from the UI.

但是,当我缩放回图像的原始缩放比例时,我的图片质量会下降.我试过制作'orig',它是一个全局'const QPixmap*'.'orig' 是在图像加载到 UI 时定义的.然后我设置了 'pixmap = orig'(见下面的代码),但这似乎不起作用,我不知道为什么.

However I am losing picture quality when I zoom back to the original scaled factor of the image. I have tried making 'orig' which is a global 'const QPixmap*'. 'orig' is defined when the image is loaded into the UI. I then set 'pixmap = orig' (seen below in the code) but this doesn't seem to work, and I'm not sure why.

这个想法是在不重写原始像素图的情况下制作它的副本,以保持像素质量,然后在调用函数时重新加载原始像素图.

The idea is to make a copy of it without rewriting the original's pixmap, to preserve the pixel quality, and then to reload the original pixmap whenever the function gets called.

void FragTreeViewer::scaleImage(double factor)
{
  Q_ASSERT(imageLabel->pixmap());
  const QPixmap* pixmap = orig;
  scaleFactor *= factor;
  int w = (imageLabel->width())*scaleFactor;
  int h = (imageLabel->height())*scaleFactor;
  imageLabel->setPixmap(pixmap->scaled(w, h, Qt::KeepAspectRatio));


  adjustScrollBar(scrollArea->horizontalScrollBar(), scaleFactor);
  adjustScrollBar(scrollArea->verticalScrollBar(), scaleFactor);

  //zoomInAct->setEnabled(scaleFactor < 3.0);
  //zoomOutAct->setEnabled(scaleFactor > 0.333);
}

推荐答案

您没有显示相关代码,您显示的内容很好.据我所知,您正在尝试调整标签中显示的像素图和标签本身的大小.你应该做一个或另一个,而不是两者都做.调整标签大小最简单,让滚动区域发挥其魔力.这让整个事情变得微不足道.

You're not showing the relevant code, what you do show is fine. From what I can glean you're trying to resize both the pixmap shown in the label, and the label itself. You should be doing one or the other, not both. It's simplest to resize the label, and let the scroll area work its magic. This makes the whole affair trivial.

下面的代码,没有修复它所需的修改,是你应该在你的问题中发布的内容.

The code below, sans modifications needed to fix it, is what you should have posted in your question.

// https://github.com/KubaO/stackoverflown/tree/master/questions/image-view-scale-31619246
#include <QtWidgets>
#include <QtNetwork>

class FragTreeViewer : public QWidget {
   QGridLayout m_layout{this};
   QScrollArea m_area;
   QLabel m_imageLabel, m_scaleLabel;
   QPushButton m_zoomOut{"Zoom Out"}, m_zoomIn{"Zoom In"};
   double m_scaleFactor = 1.0;
public:
   void setImage(const QImage & img) {
      m_scaleFactor = 1.0;
      m_imageLabel.setPixmap(QPixmap::fromImage(img));
      scaleImage(1.0);
   }
   FragTreeViewer() {
      m_layout.addWidget(&m_area, 0, 0, 1, 3);
      m_layout.addWidget(&m_zoomOut, 1, 0);
      m_layout.addWidget(&m_scaleLabel, 1, 1);
      m_layout.addWidget(&m_zoomIn, 1, 2);
      m_area.setWidget(&m_imageLabel);
      m_imageLabel.setScaledContents(true);
      connect(&m_zoomIn, &QPushButton::clicked, [this]{ scaleImage(1.1); });
      connect(&m_zoomOut, &QPushButton::clicked, [this]{ scaleImage(1.0/1.1); });
   }
   void scaleImage(double factor) {
      m_scaleFactor *= factor;
      m_scaleLabel.setText(QStringLiteral("%1%").arg(m_scaleFactor*100, 0, 'f', 1));
      QSize size = m_imageLabel.pixmap()->size() * m_scaleFactor;
      m_imageLabel.resize(size);
   }
};

int main(int argc, char *argv[])
{
   QApplication a(argc, argv);
   FragTreeViewer viewer;
   QNetworkAccessManager mgr;
   QScopedPointer<QNetworkReply> rsp(
            mgr.get(QNetworkRequest({"http://i.imgur.com/ikwUmUV.jpg"})));
   QObject::connect(rsp.data(), &QNetworkReply::finished, [&]{
      if (rsp->error() == QNetworkReply::NoError)
         viewer.setImage(QImage::fromData(rsp->readAll()));
   });
   viewer.show();
   return a.exec();
}

这篇关于QLabel:缩放图像以放大和降低图片质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆