您现在的位置是:网站首页> C/C++

Qt 打印功能

  • C/C++
  • 2021-11-11
  • 1044人已阅读
摘要
Qt做多了没有人逃得过打印模板这个功能,有的话只是时候未到,注释都在代码里了,直接上代码了
1、获取打印机的名称
  1. if(index == 1)
  2.    {
  3.        QPrinterInfo info;
  4.        QStringList list = info.availablePrinterNames(); // 所有打印机名字
  5.        ui->cB_print->clear();
  6.        ui->cB_print->setEnabled(true);
  7.        foreach (QString str, list) {
  8.            ui->cB_print->addItem(str);
  9.        }
  10.    }
  11.    else
  12.    {
  13.        QPrinterInfo info;
  14.        QString name = info.defaultPrinterName(); // 默认打印机名字
  15.        ui->cB_print->clear();
  16.        ui->cB_print->setEnabled(false);
  17.        ui->cB_print->addItem(name);
  18.    }
2、预览
  1.    QPrinter printer(QPrinter::HighResolution);
  2.    //自定义纸张大小,特别重要,不然预览效果极差
  3.    printer.setPageSize(QPrinter::Custom);
  4.    printer.setPaperSize(QSizeF(600, 800),
  5.                               QPrinter::Point);
  6.    QPrintPreviewDialog preview(&printer, this);// 创建打印预览对话框
  7.    
  8.    preview.setMinimumSize(1000,600);
  9.    /*
  10.     * QPrintPreviewDialog类提供了一个打印预览对话框,里面功能比较全,
  11.     * paintRequested(QPrinter *printer)是系统提供的,
  12.     * 当preview.exec()执行时该信号被触发,
  13.     * drawPic(QPrinter *printer)是自定义的槽函数,图像的绘制就在这个函数里。
  14.     */
  15.    connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(drawPic(QPrinter*)));
  16.    preview.exec();
  17. 然后drawPic定义:
  18. //在预览窗口显示图片
  19. void MainWindow::drawPic(QPrinter *printerPixmap)
  20. {
  21.    QPixmap pix = QPixmap(800,600);
  22.    //这个函数算是画模板的函数吧,毕竟打印时有模板的
  23.    createPix(&pix);
  24.    pix.save(sFilePix);
  25.    //纵向:Portrait 横向:Landscape
  26.    printerPixmap->setOrientation(QPrinter::Landscape);
  27.    //获取界面的图片
  28.    QPainter painterPixmap(this);
  29.    painterPixmap.begin(printerPixmap);
  30.    QRect rect = painterPixmap.viewport();
  31.    int x = rect.width() / pix.width();
  32.    int y = rect.height() / pix.height();
  33.    //设置图像长宽是原图的多少倍
  34.    painterPixmap.scale(x, y);
  35.    painterPixmap.drawPixmap(0, 0, pix);
  36.    painterPixmap.end();
  37. }
  38. void MainWindow::createPix(QPixmap *pix)
  39. {
  40.    QPainter *painter = new QPainter(this);
  41.    painter->begin(pix);
  42.    painter->setRenderHint(QPainter::Antialiasing, true);
  43.    // 设置画笔颜色、宽度
  44.    painter->setPen(QPen(QColor(255, 255, 255), 2));
  45.    // 设置画刷颜色
  46.    painter->setBrush(QColor(255, 255, 255));
  47.    QRect rect(0,0,800,600);
  48.    //整张图设置画刷白底
  49.    painter->fillRect(rect,QColor(255, 255, 255));
  50.    painter->drawRect(rect);
  51.    //画数据部分的线条
  52.    painter->setPen(QPen(QColor(0, 0, 0), 1));
  53.    QVector<QLine> lines;
  54.    lines.append(QLine(QPoint(50,50),QPoint(750,50)));//上边
  55.    lines.append(QLine(QPoint(750,50),QPoint(750,550)));//右边
  56.    lines.append(QLine(QPoint(50,550),QPoint(750,550)));//下边
  57.    lines.append(QLine(QPoint(50,50),QPoint(50,550)));//左边
  58.    lines.append(QLine(QPoint(50,120),QPoint(750,120)));//名称下边
  59.    lines.append(QLine(QPoint(50,180),QPoint(750,180)));//信息下边
  60.    lines.append(QLine(QPoint(50,200),QPoint(750,200)));//条目下边
  61.    lines.append(QLine(QPoint(50,490),QPoint(750,490)));//数据下边
  62.    lines.append(QLine(QPoint(120,180),QPoint(120,200)));//类型分割边
  63.    lines.append(QLine(QPoint(190,180),QPoint(190,200)));//类型分割边
  64.    lines.append(QLine(QPoint(260,180),QPoint(260,200)));//类型分割边
  65.    lines.append(QLine(QPoint(330,180),QPoint(330,200)));//类型分割边
  66.    lines.append(QLine(QPoint(400,180),QPoint(400,490)));//结果和曲线分割边
  67.    painter->drawLines(lines);
  68.    QFont font;
  69.    font.setPointSize(13);
  70.    font.setFamily("黑体");
  71.    font.setItalic(true);
  72.    painter->setFont(font);
  73.    //第一部分
  74.    painter->drawText(50,50,700,40,Qt::AlignCenter,ui->lE_unit->text());//单位名称
  75.    painter->drawText(QPoint(650,110),"No:");
  76.    font.setPointSize(20);
  77.    painter->setFont(font);
  78.    painter->drawText(50,80,700,40,Qt::AlignCenter,ui->lE_report->text());//报告名称
  79.    font.setPointSize(10);
  80.    painter->setFont(font);
  81.    //第二部分
  82.    painter->drawText(50,120,700,60,Qt::AlignVCenter,QString("样本ID: ")+QString("11111111"));
  83.    painter->drawText(210,120,700,60,Qt::AlignVCenter,QString("标本类型: ")+QString("ABC"));
  84.    painter->drawText(370,120,700,60,Qt::AlignVCenter,QString("检测方法: ")+ui->lE_means->text());
  85.    painter->drawText(600,120,700,60,Qt::AlignVCenter,QString("仪器: ")+ui->lE_instrument->text());
  86.    //第三部分
  87.    painter->drawText(50,180,70,20,Qt::AlignCenter,"序号");
  88.    painter->drawText(120,180,70,20,Qt::AlignCenter,"检测项目");
  89.    painter->drawText(190,180,70,20,Qt::AlignCenter,"Ct");
  90.    painter->drawText(260,180,70,20,Qt::AlignCenter,"检测下限");
  91.    painter->drawText(330,180,70,20,Qt::AlignCenter,"结果");
  92.    //第四部分
  93.    painter->drawText(50,210,70,20,Qt::AlignCenter,"1");
  94.    painter->drawText(120,210,70,20,Qt::AlignCenter,"我");
  95.    painter->drawText(190,210,70,20,Qt::AlignCenter,"真的");
  96.    painter->drawText(260,210,70,20,Qt::AlignCenter,"不知道");
  97.    painter->drawText(330,210,70,20,Qt::AlignCenter,"写啥");
  98.    //第五部分
  99.    painter->drawText(50,490,700,30,Qt::AlignVCenter,QString("送检者: ")+ui->lE_inspect->text());
  100.    painter->drawText(230,490,700,30,Qt::AlignVCenter,QString("代码: ")+ui->lE_code->text());
  101.    painter->drawText(410,490,700,30,Qt::AlignVCenter,QString("检验者: ")+ui->lE_detection->text());
  102.    painter->drawText(590,490,700,30,Qt::AlignVCenter,QString("审核者: ")+ui->lE_audit->text());
  103.    painter->drawText(50,520,700,30,Qt::AlignVCenter,QString("送检日期: ")+ui->lE_inspectDate->text());
  104.    painter->drawText(410,520,700,30,Qt::AlignVCenter,QString("报告日期: ")+ui->lE_reportDate->text());
  105.    painter->end();
  106. }
3、打印
  1. this->close();
  2.    //将文件保存
  3.    writeFile();
  4.    // 创建打印机对象
  5.    QPrinter printerPixmap;
  6.    QPixmap pix = QPixmap(800,600);
  7.    //将生成图片并保存,保不保存无所谓
  8.    createPix(&pix);
  9.    pix.save(sFilePix);
  10.    //纵向:Portrait 横向:Landscape
  11.    printerPixmap.setOrientation(QPrinter::Landscape);
  12.    //设置纸张大小
  13.    printerPixmap.setPageSize(QPrinter::A4);
  14.    
  15.    QPainter painterPixmap;
  16.    //选取打印机,特别重要
  17.    printerPixmap.setPrinterName(ui->cB_print->currentText());
  18.    //开始打印
  19.    painterPixmap.begin(&printerPixmap);
  20.    //设置图像在A4中的开始坐标是什么
  21.    painterPixmap.drawPixmap(0, 0 , pix);
  22.    painterPixmap.end();
  23.    
  24.    /*
  25.     * QPrintDialog print(&printerPixmap, this);
  26.     * 这个是点击会会出现弹框的打印,太low放弃
  27.     * 用法:
  28.     * QPrintDialog print(&printerPixmap, this);
  29.     * if (print.exec())
  30.        {
  31.            QPainter painterPixmap;
  32.            painterPixmap.begin(&printerPixmap);
  33.            QRect rect = painterPixmap.viewport();
  34.            int x = rect.width() / pixmap.width();
  35.            int y = rect.height() / pixmap.height();
  36.            painterPixmap.scale(x, y);
  37.            painterPixmap.drawPixmap(0, 0, pixmap);  
  38.            painterPixmap.end();
  39.        }
  40.    */

程序界面:

1.png

预览效果:

2.png

这里其实也是可以直接打印的,但是太low了,不想用 



Top