Search code examples
multithreadingqtactivex

Multi-Threaded Excel processing with Qt?


I'm trying to process an Excel sheet to make some treatment on the data in it with Qt (4.6.3). I've tried to use an QAxWidget excel("Excel.Application") as used here.

I tried first to use it in a simple method, everything works fine but the GUI freezes for some time, so I tried to implement it in a different QThread.

As some say, it's not allowed to use inherited classes from QWidget in other threads than the main one. I also tried this way, but there's a runtime error anyway.

Here's a snippet of my code:

void MainDialog::extractData()
{
    QAxWidget excel("Excel.Application");
    excel.setProperty("Visible", false);

    QAxObject * workbooks = excel.querySubObject("WorkBooks");
    workbooks->dynamicCall("Open (const QString&)", ui->lineEditAdress->text());
    QAxObject * workbook = excel.querySubObject("ActiveWorkBook");
    QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", 1);

    // ... some work here...

    workbook->dynamicCall("Close (Boolean)", false);
    excel.dynamicCall("Quit (void)");
}

What I'm doing wrong ? Is there a better way to get the job done ? Better idea than ActiveX ?

Thanks.

Edit: As Raiv said, I replaced QAxWidget by QAxObject

Just call in the run() of the thread:

// ...
CoInitialize(0);
QAxObject excel("Excel.Application");
// ...

and it works fine.


Solution

  • Try QAxObject instead of QAxWidget, and it would work in another thread. Just don't forget to call CoInitialize() there.