【问题标题】:How to display image in ratio as PreserveAspectFit in Qt Widgets如何在 Qt 小部件中以 PreserveAspectFit 的比例显示图像
【发布时间】:2021-11-09 23:07:54
【问题描述】:

在 qml 中,我可以简单地做到这一点

    Image {
        anchors.fill: parent
        source: "/path/to/coo/image"
        fillMode: Image.PreserveAspectFit
    }

我不想使用 QML,因为它与 JavaScript 相关。我如何使用小部件来做到这一点?

我尝试使用 QLabel,但它没有设置纵横比或填充模式的选项。我想我可以手动缩放像素图,然后将其设置为 QLabel,但这不会像 QML 那样具有反应性(调整窗口大小时调整图像大小)。 Qt 中没有任何特定于图像的小部件来执行此操作吗?

【问题讨论】:

    标签: qt qwidget qlabel qpixmap


    【解决方案1】:

    你可以这样做:

    QPixmap pixmap("background.png");
    pixmap.scaled(pixmap.size(), Qt::KeepAspectRatio);
    

    您可以根据需要操作大小值。如果你愿意,你可以捕捉像素图所在的 QLabel 的大小。

    【讨论】:

      【解决方案2】:

      一种可能的解决方案是使用 QGraphicsView、QGraphicsScene 和 QGraphicsPixmapItem:

      #include <QApplication>
      #include <QGraphicsPixmapItem>
      #include <QGraphicsView>
      #include <QPixmap>
      
      class Viewer: public QGraphicsView{
      public:
          Viewer(QWidget *parent = nullptr): QGraphicsView(parent){
              setScene(new QGraphicsScene(this));
              m_pixmapItem = scene()->addPixmap(QPixmap());
              setAlignment(Qt::AlignCenter);
          }
          QPixmap pixmap() const{
               return m_pixmapItem->pixmap();
          }
          void setPixmap(const QPixmap &newPixmap){
              m_pixmapItem->setPixmap(newPixmap);
              fitInView(m_pixmapItem, Qt::KeepAspectRatio);
          }
      protected:
          void resizeEvent(QResizeEvent *event){
              QGraphicsView::resizeEvent(event);
              fitInView(m_pixmapItem, Qt::KeepAspectRatio);
          }
      private:
          QGraphicsPixmapItem *m_pixmapItem;
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QPixmap pixmap(100, 200);
          pixmap.fill(Qt::green);
      
          Viewer w;
          w.resize(640, 480);
          w.setPixmap(pixmap);
          w.show();
      
          return a.exec();
      }
      

      Python 版本:

      from PyQt5.QtCore import Qt
      from PyQt5.QtGui import QPixmap
      from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView
      
      
      class Viewer(QGraphicsView):
          def __init__(self, parent=None):
              super().__init__(parent)
              self.setScene(QGraphicsScene(self))
              self.m_pixmapItem = self.scene().addPixmap(QPixmap())
              self.setAlignment(Qt.AlignCenter)
      
          @property
          def pixmap(self):
              return self.m_pixmapItem.pixmap()
      
          @pixmap.setter
          def pixmap(self, newPixmap):
              self.m_pixmapItem.setPixmap(newPixmap)
              self.fitInView(self.m_pixmapItem, Qt.KeepAspectRatio)
      
          def resizeEvent(self, event):
              super().resizeEvent(event)
              self.fitInView(self.m_pixmapItem, Qt.KeepAspectRatio)
      
      
      def main():
          import sys
      
          app = QApplication(sys.argv)
      
          pixmap = QPixmap(100, 200)
          pixmap.fill(Qt.green)
      
          w = Viewer()
          w.resize(640, 480)
          w.pixmap = pixmap
          w.show()
      
          sys.exit(app.exec_())
      
      
      if __name__ == "__main__":
          main()
      ``
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-23
        • 1970-01-01
        • 2018-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-30
        • 1970-01-01
        相关资源
        最近更新 更多