简体   繁体   English

使用QAxObject两次无法读取文件数据

[英]Can't read file data twice using QAxObject

I read excel file using QAxBase and QAxObject . 我使用QAxBaseQAxObject读取了excel文件。

I got global variable QAxObject* db_workbook; 我得到了全局变量QAxObject* db_workbook; where I store pointer to some workbook(don't know why it called like that, but whatever) in excel. 我在excel中存储指向某些工作簿的指针(不知道为什么这样调用它,但无论如何)的地方。 I need it because of throught it I need to get excel file data in few functions, not just only one. 我需要它,因为我需要它以几种功能(不仅是一种)获得excel文件数据。

When readExcelFile method executes fine, in test_function() an exception readExcelFile方法执行正常时,在test_function()出现异常

在此处输入图片说明

appears at this line sheet_N = db_workbook->querySubObject("Worksheets(int)", 1); 出现在此行sheet_N = db_workbook->querySubObject("Worksheets(int)", 1);

Why does it happens and how to fix it? 为什么会发生以及如何解决?

Code part is here 代码部分在这里

//
QAxObject* db_workbook;

//for read what we need to
void importdb_module::readExcelFile(QAxObject* excel, QString& file_path){

    if(initExcel(excel)){

        QAxObject* workbooks = excel->querySubObject("WorkBooks");
        workbooks->dynamicCall("Open (const QString&)", file_path);
        QAxObject* workbook = excel->querySubObject("ActiveWorkBook");
        db_workbook = workbook;//global ptr points same adress now
        QAxObject* worksheets = workbook->querySubObject("WorkSheets");

        //test getting sheet num 1 name
        QAxObject* sheet_hh = workbook->querySubObject("Worksheets(int)", 1);

        QString sheet_name = sheet_hh->property("Name").toString();

        qDebug()<<sheet_name<<"TEST!";//here everything works fine
    }
}

void importdb_module::test_function(){
    QAxObject* sheet_N;
    //ERROR IS HERE!
    sheet_N = db_workbook->querySubObject("Worksheets(int)", 1);

    QString sheet_name = sheet_N->property("Name").toString();

    qDebug()<<sheet_name;
}


//executes on button click
void importdb_module::testExlOp(QString &_path){
    QAxObject* excel;
    QStringList spreadsheet_list; //get spreadsheet list when opening file
    QString path = _path;//gonna use GUI choose

    if(initExcel(excel)){
        if (readExcelFile(excel, path)){
            //
            test_function();
            excel->dynamicCall("Quit(void)");
            }else{
            //error output
            QMessageBox::information(0, "", "Error");
        }
    }


    delete excel;
}

Thank you! 谢谢!

Probably you have a dangling pointer. 可能您有一个悬空的指针。 Use QPointer<QAxObject> instead of a naked pointer. 使用QPointer<QAxObject>代替裸指针。 It will reset itself to null when the instance of QAxObject gets destructed. QAxObject的实例被破坏时,它将自身重置为null。

Generally speaking, in this day and age, you're not supposed to be using naked pointers for anything that's not implicitly owned by something else. 一般来说,在当今时代,您不应将裸指针用于未隐式拥有的任何事物。 This means that for QObjects that have parents you don't need to do use smart pointers (although it doesn't hurt any). 这意味着对于具有父级的QObjects ,您不需要使用智能指针(尽管它不会对任何对象造成伤害)。 Generally speaking, smart pointers don't hurt. 一般来说,智能指针不会受到伤害。 Use them. 使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM