我们知道每次加载word控件,都会导致后台启动一个WINWORD.EXE
如何安全退出呢
1、一个最简单的例子
QT += core gui axcontainer
MainWindow.h
cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAxWidget>
#include <QAxObject>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QAxWidget *m_ax = nullptr;
QAxObject *m_wordApp = nullptr;
QAxObject *m_document = nullptr;
// QWidget interface
protected:
virtual void resizeEvent(QResizeEvent *event) override;
};
#endif // MAINWINDOW_H
MainWindow.cpp
cpp
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QAxObject>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_ax = new QAxWidget(this);
m_ax->setControl("D:\\1.docx");
m_wordApp = m_ax->querySubObject("Application");
m_document = m_wordApp->querySubObject("Documents(int index)", 1);
m_document->dynamicCall("Activate()");
}
MainWindow::~MainWindow()
{
delete ui;
m_ax->clear();
m_document->dynamicCall("Close()");
m_wordApp->dynamicCall("Quit()");
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
if (m_ax != nullptr)
{
QRect rc = rect();
m_ax->setGeometry(0, 0, rc.width(), rc.height());
}
}