<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[SOLVED] MS Excel and QThread]]></title><description><![CDATA[<p dir="auto">Hello all!</p>
<p dir="auto">Here I am again with a new problem on acessing MS Excel with QT, this time involving threads.<br />
I have the folowing code that perform some Excel actions (I've "posted":<a href="http://developer.qt.nokia.com/forums/viewthread/13656/" target="_blank" rel="noopener noreferrer nofollow ugc">http://developer.qt.nokia.com/forums/viewthread/13656/</a> this code regarding another problem before):<br />
@{<br />
QAxObject* excel = new QAxObject( "Excel.Application", 0 );<br />
QAxObject* workbooks = excel-&gt;querySubObject( "Workbooks" );<br />
QAxObject* workbook = workbooks-&gt;querySubObject( "Open(const QString&amp;)", "C:\Users\LocalDev\teste.xls" );<br />
QAxObject* sheets = workbook-&gt;querySubObject( "Worksheets" );</p>
<pre><code>sheets-&gt;dynamicCall("Add()");
QAxObject* sheet = sheets-&gt;querySubObject( "Item( int )", 1 );
sheet-&gt;setProperty("Name","Nova Planilha");

QAxObject * range = sheet-&gt;querySubObject("Cells( int, int)",1,1);
range-&gt;setProperty("Value", QVariant(1234));

QAxObject * shapes = sheet-&gt;querySubObject("Shapes");
shapes-&gt;dynamicCall("AddPicture( QString&amp;, bool, bool, double, double, double, double","C:\\Users\\LocalDev\\Pictures\\cvrd1.png",true,true,100,100,70,70);

excel-&gt;setProperty("DisplayAlerts", false);
excel-&gt;dynamicCall("Save()");

//workbook-&gt;dynamicCall("Close()");
//excel-&gt;dynamicCall("Quit()");

workbook-&gt;dynamicCall("Close (Boolean)", true);
excel-&gt;dynamicCall("Quit (void)");

delete shapes;
delete range;
delete sheet;
delete sheets;
delete workbook;
delete workbooks;
delete excel;
</code></pre>
<p dir="auto">}@</p>
<p dir="auto">As my specifc code may take a long time to finalize I decided to run it in a separate thread. For this task, I subclassed a QThread class and put this code inside the method "run()" of my new inherited class.</p>
<p dir="auto">When executing the code, if I call the "run()" method of my class directly (this way not executing as a thread) then the code works fine, but if I call the method "start()" (to execute as a thread) then it breaks at line 4 (while trying to open the file) with a popup message reporting an "Exception Triggered". The popup message says:</p>
<p dir="auto">"The inferior stopped because it triggered an exception.<br />
Stopped in thread 9 by: Exception at 0x45728a, code: 0x0000005, read access violation at:0x0, flags=0x0"</p>
<p dir="auto">Well, I can't understand why this code does not execute correctly as a separate thread. I may be doing something silly and as I'm not that experienced with QT I'm asking if someone could help me, please.</p>
<p dir="auto">Thanks for the attention.</p>
<p dir="auto">Leo</p>
]]></description><link>https://forum.qt.io/topic/13472/solved-ms-excel-and-qthread</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Apr 2026 14:18:59 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/13472.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 26 Jan 2012 14:38:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED] MS Excel and QThread on Tue, 31 Jul 2012 17:06:22 GMT]]></title><description><![CDATA[<p dir="auto">Thank you!<br />
Unfortunately CoInitialize only works in MSVC, but i need to use mingw, so i could not init my QAxObject in a different thread then the main thread. I've solved it with a workaround, but your comment will be useful in the future when i need this again, and i'll use MSVC.<br />
Thanks.</p>
]]></description><link>https://forum.qt.io/post/146165</link><guid isPermaLink="true">https://forum.qt.io/post/146165</guid><dc:creator><![CDATA[gmc44]]></dc:creator><pubDate>Tue, 31 Jul 2012 17:06:22 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] MS Excel and QThread on Tue, 31 Jul 2012 16:58:34 GMT]]></title><description><![CDATA[<p dir="auto">Hello gmc44,</p>
<p dir="auto">yes, I use MSVC compiler.<br />
CoInitialize() is a function from Windows API to initialize the COM Library, it is not from QT framework.<br />
Sice the time of my last post in this thread I don't deal with this project anymore, so I may not remember all the details. But my class definition header has the following include files:<br />
@#include &lt;shlobj.h&gt;<br />
#include &lt;QObject&gt;<br />
#include &lt;Qthread&gt;<br />
#include &lt;QAxObject&gt;<br />
#include &lt;ActiveQt/qaxobject.h&gt;<br />
#include &lt;ActiveQt/qaxbase.h&gt;@</p>
<p dir="auto">In the run() method of my QThread inherited class I call the CoInitialize() before using other funcitons:</p>
<p dir="auto">@CoInitializeEx(NULL, COINIT_MULTITHREADED);@</p>
<p dir="auto">And this should be enough.<br />
Hope it helps.</p>
<p dir="auto">Leo</p>
]]></description><link>https://forum.qt.io/post/146163</link><guid isPermaLink="true">https://forum.qt.io/post/146163</guid><dc:creator><![CDATA[leo1756]]></dc:creator><pubDate>Tue, 31 Jul 2012 16:58:34 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] MS Excel and QThread on Thu, 26 Jul 2012 09:14:22 GMT]]></title><description><![CDATA[<p dir="auto">Hi,<br />
do you use MSVC compiler? i cannot find this CoInitialize() function... Could you share more details of your solution, since this is a rare topic...<br />
Thanks!</p>
]]></description><link>https://forum.qt.io/post/145673</link><guid isPermaLink="true">https://forum.qt.io/post/145673</guid><dc:creator><![CDATA[gmc44]]></dc:creator><pubDate>Thu, 26 Jul 2012 09:14:22 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] MS Excel and QThread on Thu, 26 Jan 2012 17:11:30 GMT]]></title><description><![CDATA[<p dir="auto">I found the solution. We must call "CoInitialize()" when working with ActiveQt in threads. I just called it before running my code and it works well.</p>
<p dir="auto">Leo</p>
]]></description><link>https://forum.qt.io/post/127112</link><guid isPermaLink="true">https://forum.qt.io/post/127112</guid><dc:creator><![CDATA[leo1756]]></dc:creator><pubDate>Thu, 26 Jan 2012 17:11:30 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] MS Excel and QThread on Thu, 26 Jan 2012 16:01:07 GMT]]></title><description><![CDATA[<p dir="auto">Hi Acer,</p>
<p dir="auto">I'm developing with Qt Creator and already executing it in debug mode.<br />
I don't know if I got your point correctly, but this code executes fine if it is in the main program thread and it breaks in the 4th line if exactly the same code is executed in a separete thread. Maybe it is not well written, but it works. I've also checked the variables in debugger but couldn't notice any problem in them.. Furthermore, all the variables this code needs are declared there so it does't depend on any other variable delcared outside the method body.</p>
<p dir="auto">Anyway, thanks for your answer.</p>
<p dir="auto">Leo</p>
]]></description><link>https://forum.qt.io/post/127106</link><guid isPermaLink="true">https://forum.qt.io/post/127106</guid><dc:creator><![CDATA[leo1756]]></dc:creator><pubDate>Thu, 26 Jan 2012 16:01:07 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] MS Excel and QThread on Thu, 26 Jan 2012 15:05:39 GMT]]></title><description><![CDATA[<p dir="auto">Are you developing in VS? If yes, Run as debug profile and take a look at Local variables.</p>
<p dir="auto">Isn't  QAxObject * range are QAxObject shape child? Maybe it got deleted early in delete shapes. Check the delete instructions....</p>
]]></description><link>https://forum.qt.io/post/127103</link><guid isPermaLink="true">https://forum.qt.io/post/127103</guid><dc:creator><![CDATA[AcerExtensa]]></dc:creator><pubDate>Thu, 26 Jan 2012 15:05:39 GMT</pubDate></item></channel></rss>