Qt 操作words实例 --- 幼儿快算题生成器
生活随笔
收集整理的這篇文章主要介紹了
Qt 操作words实例 --- 幼儿快算题生成器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Qt 操作words實例 — 幼兒快算題生成器
幼兒教育一直受到重視,一年級開始,一般學校都會要求口算達標,為了更好的促進孩子學習,是否可以制作這樣一款word 口算練習題生成器呢?不妨用Qt來試試看吧!
先看下界面:
代碼實現:
1、。pro 文件中添加:
CONFIG += qaxcontainer
2、將word操作的函數單獨寫類
請看:qword.h文件
關于這部分程序的撰寫,這里先不做過多的解釋,在《Qt5 開發與實例 第四版中有介紹》,網上也有一些介紹。
這里有一篇博客寫得不錯,
http://www.360doc.com/content/14/0227/16/7918060_356177077.shtml
接著我們來看關鍵代碼部分
#include "mainwindow.h" #include "ui_mainwindow.h"#include "Word/qword.h" #include <QDateTime> #include<QDir> #include<qrandom.h>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) {ui->setupUi(this);minValue = 1; //最小值maxValue = 10; //最大值maxResult = 100; //結果上限pages = 10; //生成的份數_addEn = true;_minusEn = true;_mulitEn = true;_divideEn = true;}MainWindow::~MainWindow() {delete ui; }//生成快題 void MainWindow::on_pushButton_produce_clicked() {QString current_Date_Time = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");QString fileName = tr("幼兒快速口算訓練習題")+"_" + "_" + current_Date_Time;fileName.replace("-", "");fileName.replace(":", "");fileName.replace(" ", "");QString filePath = "";QDate Cur_Date= QDate::currentDate();QString strCurDate = Cur_Date.toString("yyyy-MM-dd");filePath = "E:/demo/QWordDemo/ReportWord/"+strCurDate+"/";QDir dirReportPath(filePath);if (!dirReportPath.exists()){if (dirReportPath.mkpath(filePath)){filePath += fileName + tr(".doc");}}else{filePath += fileName + tr(".doc");}QWord word;if( !word.createNewWord(filePath) ){QString error = tr("Failed to export exercise,") + word.getStrErrorInfo();return;}word.setPageOrientation(0); //頁面方向word.setWordPageView(3); //頁面視圖for(int p = 1;p<= pages;p++){word.setParagraphAlignment(0); //下面文字位置word.setFontSize(30); //字體大小word.setFontBold(true); //字體加粗word.insertText(tr("幼兒快速口算訓練習題( ")+QString::number(p)+ " )");word.setFontBold(false);word.insertMoveDown();word.setFontSize(10);word.setParagraphAlignment(2);// QString current_Time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");word.insertText(tr("日期________________ 時間________________ 分數________________"));word.insertMoveDown();word.insertMoveDown();QString str = "";word.intsertTable(36,5);for(int row = 1;row<=36;row++){for(int colum = 1;colum<=5;colum++){str.clear();QTime time;time= QTime::currentTime();qsrand(time.msec()+time.second()*1000);qrand();qrand();nums0=qrand()%(maxValue - minValue) + minValue;nums1=qrand()%(maxValue - minValue) + minValue;while(DoOperation())str.clear();str = QString::number(nums0) + operator_str + QString::number(nums1) + " =";word.setCellString(p,row,colum,str);}word.setRowAlignment(p,row,3);}word.setTableAutoFitBehavior(0);word.moveForEnd();}word.setVisible(true);word.saveAs(); }bool MainWindow:: DoOperation() {bool loopflag = true;QTime time;time= QTime::currentTime();qsrand(time.msec()+time.second()*1000);qrand();int var = qrand()%4 + 1;switch (var) {case 1:loopflag = (_addEn==true)?false:true;if(!loopflag){operator_str = " + ";}break;case 2:loopflag = (_addEn==true)?false:true;if(!loopflag){operator_str = " - ";}break;case 3:loopflag = (_addEn==true)?false:true;if(!loopflag){operator_str = " × ";}break;case 4:loopflag = (_addEn==true)?false:true;if(!loopflag){operator_str = " ÷ ";}break;default:loopflag = true;break;}return loopflag; }void MainWindow::on_checkBox_add_clicked(bool checked) {if(checked){_addEn = true;}else{_addEn = false;} }void MainWindow::on_checkBox_multi_clicked(bool checked) {if(checked){_mulitEn = true;}else{_mulitEn = false;} }void MainWindow::on_checkBox_minus_clicked(bool checked) {if(checked){_minusEn = true;}else{_minusEn = false;} }void MainWindow::on_checkBox_divide_clicked(bool checked) {if(checked){_divideEn = true;}else{_divideEn = false;} }void MainWindow::on_spinBox_minValue_valueChanged(int arg1) {minValue = arg1; }void MainWindow::on_spinBox_maxValue_valueChanged(int arg1) {maxValue = arg1; }void MainWindow::on_spinBox_maxResult_valueChanged(int arg1) {maxResult = arg1; }void MainWindow::on_spinBox_pages_valueChanged(int arg1) {pages = arg1; }效果:
總結:
Qt 操作word 還是非常方便的,關鍵在于理解操作的機制,這樣才能靈活應用。
下載鏈接:
https://download.csdn.net/download/qq_21291397/12299262
總結
以上是生活随笔為你收集整理的Qt 操作words实例 --- 幼儿快算题生成器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于笛卡尔坐标系下的三边定位的研究(TO
- 下一篇: Qt 多线程的简单演示