tags:

views:

417

answers:

3

hey,

I have a DLL in wich I would like to take a reference to a QObject and manipulate it, without actually creating an interface. So, I included "Qt/qobject.h" and compiled, but the compiler (Visual Studio 2008 pro) gives me syntax errors. It looks like it doesn't recognize the QThread object. How do I use a QObject in my dll? Is this even possible? Do I have to start my program from a Qt app? I'm actually trying to set a system-wide hook and get 3rd application QWidgets to manipulate... Any idea how I can use QObject in my dll?

Here are the errors:

1>------ Build started: Project: FroggerDLL, Configuration: Debug Win32 ------
1>Compiling...
1>FTClient.cpp
1>c:\qt-win-opensource-src-4.5.2\src\corelib\kernel\qobject.h(154) : error C2059: syntax error : 'type'
1>c:\qt-win-opensource-src-4.5.2\src\corelib\kernel\qobject.h(154) : error C2238: unexpected token(s) preceding ';'
1>c:\qt-win-opensource-src-4.5.2\src\corelib\kernel\qobject.h(155) : error C2144: syntax error : 'int' should be preceded by ')'
1>c:\qt-win-opensource-src-4.5.2\src\corelib\kernel\qobject.h(155) : error C2144: syntax error : 'int' should be preceded by ';'
1>c:\qt-win-opensource-src-4.5.2\src\corelib\kernel\qobject.h(155) : error C2059: syntax error : ')'
1>c:\qt-win-opensource-src-4.5.2\src\corelib\kernel\qobject.h(155) : error C2208: 'int' : no members defined using this type
1>FroggerDLL - 6 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 3 up-to-date, 0 skipped ==========

Any help would be greatly appreciated,

Thanks

Dave

A: 

Try including QThread?

#include <QThread>

Qt uses forward declaration extensively and sometimes you need to include extra headers.

EDIT:

Do you set any defines? Here is what I have for my 2003 Qt commercial (4.3.4) project (executable that links to Qt dlls):

QT_LARGEFILE_SUPPORT
QT_DLL
QT_GUI_LIB
QT_CORE_LIB
QT_THREAD_SUPPORT
QT_NETWORK_LIB
Eugene
I tried this, the same errors appear...
David Menard
I don't set any defines. I am just starting out the project, and the only line of "Qt code" is my #include "Qt/qobject.h", and now #include "Qt/qthread.h". I really don<t know what's going on, and why I can't seem to use Qt headers like this.
David Menard
And thanks for the fast replies, very appreciated!
David Menard
Yeah, I think you need those defines. At least those: QT_DLL QT_GUI_LIB QT_THREAD_SUPPORT.They affect headers a lot, especially such basic ones as QObject.Did you try using VS integration? (Or is this commercial version only?)
Eugene
+1  A: 

What's on line 154? Mine is just the declaration

QThread* thread() const;

but that's 4.5.1 on Linux so it might be different. The first reference to anything involving the token type is on line 204 which is a variable of type Qt::ConnectionType.

BTW. I just tried compiling the following on my system ( in the file incqobj.cpp )

include <QOObject>
QObject myQOject;

with

g++ -I/usr/lib/qt4/include -I/usr/lib/qt4/include/QtCore -c incqobj.cpp

and it compiled fine so it should be as simple as that.

Edit: Since Jesse confirms that it works for him on Windows I'm tempted to say that you've got a non-Qt macro coming in and interfering. One thing you could do is a sanity check on what the compiler is actually seeing by getting VS to only produce the preprocessed source rather than do the compilation.

I haven't used VS in years but I think the option is \E maybe? [Edit: see the 2nd comment by Jesse, it should be /E] It may also be an explicit option now in the compiler properties which can be set for that source file. Can't remember where it puts the output either so you may need to hunt around for it a bit! If you get that going though you can check to see if the code looks right at the part that would correspond to line 154 in the original QObject header.

Troubadour
That is what it looks like in 4.5.0 under windows as well. Weird error and QThread is forward declared so if he is just creating the object there should not be any trouble.
Jesse
Good call on the preprocessor output. /E is correct: http://msdn.microsoft.com/en-us/library/3xkfswhy.aspx
Jesse
A: 

Thanks for all the help, solution: I had to include the Qt headers before all my other includes, and it now compiles.

Thanks again!

David Menard
Ah, so it *was* a non-Qt macro interfering. Good to know it's sorted although if you ever work out what header is the offending one I'd be interested to know.
Troubadour
It's the Stdafx.h header that interfering with Qt
David Menard