Qt浅谈之八:富文本转换成pdf

来源:互联网 发布:仓管要会什么软件 编辑:程序博客网 时间:2024/06/16 02:33

一、简介

       Qt对富文本的处理,主要有几个感兴趣的知识点才写下这篇文章,将文本或图片转换成pdf格式、文件直接拖拽到文本框中、双击对程序全屏和缩小、滚动滑轮对文字放大缩小及安装事件过滤器通过键盘的上下按键对文本放大缩小。

二、运行图

(1)运行效果图如下图1所示。


三、详解

1、文本文件转换成pdf

[cpp] view plaincopy
  1. void MainWindow::createPdf()  
  2. {  
  3.   QString fileName = QFileDialog::getSaveFileName(this, tr("导出PDF文件"),  
  4.                                                   QString(), "*.pdf");  
  5.   if (!fileName.isEmpty()) {  
  6.   
  7.       // 如果文件后缀为空,则默认使用.pdf  
  8.       if (QFileInfo(fileName).suffix().isEmpty())  
  9.           fileName.append(".pdf");  
  10.       QPrinter printer;  
  11.       // 指定输出格式为pdf  
  12.       printer.setOutputFormat(QPrinter::PdfFormat);  
  13.       printer.setOutputFileName(fileName);  
  14.       ui->textEdit->print(&printer);  
  15.   }  
  16. }  
调用QTextEdit的print函数进行转换输出。输出效果如下图2所示。

2、拖放功能

[cpp] view plaincopy
  1. // 拖动进入事件  
  2. void MainWindow::dragEnterEvent(QDragEnterEvent *event)  
  3. {  
  4.     // 数据中是否包含URL,如果是则接收动作,否则忽略该事件  
  5.     if(event->mimeData()->hasUrls()) {  
  6.         event->acceptProposedAction();  
  7.     }  
  8.     else event->ignore();  
  9. }  
  10. // 放下事件  
  11. void MainWindow::dropEvent(QDropEvent *event)  
  12. {  
  13.     // 获取MIME数据  
  14.     const QMimeData *mimeData = event->mimeData();  
  15.     // 如果数据中包含URL  
  16.     if(mimeData->hasUrls()){  
  17.         // 获取URL列表  
  18.         QList<QUrl> urlList = mimeData->urls();  
  19.         // 将其中第一个URL表示为本地文件路径  
  20.         QString fileName = urlList.at(0).toLocalFile();  
  21.         QFileInfo info(fileName);  
  22.         // 如果文件路径不为空  
  23.         if(!fileName.isEmpty()){  
  24.             // 建立QFile对象并且以只读方式打开该文件  
  25.             QFile file(fileName);  
  26.             if(!file.open(QIODevice::ReadOnly)) return;  
  27.             // 建立文本流对象  
  28.             QTextStream in(&file);  
  29.             // 将文件中所有内容读入编辑器  
  30.             setWindowTitle(tr("文件转换:%1").arg(info.fileName()));  
  31.             ui->textEdit->setText(in.readAll());  
  32.         }  
  33.     }  
  34. }  
在此只介绍文本的简单拖放,重载两个函数 void dragEnterEvent(QDragEnterEvent *event); //拖动进入事件void dropEvent(QDropEvent *event); //放下事件,实现拖动文件到多信息文本编辑器中去,后面还会有相应的文章专门介绍文本和图片的拖放及不同程序间的拖放。

3、全屏

[cpp] view plaincopy
  1. // 鼠标双击事件  
  2. void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)  
  3. {  
  4.     // 如果是鼠标左键按下  
  5.     if(event->button() == Qt::LeftButton){  
  6.   
  7.         // 如果现在不是全屏,将窗口设置为全屏  
  8.         if(windowState() != Qt::WindowFullScreen)  
  9.             setWindowState(Qt::WindowFullScreen);  
  10.   
  11.         // 如果现在已经是全屏状态,那么恢复以前的大小  
  12.         else setWindowState(Qt::WindowNoState);  
  13.     }  
  14. }  
鼠标双击文本编辑器和菜单栏外的位置都会全屏显示(全屏将隐藏菜单栏),再双击回到原来大小。

4、滚轮放大缩小

[cpp] view plaincopy
  1. // 滚轮事件  
  2. void MainWindow::wheelEvent(QWheelEvent *event)  
  3. {  
  4.     // 当滚轮远离使用者时进行放大,当滚轮向使用者方向旋转时进行缩小  
  5.     if(event->delta() > 0){  
  6.         ui->textEdit->zoomIn();  
  7.     }else{  
  8.         ui->textEdit->zoomOut();  
  9.     }  
  10. }  
向前滚动滑轮delta值大于0放大操作,向后滚动缩小操作,其运行如下图3所示。


5、方向键上下放大缩小

在MainWindow上为lineEdit安装事件过滤器, ui->textEdit->installEventFilter(this);

[cpp] view plaincopy
  1. bool MainWindow::eventFilter(QObject *obj, QEvent *event)  
  2. {  
  3. //  // 事件过滤器  
  4. //  // 如果是textEdit部件上的事件  
  5.   if(obj == ui->textEdit){  
  6.     if(event->type() == QEvent::KeyPress) {  
  7.       QKeyEvent *kevent = dynamic_cast<QKeyEvent *>(event);  
  8.       if (kevent->key() == Qt::Key_Down) {  
  9.         ui->textEdit->zoomOut();  
  10.       }  
  11.       else if(kevent->key() == Qt::Key_Up) {  
  12.         ui->textEdit->zoomIn();  
  13.       }  
  14.     }  
  15.   }  
  16.   return QMainWindow::eventFilter(obj,event);  
  17. }  
捕获方向键的上下按键进行放大缩小操作。其运行如下图3所示。

0 0
原创粉丝点击