zoukankan      html  css  js  c++  java
  • Qt打开并显示图像

    imagewidget.h
    #ifndef IMAGEWIDGET_H 
     #define IMAGEWIDGET_H 
      
     #include <QWidget> 
     #include <QImage> 
     #include <QPainter> 
     #include <QString> 
     #include <QFileDialog> 
     #include <QMessageBox> 
     #include <QPushButton> 
      
     class ImageWidget : public QWidget 
     { 
         Q_OBJECT 
     public: 
         ImageWidget(); 
      
      
         /* The QImage class provides a hardware-independent 
      image representation that allows direct access to the 
     pixel data, and can be used as a paint device. 
      
     Qt provides four classes for handling image data: 
     QImage, QPixmap, QBitmap and QPicture. QImage is 
     designed and optimized for I/O, and for direct pixel access 
     and manipulation, while QPixmap is designed and optimized 
     for showing images on screen. QBitmap is only a convenience 
      class that inherits QPixmap, ensuring a depth of 1. Finally, 
     the QPicture class is a paint device that records and replays 
     QPainter commands.      */ 
         QImage img; 
      
     protected: 
         void paintEvent(QPaintEvent*); 
     private: 
          QPushButton *openButton; 
     public slots: 
         void openImage(); 
     }; 
      
     #endif // IMAGEWIDGET_H 
     
     
    imagewidget.cpp
    #include "imagewidget.h" 
      
     ImageWidget::ImageWidget() 
     { 
         openButton=new QPushButton(tr("Open Image"),this); 
         //openButton->setGeometry(50,50,50,100); 
         connect(openButton,SIGNAL(clicked()),this,SLOT(openImage())); 
         openButton->show(); 
     } 
      
     void ImageWidget::paintEvent(QPaintEvent *event) { 
         QPainter painter(this); 
         if(!img.isNull()) 
             painter.drawImage(0,0,img); 
         //img.save("test.bmp"); 
     } 
      
     void ImageWidget::openImage() 
     { 
        QString filename=QFileDialog::getOpenFileName(this, 
                                                       tr("Open Image"),"", 
                                                       tr("BMP(*.bmp);;JPG(*.jpg);;ALL files(*.*)")); 
         if(filename.isEmpty()) 
         { 
             QMessageBox::information(this,tr("Open Image"), 
                                          tr("Please select an image to open")); 
             filename=QFileDialog::getOpenFileName(this, 
                                                       tr("Open Image"),"", 
                                                       tr("BMP(*.bmp);;JPG(*.jpg);;ALL files(*.*)")); 
         } 
      
          if(!(img.load(filename,0))) 
          { 
             QMessageBox::information(this,tr("Unable to open the Image"), 
                                     tr("Please select a valid image.")); 
            return; 
          } 
         QWidget::update(); 
     } 
     
  • 相关阅读:
    架构之路(六):把框架拉出来
    读取mdb文件
    基类、子类之间的类型转换
    WPF Trigger
    WPF 打开txt文件
    C# 匿名方法
    自定义显隐式类型转换
    枚举获得Description扩展方法
    IFormattable和IFormatProvider
    WPF DataGrid下滑动态加载数据
  • 原文地址:https://www.cnblogs.com/feisky/p/1655182.html
Copyright © 2011-2022 走看看