• Qt之主窗口设计——打印图像


    printimage.h

    #ifndef PRINTIMAGE_H
    #define PRINTIMAGE_H
    
    #include <QtGui>
    #include <QImage>
    
    
    class PrintImage : public QMainWindow
    {
    	Q_OBJECT
    
    public:
    	PrintImage(QWidget *parent = 0, Qt::WFlags flags = 0);
    	~PrintImage();
    
    	void createActions();
    	void createMenus();
    	void createToolBars();
    
    public slots:
    	void sl_print();
    
    private:
    	QLabel *labImage;
    	QImage image;
    
    	QAction *printAction;
    	QMenu *fileMenu;
    	QToolBar *fileToolBar;
    };
    
    #endif // PRINTIMAGE_H
    

    printimage.cpp

    #include "printimage.h"
    
    
    PrintImage::PrintImage(QWidget *parent, Qt::WFlags flags)
    	: QMainWindow(parent, flags)
    {
    	setWindowTitle(tr("PrintImage"));
    
    	labImage = new QLabel(this);
    	labImage->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
    	labImage->setScaledContents(true);
    	setCentralWidget(labImage);
    
    	createActions();
    	createMenus();
    	createToolBars();
    
    	if(image.load("tux.png"))
    	{
    		labImage->setPixmap(QPixmap::fromImage(image));
    		resize(image.width(),image.height());
    	}
    }
    
    PrintImage::~PrintImage()
    {
    
    }
    
    void PrintImage::createActions()
    {
    	printAction = new QAction(QIcon("images/print.png"),tr("Print"),this);
    	printAction->setShortcut(tr("Ctrl+P"));
    	printAction->setStatusTip(tr("Print Image"));
    	connect(printAction,SIGNAL(triggered()),this,SLOT(sl_print()));
    }
    
    void PrintImage::createMenus()
    {
    	fileMenu = menuBar()->addMenu(tr("File"));
    	fileMenu->addAction(printAction);
    }
    
    void PrintImage::createToolBars()
    {
    	fileToolBar = addToolBar("File");
    	fileToolBar->addAction(printAction);
    }
    
    void PrintImage::sl_print()
    {
    	QPrinter printer;
    	QPrintDialog printDlg(&printer,this);
    	if(printDlg.exec())
    	{
    		QPainter painter(&printer);		
    		QRect rect = painter.viewport();	
    		QSize size = image.size();
    		size.scale(rect.size(),Qt::KeepAspectRatio);
    		painter.setViewport(rect.x(),rect.y(),size.width(),size.height());
    		painter.setWindow(image.rect());
    		painter.drawImage(0,0,image);
    		
    	}
    }
    
  • 相关阅读:
    文件打开的几种访问模式
    数据分析师简介
    python数据处理----常用数据文件的处理
    markdown使用方法介绍
    gdb调试常用方法介绍
    [OPEN CV] 常用视频操作方法
    [转载]C++中四种强制类型转换方式
    python 定时服务模块
    pymysql安装和使用
    VS2019开发Qt程序中文乱码
  • 原文地址:https://www.cnblogs.com/hanzhaoxin/p/2786453.html
Copyright © 2020-2023  润新知