如何使用QAxObject捕获/抛出错误

时间:2013-10-18 05:23:37

标签: c++ excel qt try-catch throw

我希望使用QAxObject来处理Excel文件。
我希望实现初始化,如下面的代码:

QAxObject* excel;//excel pointer

void initExcel(){
    try
    {
        //if there excel process already running try to use it
    }
    //catch if it's not running
    catch()
    {
        try 
        {
            excel = new QAxObject("Excel.Application");
        } 
        catch 
        {
            //meassge if excel not exist/can't start     
        }
    }
}

我怎么能用QAxObject捕获/抛出错误?我试图谷歌但没有找到任何exapmlpe

1 个答案:

答案 0 :(得分:2)

要知道是否加载了ActiveX控件,应该使用setControl方法的结果。要捕获ActiveX控件的异常,您应该连接到异常信号。

bool controlLoaded = axWidget->setControl("Word.Document");
if (!controlLoaded)
{
    // Message about control didn't load
}
else
{
    // Control loaded OK; connecting to catch exceptions from control
    connect(
        axWidget, 
        SIGNAL(exception(int, const QString &, const QString &, const QString &)), 
        this, 
        SLOT(onAxWidgetException(int, const QString &, const QString &, const QString &)));
}