修改:"Qt Creator 为我们的程序写一个动态链接库help.dll”使代码写的更专业

我们封装dll是程序更灵活,但是.h文件暴露了我的成员变量,所以我们在进行封装保护我们的成员变量外漏。

1.修改.h 文件

/*
 * @2012-10-9
 */
#ifndef HELP_H
#define HELP_H

#include <QtCore/QString>
#include "help_global.h"

class QProcess;

class HELPSHARED_EXPORT Help
{
public:
    Help();
    ~Help();

    void showDocumentation(const QString &file);

private:
        class helpPrivate *p;
};

#endif // HELP_H
2.修改 .cpp文件

/*
 * @2012-10-9
 * 帮助文档核心调用assistant.exe
 */
#include <QtCore/QByteArray>
#include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtGui/QMessageBox>

#include "help.h"
class helpPrivate
{
public:
    helpPrivate(){
        m_proc = 0;
    }

    ~helpPrivate(){
        delete m_proc;
    }

    bool startHelp();

public:
    QProcess *m_proc;
};

Help::Help()
{
    p = new helpPrivate();
}

Help::~Help()
{
    if (p->m_proc && p->m_proc->state() == QProcess::Running) {
        p->m_proc->terminate();
        p->m_proc->waitForFinished(3000);
    }
    delete p;
}

void Help::showDocumentation(const QString &page)
{
    if (!p->startHelp())
        return;
    //首页显示
    QByteArray ba("SetSource ");
    ba.append("qthelp://com.PeralGlobal.RobotWorkbench.help/doc/");
    p->m_proc->write(ba + page.toLocal8Bit() + '\n');
}

bool helpPrivate::startHelp()
{
    if (!m_proc)
        m_proc = new QProcess();
    if (m_proc->state() != QProcess::Running){
        QDir dir;
        //获取assistant.exe的路径
        QString app = dir.currentPath() + QDir::separator();
        app += QLatin1String("assistant.exe");
        //执行命令
        QStringList args;
        args << QLatin1String("-collectionFile")
            << dir.currentPath()
            + QLatin1String("/documentation/help.qhc")
            << QLatin1String("-enableRemoteControl");
        //执行进程
        m_proc->start(app, args);
        if (!m_proc->waitForStarted()){
            QMessageBox::critical(0, QObject::tr("RobotWorkbench Help"),
                QObject::tr("Unable to launch Robot Workbench Assistant (%1)").arg(app));
            return false;
        }
    }
    return true;
}


 

你可能感兴趣的:(String,delete,Class,文档,dll,qt)