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(); 
     } 
     
  • 相关阅读:
    (C#)asp_net调试错误解决方法收集(1)
    asp.net调试技巧
    asp。Net 页面传值
    viewState详解
    Session,ViewState用法
    asp.net中通过form表单submit提交到后台的实例
    C#读写xml文件
    Asp.Net前台调用后台变量
    3. mybatis # 与 $ 的区别
    IDEA 中 使用 git(Git)
  • 原文地址:https://www.cnblogs.com/feisky/p/1655182.html
Copyright © 2011-2022 走看看