本来的需求是在qt的UI界面中预览word文件,但找了半天也没有好的方法,操作和生成word文档的方法还挺多,但想要预览word文件,实在找不到。现在成功预览了WPS下的word文件,虽然我的项目用不上,还是记录如下。
在UI界面中添加frame控件。
1.pro文件
QT += axcontainer
2.cpp文件
#include <QAxObject>
#include <QAxWidget>
QString fileName = "D:\\111_202212050023.docx";
officeContent_ = new QAxWidget("Word.Application",ui->frame);
officeContent_->setProperty("Visible", false);//调用本地wps或者office 不显示
officeContent_->dynamicCall("SetVisible (bool Visible)","false");
officeContent_->setProperty("DisplayAlerts", false);//不显示任何警告信息
officeContent_->setProperty("DisplayScrollBars", true);
officeContent_->setGeometry(QRect(0,0,1500,1500));
officeContent_->setControl(fileName);
officeContent_->show();
3.存在的问题
可用在frame中显示WPS的word文件,但是关闭时关不掉WPS的后台程序。所有后续再编辑此word时,只能通过只读的方式。这个问题还没有解决。