Results 1 to 7 of 7

Thread: Where does QMetaObject "live"? A .dll, a .lib, a moose in the Ukraine, etc.?

  1. #1
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Where does QMetaObject "live"? A .dll, a .lib, a moose in the Ukraine, etc.?

    I'm trying to follow some examples to get signal and slot connections to work in Visual Studio 2013. Here's the basic code:

    Qt Code:
    1. #include <QtCore/QObject>
    2.  
    3. class my_derived_object : public QObject
    4. {
    5. private:
    6. // http://qt-project.org/doc/qt-4.8/qobject.htm#Q_OBJECT
    7. // "The Q_OBJECT macro must appear in the private section of a class definition
    8. // that declares its own signals and slots or that uses other services provided
    9. // by Qt's meta-object system."
    10. Q_OBJECT;
    11.  
    12. public:
    13. // http://qt-project.org/doc/qt-4.8/qobject.html#Q_INVOKABLE
    14. // "Apply this macro to definitions of member functions to allow them to be
    15. // invoked via the meta-object system. The macro is written before the return
    16. // type..."
    17. Q_INVOKABLE int say_hi(int number)
    18. {
    19. cout << "hello with number '" << number << "'" << endl;
    20.  
    21. return (number * 2);
    22. }
    23. };
    24.  
    25. int main(int argc, char **argv)
    26. {
    27. my_derived_object my_obj;
    28.  
    29. int method_index = my_obj.metaObject()->indexOfMethod("say_hi(int)");
    30. cout << "method index = '" << method_index << "'" << endl;
    31.  
    32. return 0;
    33. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that "Q_OBJECT" macro. Having that in my code produces a link error like so:
    Qt Code:
    1. LNK2019: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall my_derived_object ::metaObject(void)const " (?metaObject@my_derived_object @@UBEPBUQMetaObject@@XZ) referenced in function _main *error location has personal folder locations and is therefore redacted*
    To copy to clipboard, switch view to plain text mode 

    But that macro, as per the documentation referenced in the comment above it, is required to specify my own slot and signal functions, which in turn are required for anything using SIGNAL(...) or SLOT(...), such as a call to QObject::connect(...). This code worked just dandy in QtCreator, but not in Visual Studio.

    I tried a brute force approach of linking every possible Qt .lib file into my program and including all possible Qt .dll files into my project, but that did nothing. I still had the linker error.

    So where does QMetaObject "live"? I've tried some "moc"-related tutorials that tried to generate certain files, but I didn't get anywhere.

  2. #2
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Where does QMetaObject "live"? A .dll, a .lib, a moose in the Ukraine, etc.?

    You don't need a ";" after the Q_OBJECT macro.

    So where does QMetaObject "live"?
    QMetaObject is a class of the Qt framework. It is part of the core module (and thus "lives" in the corresponding library, eg. libQt5Core.so).

    It looks like as if moc isn't configured probably. Do you get, compile and link moc_*.cpp-files which should be generated by moc?

  3. #3
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Where does QMetaObject "live"? A .dll, a .lib, a moose in the Ukraine, etc.?

    What is "libQt5Core.so"? I don't see any files in the /lib folder that end in ".so".

    What is "moc", anyway? Whatever it is, I haven't used it before, so it probably isn't. What are "moc_*.cpp-files"?

    I don't think that "moc" is part of my build routine. I tried a tutorial (http://ldmartin68.com/QTSetup4VSNET.html), but that didn't work. I got errors saying that "$(QTDIR)\bin\moc MyHeaderFile.h -o tmp\moc\moc_MyHeaderFile.cpp" didn't work (the error wasn't particularly explicit; it just said that the command didn't work).

    I'm trying to go through this (http://qt.developpez.com/doc/4.7/moc/) but I'm not understanding it.

    All the tutorials around moc seem to assume (1) that QtCreator is the IDE, (2) linux is the OS, or (3) both (1) and (2). Can you point me at some tutorial that shows how to set up "moc", whatever it is?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Where does QMetaObject "live"? A .dll, a .lib, a moose in the Ukraine, etc.?

    Since you put the Q_OBJECT code in the CPP file and not in a separate header it is not listed in HEADERS and therefore does not get the automated Metaobject compiler (moc) handling from qmake. You need to trigger qmake to generate moc pre-processing in the Makefile, and include the moc generated C++ source, an the easiest way to do that with a one-file-wonder is:
    Qt Code:
    1. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    at the bottom of the main.cpp file and re-run qmake.

    Normally the header for the QObject class would be in separate file listed in HEADERS. Then qmake ensures that moc is run, the resulting C++ code is compiled, and the result included in your linker run.

  5. #5
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Where does QMetaObject "live"? A .dll, a .lib, a moose in the Ukraine, etc.?

    I get the compile error
    Qt Code:
    1. error C1083: Cannot open include file: 'main.moc': No such file or directory
    To copy to clipboard, switch view to plain text mode 

    Remember that I am running Visual Studio. That command works in QtCreator, but not Visual Studio. "main.moc" doesn't exist.

    Any other ideas on how to initialize this "moc" thing?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Where does QMetaObject "live"? A .dll, a .lib, a moose in the Ukraine, etc.?

    Try putting the class declaration into a header.

    Do you use qmake to generate the VS project file or do you use the Qt plugin?

    Cheers,
    _

  7. #7
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Where does QMetaObject "live"? A .dll, a .lib, a moose in the Ukraine, etc.?

    I figured it out in this thread: http://www.qtcentre.org/threads/5923...169#post263169

    I was using neither qmake nor the Qt plugin (I'm using VS express, and the plugin download description says that it doesn't work in express). I used VS's build system and had to add a custom build step that executed moc.exe before the first of the build steps.

Similar Threads

  1. Replies: 15
    Last Post: 26th May 2014, 15:54
  2. Replies: 1
    Last Post: 3rd December 2013, 02:19
  3. Replies: 1
    Last Post: 7th April 2010, 21:46
  4. Replies: 3
    Last Post: 15th February 2010, 17:27
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.