【Qt5开发及实例】12、实现一个简单的文本编辑器1

showwidget.h

/**
* 书本:【Qt5开发及实例】
* 功能:实现一个简单的文本编辑器
* 文件:showwidget.h
* 时间:2015年1月18日10:03:21
* 作者:cutter_point
*/
#ifndef SHOWWIDGET_H
#define SHOWWIDGET_H

#include <QWidget>
#include <QLabel>
#include <QTextEdit>
#include <QImage>

class ShowWidget : public QWidget
{
  Q_OBJECT
public:
  explicit ShowWidget(QWidget *parent = 0);
  QImage img;
  QLabel *imageLabel;
  QTextEdit *text;

signals:

public slots:
};

#endif // SHOWWIDGET_H

showwidget.cpp

/**
* 书本:【Qt5开发及实例】
* 功能:实现一个简单的文本编辑器
* 文件:showwidget.h
* 时间:2015年1月18日10:03:21
* 作者:cutter_point
*/
#include "showwidget.h"

#include <QHBoxLayout>

ShowWidget::ShowWidget(QWidget *parent) :
  QWidget(parent)
{
  imageLabel = new QLabel;
  imageLabel->setScaledContents(true);

  text = new QTextEdit;

  QHBoxLayout *mainLayout = new QHBoxLayout(this);
  mainLayout->addWidget(imageLabel);
  mainLayout->addWidget(text);
}

imageprocessor.h

/**
* 书本:【Qt5开发及实例】
* 功能:实现一个简单的文本编辑器
* 文件:showwidget.h
* 时间:2015年1月18日10:41:52
* 作者:cutter_point
*/
#ifndef IMAGEPROCESSOR_H
#define IMAGEPROCESSOR_H

#include <QMainWindow>
#include <QImage>
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QComboBox>
#include <QSpinBox>
#include <QToolBar>
#include <QFontComboBox>
#include <QToolButton>
#include <QTextCharFormat>

#include "showwidget.h"

class ImageProcessor : public QMainWindow
{
  Q_OBJECT

public:
  ImageProcessor(QWidget *parent = 0);
  ~ImageProcessor();

  //创建动作
  void createActions();
  //创建菜单
  void createMenus();
  //创建工具栏
  void createToolBars();

  void loadFile(QString filename);    //加载文件
  void mergeFormat(QTextCharFormat);    //文本格式

private:
  //各项菜单栏
  QMenu *fileMenu;
  QMenu *zoomMenu;
  QMenu *rotateMenu;
  QMenu *mirrorMenu;

  QImage img;   //图片
  QString fileName;     //文件名
  ShowWidget *showWidget;     //显示流

  //文件菜单项
  QAction *openFileAction;
  QAction *NewFileAction;
  QAction *PrintTextAction;
  QAction *PrintImageAction;
  QAction *exitAction;

  //编辑菜单项
  QAction *copyAction;
  QAction *cutAction;
  QAction *pasteAction;
  QAction *aboutAction;
  QAction *zoomInAction;
  QAction *zoomOutAction;

  //旋转菜单项
  QAction *rotate90Action;
  QAction *rotate180Action;
  QAction *rotate270Action;

  //镜像菜单项
  QAction *mirrorVerticalAction;
  QAction *mirrorHorizontalAction;


  QAction *undoAction;
  QAction *redoAction;

  //工具栏
  QToolBar *fileTool;
  QToolBar *zoomTool;
  QToolBar *rotateTool;
  QToolBar *mirrorTool;


  QToolBar *doToolBar;

protected slots:    //添加函数槽
  void ShowNewFile();   //新建一个新的文件
  void ShowOpenFile();    //打开文件
  void ShowPrintText();   //打印功能的实现
  void ShowPrintImage();    //打印图片

};

#endif // IMAGEPROCESSOR_H

imageprocessor.cpp

/**
* 书本:【Qt5开发及实例】
* 功能:实现一个简单的文本编辑器
* 文件:showwidget.h
* 时间:2015年1月18日10:41:52
* 作者:cutter_point
*/
#include "imageprocessor.h"

#include <QApplication>
#include <QDebug>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
//#include <QPrintDialog>
#include <QtPrintSupport/QPrintDialog>
#include <QtPrintSupport/QPrinter>
#include <QPainter>

ImageProcessor::ImageProcessor(QWidget *parent)
  : QMainWindow(parent)
{
  //首先设定标题
  setWindowTitle(tr("my word"));

  //创建放图像的label和文本输入的地方,把这个设置为中心部件
  showWidget = new ShowWidget(this);    //创建一个对象
  setCentralWidget(showWidget);   //把这个设置为中心部件

//  qDebug()<<"11111111111111";

  createActions();      //创建动作
//  qDebug()<<"222222222222222222";
  createMenus();    //创建菜单  
//  qDebug()<<"3333333333333333";
  createToolBars();   //工具

//  qDebug()<<"......";

  //如果图像加载成功
  if(img.load(":/image.png"))
    {
      //在imageLabel中放图像
      showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
    }

}

//工具栏的实现
void ImageProcessor::createToolBars()
{
  //文件工具条
  fileTool = addToolBar("File");
  fileTool->addAction(openFileAction);
  fileTool->addAction(NewFileAction);
  fileTool->addAction(PrintTextAction);
  fileTool->addAction(PrintImageAction);

  //编辑工具条
  zoomTool = addToolBar("Edit");
  zoomTool->addAction(copyAction);
  zoomTool->addAction(cutAction);
  zoomTool->addAction(pasteAction);
//  zoomTool->addAction(aboutAction);
  zoomTool->addSeparator(); //分隔符
  zoomTool->addAction(zoomInAction);
  zoomTool->addAction(zoomOutAction);

  //旋转工具
  rotateTool = addToolBar("rotate");
  rotateTool->addAction(rotate90Action);
  rotateTool->addAction(rotate180Action);
  rotateTool->addAction(rotate270Action);

  //撤销和重做
  doToolBar = addToolBar("doEdit");
  doToolBar->addAction(undoAction);
  doToolBar->addAction(redoAction);

  fileTool->setAllowedAreas(Qt::TopToolBarArea|Qt::LeftToolBarArea);
}

//菜单的实现
void ImageProcessor::createMenus()
{
  //文件菜单
  fileMenu = menuBar()->addMenu(tr("file"));
  fileMenu->addAction(openFileAction);
  fileMenu->addAction(NewFileAction);
  fileMenu->addAction(PrintTextAction);
  fileMenu->addAction(PrintImageAction);
  fileMenu->addSeparator(); //分隔符
  fileMenu->addAction(exitAction);

  //缩放菜单
  zoomMenu = menuBar()->addMenu(tr("zoom"));  //缩放
  zoomMenu->addAction(copyAction);
  zoomMenu->addAction(cutAction);
  zoomMenu->addAction(pasteAction);
  zoomMenu->addAction(aboutAction);
  zoomMenu->addSeparator(); //分隔符
  zoomMenu->addAction(zoomInAction);
  zoomMenu->addAction(zoomOutAction);

  //旋转菜单
  rotateMenu = menuBar()->addMenu(tr("rotate"));
  rotateMenu->addAction(rotate90Action);
  rotateMenu->addAction(rotate180Action);
  rotateMenu->addAction(rotate270Action);

  //镜像菜单
  mirrorMenu = menuBar()->addMenu(tr("mirror"));
  mirrorMenu->addAction(mirrorVerticalAction);
  mirrorMenu->addAction(mirrorHorizontalAction);

}

//动作的实现
void ImageProcessor::createActions()
{
  //打开动作
//  qDebug()<<"11111111111111";
  openFileAction = new QAction(QIcon(":/open.png"), tr("open"), this);    //设置图片,名字
  openFileAction->setShortcut(tr("Ctrl+O"));    //设置快捷键
  openFileAction->setStatusTip(tr("open a file"));    //设置提示
//  qDebug()<<"222222222222222222";
//  connect(NewFileAction, SIGNAL(triggered()), this, SLOT(ShowNewFile()));   //连接上相应的槽
  connect(openFileAction, SIGNAL(triggered()), this, SLOT(ShowOpenFile()));

  //新建动作
  NewFileAction = new QAction(QIcon(":/new.png"), tr("new"), this);    //设置图片,名字
  NewFileAction->setShortcut(tr("Ctrl+N"));    //设置快捷键
  NewFileAction->setStatusTip(tr("new a file"));    //设置提示
  connect(NewFileAction, SIGNAL(triggered()), this, SLOT(ShowNewFile()));   //连接上相应的槽

  //退出动作
  exitAction = new QAction(tr("out"), this);    //设置图片,名字
  exitAction->setShortcut(tr("Ctrl+Q"));    //设置快捷键
  exitAction->setStatusTip(tr("close"));    //设置提示
  connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));

  //复制
  copyAction = new QAction(QIcon(":/copy.png"), tr("out"), this);    //设置图片,名字
  copyAction->setShortcut(tr("Ctrl+C"));    //设置快捷键
  copyAction->setStatusTip(tr("copy file"));    //设置提示
  connect(copyAction, SIGNAL(triggered()), showWidget->text, SLOT(copy()));

  //剪切
  cutAction = new QAction(QIcon(":/cut.png"), tr("cut"), this);    //设置图片,名字
  cutAction->setShortcut(tr("Ctrl+X"));    //设置快捷键
  cutAction->setStatusTip(tr("cut file"));    //设置提示
  connect(copyAction, SIGNAL(triggered()), showWidget->text, SLOT(cut()));

  //粘贴
  pasteAction = new QAction(QIcon(":/paste.png"), tr("paste"), this);    //设置图片,名字
  pasteAction->setShortcut(tr("Ctrl+V"));    //设置快捷键
  pasteAction->setStatusTip(tr("paste file"));    //设置提示
  connect(copyAction, SIGNAL(triggered()), showWidget->text, SLOT(paste()));

  //关于
//  QApplication *app;
  aboutAction = new QAction(tr("about"), this);
  connect(aboutAction, SIGNAL(triggered()), this, SLOT(QApplication::aboutQt()));

  //打印文本
  PrintTextAction = new QAction(QIcon(":/printText.png"), tr("printtext"), this);
  PrintTextAction->setStatusTip(tr("print a text"));
  connect(PrintTextAction, SIGNAL(triggered()), this, SLOT(ShowPrintText()));   //连接相应的动作

  //打印图像
  PrintImageAction = new QAction(QIcon(":/printImage.png"), tr("printImage"), this);
  PrintImageAction->setStatusTip(tr("print a image"));
  connect(PrintImageAction, SIGNAL(triggered()), this, SLOT(ShowPrintImage()));

  //放大图像
  zoomInAction = new QAction(QIcon(":/zoomin.png"), tr("get big"), this);
  zoomInAction->setStatusTip(tr("get big image"));

  //缩小
  zoomOutAction = new QAction(QIcon(":/zoomout.png"), tr("get small"), this);
  zoomOutAction->setStatusTip(tr("get small image"));


  //实现图片的选择动作
  //旋转90°
  rotate90Action = new QAction(QIcon(":/rotate90.png"), tr("get rotate90"), this);
  rotate90Action->setStatusTip(tr("get rotate90 image"));
  //180°
  rotate180Action = new QAction(QIcon(":/rotate180.png"), tr("get rotate180"), this);
  rotate180Action->setStatusTip(tr("get rotate180 image"));
  //270°
  rotate270Action = new QAction(QIcon(":/rotate270.png"), tr("get rotate270"), this);
  rotate270Action->setStatusTip(tr("get rotate270 image"));


  //实现图像镜像的动作
  //纵向
  mirrorVerticalAction = new QAction(QIcon("mirrorVertical.png"), tr("mirrorVertical"), this);
  mirrorVerticalAction->setStatusTip(tr("mirrorVertical the image"));
  //横向
  mirrorHorizontalAction = new QAction(QIcon("mirrorHorizontal.png"), tr("mirrorHorizontal"), this);
  mirrorHorizontalAction->setStatusTip(tr("mirrorHorizontal the image"));

  //实现撤销和恢复的动作
  //撤销和恢复
  undoAction = new QAction(QIcon(":/undo.png"), "undo", this);    //撤销
  connect(undoAction, SIGNAL(triggered()), showWidget->text, SLOT(undo()));

  redoAction = new QAction(QIcon(":/redo.png"), "redo", this);    //重做
  connect(undoAction, SIGNAL(triggered()), showWidget->text, SLOT(redo()));

}

//实现打印图像功能的函数
void ImageProcessor::ShowPrintImage()
{
  QPrinter printer;
  QPrintDialog printDialog(&printer, this);
  //是否点击确定
  if(printDialog.exec())
    {
      QPainter painter(&printer);
      QRect rect = painter.viewport();    //获得QPainter对象的视角矩形区域
      QSize size = img.size();    //图片的尺寸
      size.scale(rect.size(), Qt::KeepAspectRatio);

      painter.setViewport(rect.x(), rect.y(), size.width(), size.height());   //设置矩形区域
      painter.drawImage(0, 0, img);
    }

}

//实现打印文本功能
void ImageProcessor::ShowPrintText()
{
//  QP
  QPrinter printer;   //打印
  QPrintDialog printDialog(&printer, this);
  if(printDialog.exec())    //是否点击确定
    {
      QTextDocument *doc = showWidget->text->document();    //得到文档对象
      doc->print(&printer);   //开始打印
    }

}

//打开文件
void ImageProcessor::ShowOpenFile()
{
  fileName = QFileDialog::getOpenFileName(this);    //得到文件名
  if(!fileName.isEmpty())   //文件不为空
    {
      if(showWidget->text->document()->isEmpty()) //中心部件的内容的根节点不是空
        {
          loadFile(fileName);   //添加文件
        }
      else
        {
          ImageProcessor *newImageProcessor = new ImageProcessor;   //创建一个新的my word文档
          newImageProcessor->show();    //显示出来
          newImageProcessor->loadFile(fileName);    //添加文件
        }
    }
}

//实现loadFile函数,这个文件的处理后面会再次进行学习
void ImageProcessor::loadFile(QString filename)
{
  printf("file name:%s\n", filename.data());    //输出文件名

  QFile file(filename);
  if(file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
      QTextStream textStream(&file);
      while(!textStream.atEnd())    //循环到后面
        {
          showWidget->text->append(textStream.readLine());    //给文档添加文件
          printf("read line\n");
        }
      printf("end\n");
    }

}

//新建文件槽
void ImageProcessor::ShowNewFile()
{
//  qDebug()<<"????";
  ImageProcessor *newImageProcessor = new ImageProcessor;
  newImageProcessor->show();    //一个新的文档显示
}

ImageProcessor::~ImageProcessor()
{

}

main.cpp

#include "imageprocessor.h"

#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  ImageProcessor w;
  w.show();

  return a.exec();
}

ImageProcessor.pro

#-------------------------------------------------
#
# Project created by QtCreator 2015-01-18T10:01:31
#
#-------------------------------------------------

QT       += core gui

QT += printsupport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = ImageProcessor
TEMPLATE = app


SOURCES += main.cpp\
        imageprocessor.cpp \
    showwidget.cpp

HEADERS  += imageprocessor.h \
    showwidget.h

RESOURCES += \
    images.qrc

运行结果:








哎 考完试了,应该可以正常点了,现在还在慢慢调整中。

不过最近LPL联赛真的很有意思啊!!!!有木有!!!支持OMG














评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值