基于QMainWindow主窗体程序
来源:互联网 发布:js escape替代方法 编辑:程序博客网 时间:2024/06/05 08:41
1.下面实现的是一个基本主窗口程序,包含一个菜单栏,一个工具栏,中央可编辑窗体以及状态栏
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>class QAction;class QMenu;class QToolBar;class QTextEdit;class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(); void createMenus();//创建菜单 void createActions();//创建动作 void createToolBars();//创建工具栏 public slots: void slotNewFile(); void slotOpenFile(); void slotSaveFile(); void slotCopy(); void slotCut(); void slotPaste(); void slotAbout(); private: QTextCodec *codec; QMenu *fileMenu; QMenu *editMenu; QMenu *aboutMenu; QToolBar *fileTool; QToolBar *editTool; QAction *fileOpenAction; QAction *fileNewAction; QAction *fileSaveAction; QAction *exitAction; QAction *copyAction; QAction *cutAction; QAction *pasteAction; QAction *aboutAction; QTextEdit * text;};#endif // MAINWINDOW_H
构造函数
MainWindow::MainWindow(){ setWindowTitle(tr("Doc")); text = new QTextEdit(this); setCentralWidget(text);//把QTextEdit作为主窗口的中央窗体 createActions();/*QAction类为用户提供了一个统一的命令接口,无论是从菜单触发还是从工具栏触发,或者是快捷键触发都调用同样的操作接口,达到同样的目的。*/ createMenus(); createToolBars();}
void MainWindow::createActions(){ // file open action fileOpenAction = new QAction(QIcon(":/images/open.png"),tr("Open"),this);// 打开文件/*指定动作所用的图标时,使用的是QIcon(":/images/open.png"),需要在程序所在的文件夹下面新增一个mainwindow.qrc文件用于说明程序中所用到的图标<!DOCTYPE RCC><RCC version="1.0"><qresource><file>images/copy.png</file><file>images/cut.png</file><file>images/new.png</file><file>images/open.png</file><file>images/paste.png</file><file>images/save.png</file></qresource></RCC>并且在.pro文件中加入一行:RESOURCES=mainwindow.qrc*/ fileOpenAction->setShortcut(tr("Ctrl+O"));//设置这个动作的快捷键 fileOpenAction->setStatusTip(tr("open a file"));//设定了状态条显示 connect(fileOpenAction,SIGNAL(triggered()),this,SLOT(slotOpenFile())); // file new action fileNewAction = new QAction(QIcon(":/images/new.png"),tr("New"),this);// 新建文件 fileNewAction->setShortcut(tr("Ctrl+N")); fileNewAction->setStatusTip(tr("new file")); connect(fileNewAction,SIGNAL(triggered()),this,SLOT(slotNewFile())); // save file action fileSaveAction = new QAction(QPixmap(":/images/save.png"),tr("Save"),this);// 保存文件 fileSaveAction->setShortcut(tr("Ctrl+S")); fileSaveAction->setStatusTip(tr("save file")); connect(fileSaveAction,SIGNAL(activated()),this,SLOT(slotSaveFile())); // exit action exitAction = new QAction(tr("Exit"), this);// 退出 exitAction->setShortcut(tr("Ctrl+Q")); exitAction->setStatusTip(tr("exit")); connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); cutAction = new QAction(QIcon(":/images/cut.png"), tr("Cut"), this);// 剪切 cutAction->setShortcut(tr("Ctrl+X")); cutAction->setStatusTip(tr("cut to clipboard")); connect(cutAction, SIGNAL(triggered()), text, SLOT(cut())); copyAction = new QAction(QIcon(":/images/copy.png"), tr("Copy"), this);// 复制 copyAction->setShortcut(tr("Ctrl+C")); copyAction->setStatusTip(tr("copy to clipboard")); connect(copyAction, SIGNAL(triggered()), text, SLOT(copy())); pasteAction = new QAction(QIcon(":/images/paste.png"), tr("Paste"), this);// 粘贴 pasteAction->setShortcut(tr("Ctrl+V")); pasteAction->setStatusTip(tr("paste clipboard to selection")); connect(pasteAction, SIGNAL(triggered()), text, SLOT(paste())); aboutAction = new QAction(tr("About"), this);// 关于,可不指定图标,一般只在菜单栏中使用 connect(aboutAction, SIGNAL(triggered()), this, SLOT(slotAbout()));}
在实现了各个动作之后,需要把它通过菜单及工具栏快捷键的方式体现出来。
void MainWindow::createMenus(){//在菜单栏中添加新菜单 fileMenu = menuBar()->addMenu(tr("File")); editMenu = menuBar()->addMenu(tr("Edit")); aboutMenu = menuBar()->addMenu(tr("Help")); //添加菜单条目到菜单 fileMenu->addAction(fileNewAction); fileMenu->addAction(fileOpenAction); fileMenu->addAction(fileSaveAction); fileMenu->addAction(exitAction); editMenu->addAction(copyAction); editMenu->addAction(cutAction); editMenu->addAction(pasteAction); aboutMenu->addAction(aboutAction); }
创建工具栏
void MainWindow::createToolBars(){//编辑两个工具栏,并排显示 fileTool = addToolBar("File"); fileTool->setMovable(false);//设置工具条的可移动性,此处为不可移动 editTool = addToolBar("Edit"); fileTool->addAction(fileNewAction); fileTool->addAction(fileOpenAction); fileTool->addAction(fileSaveAction); editTool->addAction(copyAction); editTool->addAction(cutAction); editTool->addAction(pasteAction); }
标准打印对话框QPrintDialog的使用
2.打印文本
#ifndef PRINTTEXT_H#define PRINTTEXT_H#include <QtGui>class PrintText : public QMainWindow{ Q_OBJECTpublic: PrintText(); ~PrintText(); void createMenus(); void createActions(); void createToolBars(); private: QTextEdit *contentTextEdit; QMenu *printMenu; QAction *printAction; QToolBar *printTool; protected slots: void slotPrint();};#endif
#include "printtext.h"PrintText::PrintText() : QMainWindow(){ QFont f("ZYSong18030",12); setFont(f); setWindowTitle(tr("Printer")); contentTextEdit = new QTextEdit(this);setCentralWidget(contentTextEdit);//设置中央窗口部件 createActions();//创建动作 createMenus();//创建菜单 createToolBars();//创建工具栏 QFile file("QPrinter.txt");//创建一个file对象 if (file.open(QIODevice::ReadOnly|QIODevice::Text))//按行读取数据并显示在contentTextEdit中 { QTextStream textStream(&file); while(!textStream.atEnd()) { contentTextEdit->append(textStream.readLine()); } file.close(); }}PrintText::~PrintText(){}void PrintText::createActions(){ // print action printAction = new QAction(QIcon(":/images/print.png"),tr("Print"),this); printAction->setShortcut(tr("Ctrl+P")); printAction->setStatusTip(tr("Print")); connect(printAction,SIGNAL(triggered()),this,SLOT(slotPrint()));}void PrintText::createMenus(){ printMenu = menuBar()->addMenu(tr("Print")); printMenu->addAction(printAction);}void PrintText::createToolBars(){ printTool = addToolBar("Print"); printTool->addAction(printAction);}void PrintText::slotPrint (){QPrinter printer; //创建一个QPrinter对象 QPrintDialog printDialog(&printer, this);/*参数为printer,QPrinterDialog是Qt提供的标准对话框,为打印机的使用提供了一种方便,规范的方法。*/ if (printDialog.exec()) //判断打印对话框显示后用户是否单击打印按钮 {QTextDocument *doc = contentTextEdit->document();//获得QTextEdit对象的文档doc->print(&printer);//打印}}
3.打印图像
打印图像实际上是在一个QPaintDevice中画图,与平常在QWidget,Qpixmap,Qimage中画图一样,都是在创建一个QPainter对象进行画图。
#ifndef PRINTIMAGE_H#define PRINTIMAGE_H#include <QtGui>class PrintImage : public QMainWindow{ Q_OBJECTpublic: PrintImage(); ~PrintImage(); void createMenus(); void createActions(); void createToolBars(); private: QImage image; QLabel *imageLabel; QMenu *printMenu; QAction *printAction; QToolBar *printTool; protected slots: void slotPrint();};#endif
#include "printimage.h"PrintImage::PrintImage() : QMainWindow(){ QFont f("ZYSong18030",12); setFont(f); setWindowTitle(tr("PrintImage")); imageLabel = new QLabel(this); imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); imageLabel->setScaledContents(true);setCentralWidget(imageLabel); //设置中央窗口部件 createActions(); createMenus(); createToolBars(); if(image.load("tux.png"));//载入图片 { imageLabel->setPixmap (QPixmap::fromImage(image));//在imageLabel中显示图片 resize(image.width(), image.height()); }}PrintImage::~PrintImage(){}void PrintImage::createActions(){ // print action printAction = new QAction(QIcon(":/images/print.png"),tr("Print"),this); printAction->setShortcut(tr("Ctrl+P")); printAction->setStatusTip(tr("Print")); connect(printAction,SIGNAL(triggered()),this,SLOT(slotPrint()));}void PrintImage::createMenus(){ printMenu = menuBar()->addMenu(tr("Print")); printMenu->addAction(printAction);}void PrintImage::createToolBars(){ printTool = addToolBar("Print"); printTool->addAction(printAction);}void PrintImage::slotPrint (){QPrinter printer; QPrintDialog printDialog(&printer, this); if (printDialog.exec()) //若用户单击打印按钮 { QPainter painter(&printer);//指定绘图设备为QPrinter对象 QRect rect = painter.viewport();//获得QPainter对象的视口矩形 qWarning("rect.x()=%d,rect.y()=%d",rect.x(),rect.y()); qWarning("rect.width()=%d,rect.height()=%d",rect.width(),rect.height()); QSize size = image.size();//获得图像大小 qWarning("size.width()=%d,size.height()=%d",size.width(),size.height()); size.scale(rect.size(), Qt::KeepAspectRatio);//按照图形的比例大小重新设置视口矩形 qWarning("size.width()=%d,size.height()=%d",size.width(),size.height()); painter.setViewport(rect.x(), rect.y(),size.width(), size.height()); painter.setWindow(image.rect());//设置QPainter窗口大小为图像大小 painter.drawImage(0, 0, image);//打印 }}
4.图片的旋转与缩放
使用QMatrix实现坐标变换
QMatrix类提供了世界坐标系统的2D转换功能,可以使窗体转换变形,可实现坐标系统的移动,缩放,变形以及旋转的功能
下面是其构造函数
ImgProcessor::ImgProcessor() : QMainWindow(){ QFont f("ZYSong18030",12); setFont(f); setWindowTitle(tr("Image Processor")); imageLabel = new QLabel(this); imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); imageLabel->setScaledContents(true);//是否会根据其大小自动调节内容大小,默认为false. setCentralWidget(imageLabel);//设置中央窗口部件 createActions(); createMenus(); createToolBars();}
载入图形文件函数
void ImgProcessor::slotOpenFile (){ QString s = QFileDialog::getOpenFileName( this, "open image file", ".", "Image files (*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm);;All files (*.*)"); if(s != "") { if(img.load(s)); { imageLabel->setPixmap (QPixmap::fromImage(img)); resize(img.width(), img.height()); } }}
图像的放大与缩小函数
void ImgProcessor::slotZoomIn ()//实现图形的放大功能{if(img.isNull())//行为的有效性判断{return;}QMatrix martix;//QMatrix类的实例martix.scale (2,2); // 按照两倍的比例对水平和垂直方向进行放大img=img.transformed(martix);//并将当前显示的图形按照该坐标矩阵进行转换imageLabel->setPixmap (QPixmap::fromImage(img));//重新设置显示图形resize(img.width(),img.height()); //调整大小 }void ImgProcessor::slotZoomOut (){if(img.isNull()){return;}QMatrix martix;martix.scale(0.5,0.5); img=img.transformed(martix);imageLabel->setPixmap (QPixmap::fromImage(img));resize(img.width(),img.height());}
实现旋转函数
void ImgProcessor::slotRotate90 (){if(img.isNull()){return;}QMatrix martix;martix.rotate(90); //实现坐标的逆时针旋转img=img.transformed(martix);imageLabel->setPixmap (QPixmap::fromImage(img));resize(img.width(),img.height());}
垂直与水平镜像函数
void ImgProcessor::slotMirrorVertical ()//实现图形的垂直镜像{if(img.isNull()){return;}img=img.mirrored(false,true);//第一个参数表示水平,第二个参数表示垂直imageLabel->setPixmap (QPixmap::fromImage(img));resize(img.width(),img.height()); }void ImgProcessor::slotMirrorHorizontal (){if(img.isNull()){return;} img=img.mirrored(true,false);imageLabel->setPixmap (QPixmap::fromImage(img));resize(img.width(),img.height()); }
0 0
- 基于QMainWindow主窗体程序
- 基于QMainWindow的主窗口程序例子
- QT 基于QMainWindow的堆栈窗体设计
- Qt入门-基于QMainWindow的窗体
- Qt实现基本QMainWindow主窗口程序
- Qt实现基本QMainWindow主窗口程序
- Qt实现基本QMainWindow主窗口程序
- Qt实现基本QMainWindow主窗口程序
- QT主窗口QMainWindow
- QMAINWINDOW
- QMainWindow
- VC中基于对话框的程序在一开始创建时候就隐藏主窗体的方法
- PyQt4 精彩实例分析* 实例24 基本QMainWindow主窗口程序
- Qt的主窗口QMainWindow
- Qt中的主窗口QMainWindow
- 程序启动时不显示主窗体
- qml程序主窗体自制关闭按钮
- 基于窗体的身份验证
- git tip: How to undo an uncommitted change/deletion for a specific file in your working directory
- struts2的执行流程分析
- 高性能C++网络库libtnet实践:comet单机百万连接挂载测试
- c++学习笔记(12.继承与多态)
- linux内核编程之list_head
- 基于QMainWindow主窗体程序
- UML图 符号的定义
- 图像检索中一些特征索引技术
- 阿里巴巴集团2014秋季校园招聘笔试题
- kaldi上的深度神经网络(Deep Neural Networks in Kaldi)
- Quick share by "Python -m SimpleHTTPServer 8000"
- ios提交二进制
- SIGCOMM13论文简要选读
- 2014校园招聘_百度2014校园招聘