Qt操作Office的一个实例--口算题生成器制作。
文章目錄
- Qt操作Office的一個(gè)實(shí)例--口算題生成器制作。
- 1、關(guān)聯(lián)QAxObject
- 2、操作Word文檔:
- 3、設(shè)計(jì)界面
- 4、關(guān)鍵代碼
- 效果:
Qt操作Office的一個(gè)實(shí)例–口算題生成器制作。
學(xué)校里經(jīng)常會(huì)要求口算達(dá)標(biāo)。如果手寫口算題效率太慢,何不用Qt做一個(gè)口算生成器呢?
雖然Qt自帶文本編輯功能,不過這次還是想嘗試操作COM對(duì)象來實(shí)現(xiàn)。
于是從網(wǎng)上下載一個(gè)文檔QWord.cpp 來了解Qt操作COM對(duì)象的方法。
粗略分析了QWord.cpp 的文檔,基本了解如下:
1、關(guān)聯(lián)QAxObject
要操作office需要 include <ActiveQt/QAxObject>,同時(shí)QT += axcontainer
什么是QAxObject ?
QAxObject 類提供了封裝COM對(duì)象的一些操作。
QAxObject可以被實(shí)例化一個(gè)空的對(duì)象,或者指向COM組件的一個(gè)指針。如果這個(gè)COM對(duì)象提供了IDispatch接口。那么該COM對(duì)象的屬性、方法和事件可以通過Qt的屬性、槽、信號(hào)來實(shí)現(xiàn)。
例如:初始化一個(gè)COM對(duì)象:
QAxObject* m_word;m_word = new QAxObject(parent);2、操作Word文檔:
這里引用一段代碼文檔
void QWord::setPageOrientation(int flag) //設(shè)置頁面1 橫向 還是0豎向 { QAxObject* selection = m_word->querySubObject("Selection"); //返回當(dāng)前選定COM對(duì)象的指針、if(NULL== selection){return;}QString page;switch (flag){case 0:page = "wdOrientPortrait";break;case 1:page = "wdOrientLandscape";break;}selection->querySubObject("PageSetUp")->setProperty("Orientation",page);//設(shè)置Page的橫向或者豎向的屬性。 }從上段代碼中可以發(fā)現(xiàn):用Qt可以很方便的操作word文檔的屬性。
3、設(shè)計(jì)界面
在了解了word 操作之后,我們接著來實(shí)現(xiàn)口算生成器的界面
這里我們?cè)O(shè)置加減乘除四項(xiàng)運(yùn)算。設(shè)置兩個(gè)運(yùn)算數(shù)的最大值和最小值。設(shè)置得數(shù)的上限,設(shè)置試題的份數(shù)。
4、關(guān)鍵代碼
#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; //結(jié)果上限pages = 1; //生成的份數(shù)_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("口算訓(xùn)練習(xí)題")+"_" + "_" + 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("口算訓(xùn)練習(xí)題( ")+QString::number(p)+ " )");word.setFontBold(false);word.insertMoveDown();word.setFontSize(12);word.setParagraphAlignment(2);// QString current_Time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");word.insertText(tr("日期________________ 時(shí)間________________ 分?jǐn)?shù)________________"));word.insertMoveDown();word.insertMoveDown();QString str = "";word.intsertTable(20,4);for(int row = 1;row<=20;row++){//for(int colum = 1;colum<=4;colum++){word.setColumnHeight(colum,25);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.setRowHeight(p,row,35);}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 = (_minusEn==true)?false:true;if(!loopflag){operator_str = " - ";}break;case 3:loopflag = (_mulitEn==true)?false:true;if(!loopflag){operator_str = " × ";}break;case 4:loopflag = (_divideEn==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; }效果:
總結(jié)
以上是生活随笔為你收集整理的Qt操作Office的一个实例--口算题生成器制作。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DW1000 TX POWER (发射功
- 下一篇: mysql root密码重置