qt QAxWidget打开Office文件及pdf

 承接上章,该扯皮的扯完了,直接进入正题:

顾名思义,这篇先介绍下QAxwidget来操作office和pdf
 QAxwidget,即一个ActiveX控件的qt版本,方便我们在qt程序中调用显示,可以直接调用com组件。关于QAxwidget的介绍,官网上有更详细的,可以自行查看:
QAxwidget官网介绍
此外,引用别人的一张图,能够更好理解继承结构:
这里写图片描述
传送门

开发环境:windows7+vs2013+qt5.4(32位)。

一、QAxwidget操作office:

 如果仅仅是使用QAxwidget来打开pdf文件还是较为简单的,网上也更多这方面的例子,但也基本上浅尝辄止,没有更深的内容了,很遗憾,我也是,如果要详细的api,需要直接去找com组件的api,但是我当时只看了pdf的api。

使用QAXwidget操作office文件时,必须电脑装有office,office2007以上都可以(03没试过,未知),同时qt5.7如果是调用dilaog打开文件来显示有效果,如果是直接load没效果,大约是一个bug。

准备工作:使用QAxwidget需要添加库:

QT       += axcontainer 
    
    
  • 1

1、搞个简单的界面:
这里写图片描述
点击按钮打开个文件选择框(QFileDialog),根据选择文件来确定打开的文件格式

void MainWindow::on_pushButton_clicked()
{
    QFileDialog dialog;
    dialog.setFileMode(QFileDialog::ExistingFile);
    dialog.setViewMode(QFileDialog::Detail);
    dialog.setOption(QFileDialog::ReadOnly, true);
    dialog.setWindowTitle(QString("QAXwidget操作文件"));
    dialog.setDirectory(QString("./"));
    dialog.setNameFilter(QString("所有文件(*.*);;excel(*.xlsx);;word(*.docx *.doc);;pdf(*.pdf)"));
    if (dialog.exec())
    {
    //根据文件后缀打开
        QStringList files = dialog.selectedFiles();
        for(auto filename : files)
        {
            if(filename.endsWith(".xlsx"))
            {
                this->OpenExcel(filename);
            }
            else if(filename.endsWith(".docx") || filename.endsWith(".doc"))
            {
                this->OpenWord(filename);
            }
        }
    }
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2、根据所选择的文件打开:

void MainWindow::OpenExcel(QString &filename)
{
    this->CloseOffice();
    officeContent_ = new QAxWidget("Excel.Application", this->ui->widget);
    officeContent_->dynamicCall("SetVisible (bool Visible)","false");//不显示窗体
    officeContent_->setProperty("DisplayAlerts", false);
    auto rect = this->ui->widget->geometry();
    officeContent_-> setGeometry(rect);
    officeContent_->setControl(filename);
    officeContent_->show();
}

void MainWindow::OpenWord(QString &filename)
{
    this->CloseOffice();
    officeContent_ = new QAxWidget("Word.Application", this->ui->widget);
    officeContent_->dynamicCall("SetVisible (bool Visible)","false");//不显示窗体
    officeContent_->setProperty("DisplayAlerts", false);
    auto rect = this->ui->widget->geometry();
    officeContent_-> setGeometry(rect);
    officeContent_->setControl(filename);
    officeContent_->show();
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

3、就打开了,效果如下(忽略word文档内容,以前的博客内容):
这里写图片描述

4、需要注意的是,使用完之后记得关干净,不然打开的word.exe还在进程。

void MainWindow::CloseOffice()
{
    if(this->officeContent_)
    {
        officeContent_->close();
        officeContent_->clear();
        delete officeContent_;
        officeContent_ = nullptr;
    }
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

二、QAxwidget操作pdf:

 其实打开pdf也类似,在放弃office文件之后选择了打开pdf文件。
 注意,需要安装adobe pdf reader:
这里写图片描述
1、在打开文件后选择分支处加一个选项:

 else if(filename.endsWith(".docx") || filename.endsWith(".doc"))
 {
  this->OpenWord(filename);
 }
  else if(filename.endsWith(".pdf"))
  {
    this->OpenPdf(filename);
  }
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2、打开pdf文件:
在布局上加一个类型gridLayout:
这里写图片描述
添加函数:

void MainWindow::OpenPdf(QString &filename)
{
    this->CloseOffice();
    officeContent_ = new QAxWidget(this);
    if(!officeContent_->setControl("Adobe PDF Reader"))
        QMessageBox::critical(this, "Error", "没有安装pdf!");
    this->ui->gridLayout->addWidget(officeContent_);
    officeContent_->dynamicCall(
                "LoadFile(const QString&)",
                filename);
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

这里有点不一样了,不能通过打开office的方式来直接打开。
有兴趣的话可以翻看官方文档,我找了好久找到了:传送门

3、完事儿,打开效果如下:
这里写图片描述

三、总结:

 通过QAxwidget来操作office文件和pdf文件,有以下几点优缺点:
1、很方便显示这些,几乎没什么代码量,直接可以看到效果。
2、本质其实就是调用别的软件来显示,跟自己没半毛钱关系,屏蔽不了按键,不能让他无法编辑,无法复制,无法保存,故而弃之。
3、需要源码的话可以留评论,我再上传。

更新:
代码地址:
传送门



转载自:https://blog.csdn.net/zy19940906/article/details/61426429

 承接上章,该扯皮的扯完了,直接进入正题:

顾名思义,这篇先介绍下QAxwidget来操作office和pdf
 QAxwidget,即一个ActiveX控件的qt版本,方便我们在qt程序中调用显示,可以直接调用com组件。关于QAxwidget的介绍,官网上有更详细的,可以自行查看:
QAxwidget官网介绍
此外,引用别人的一张图,能够更好理解继承结构:
这里写图片描述
传送门

开发环境:windows7+vs2013+qt5.4(32位)。

一、QAxwidget操作office:

 如果仅仅是使用QAxwidget来打开pdf文件还是较为简单的,网上也更多这方面的例子,但也基本上浅尝辄止,没有更深的内容了,很遗憾,我也是,如果要详细的api,需要直接去找com组件的api,但是我当时只看了pdf的api。

使用QAXwidget操作office文件时,必须电脑装有office,office2007以上都可以(03没试过,未知),同时qt5.7如果是调用dilaog打开文件来显示有效果,如果是直接load没效果,大约是一个bug。

准备工作:使用QAxwidget需要添加库:

QT       += axcontainer 
  
  
  • 1

1、搞个简单的界面:
这里写图片描述
点击按钮打开个文件选择框(QFileDialog),根据选择文件来确定打开的文件格式

void MainWindow::on_pushButton_clicked()
{
    QFileDialog dialog;
    dialog.setFileMode(QFileDialog::ExistingFile);
    dialog.setViewMode(QFileDialog::Detail);
    dialog.setOption(QFileDialog::ReadOnly, true);
    dialog.setWindowTitle(QString("QAXwidget操作文件"));
    dialog.setDirectory(QString("./"));
    dialog.setNameFilter(QString("所有文件(*.*);;excel(*.xlsx);;word(*.docx *.doc);;pdf(*.pdf)"));
    if (dialog.exec())
    {
    //根据文件后缀打开
        QStringList files = dialog.selectedFiles();
        for(auto filename : files)
        {
            if(filename.endsWith(".xlsx"))
            {
                this->OpenExcel(filename);
            }
            else if(filename.endsWith(".docx") || filename.endsWith(".doc"))
            {
                this->OpenWord(filename);
            }
        }
    }
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2、根据所选择的文件打开:

void MainWindow::OpenExcel(QString &filename)
{
    this->CloseOffice();
    officeContent_ = new QAxWidget("Excel.Application", this->ui->widget);
    officeContent_->dynamicCall("SetVisible (bool Visible)","false");//不显示窗体
    officeContent_->setProperty("DisplayAlerts", false);
    auto rect = this->ui->widget->geometry();
    officeContent_-> setGeometry(rect);
    officeContent_->setControl(filename);
    officeContent_->show();
}

void MainWindow::OpenWord(QString &filename)
{
    this->CloseOffice();
    officeContent_ = new QAxWidget("Word.Application", this->ui->widget);
    officeContent_->dynamicCall("SetVisible (bool Visible)","false");//不显示窗体
    officeContent_->setProperty("DisplayAlerts", false);
    auto rect = this->ui->widget->geometry();
    officeContent_-> setGeometry(rect);
    officeContent_->setControl(filename);
    officeContent_->show();
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

3、就打开了,效果如下(忽略word文档内容,以前的博客内容):
这里写图片描述

4、需要注意的是,使用完之后记得关干净,不然打开的word.exe还在进程。

void MainWindow::CloseOffice()
{
    if(this->officeContent_)
    {
        officeContent_->close();
        officeContent_->clear();
        delete officeContent_;
        officeContent_ = nullptr;
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

二、QAxwidget操作pdf:

 其实打开pdf也类似,在放弃office文件之后选择了打开pdf文件。
 注意,需要安装adobe pdf reader:
这里写图片描述
1、在打开文件后选择分支处加一个选项:

 else if(filename.endsWith(".docx") || filename.endsWith(".doc"))
 {
  this->OpenWord(filename);
 }
  else if(filename.endsWith(".pdf"))
  {
    this->OpenPdf(filename);
  }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2、打开pdf文件:
在布局上加一个类型gridLayout:
这里写图片描述
添加函数:

void MainWindow::OpenPdf(QString &filename)
{
    this->CloseOffice();
    officeContent_ = new QAxWidget(this);
    if(!officeContent_->setControl("Adobe PDF Reader"))
        QMessageBox::critical(this, "Error", "没有安装pdf!");
    this->ui->gridLayout->addWidget(officeContent_);
    officeContent_->dynamicCall(
                "LoadFile(const QString&)",
                filename);
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

这里有点不一样了,不能通过打开office的方式来直接打开。
有兴趣的话可以翻看官方文档,我找了好久找到了:传送门

3、完事儿,打开效果如下:
这里写图片描述

三、总结:

 通过QAxwidget来操作office文件和pdf文件,有以下几点优缺点:
1、很方便显示这些,几乎没什么代码量,直接可以看到效果。
2、本质其实就是调用别的软件来显示,跟自己没半毛钱关系,屏蔽不了按键,不能让他无法编辑,无法复制,无法保存,故而弃之。
3、需要源码的话可以留评论,我再上传。

更新:
代码地址:
传送门



猜你喜欢

转载自blog.csdn.net/u011731378/article/details/80984972