Results 1 to 7 of 7

Thread: Signals and Slots - Moc QObject Problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Signals and Slots - Moc QObject Problem

    Hi all,

    I was wondering would someone be able to help me with a slight problem. I am trying to use signals and slots within my program. From QT, we know that “all classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject”. That’s fine. So the following is an example I’m trying to implement, taken directly from http://doc.trolltech.com/4.6/signalsandslots.html

    Counter.h

    #include <QObject>

    class Counter : public QObject
    {
    Q_OBJECT

    public:
    Counter() { m_value = 0; }

    int value() const { return m_value; }

    public slots:
    void setValue(int value);

    signals:
    void valueChanged(int newValue);

    private:
    int m_value;
    };

    Counter.cpp

    #include “Counter.h”

    void Counter::setValue(int value)
    {
    if (value != m_value) {
    m_value = value;
    emit valueChanged(value);
    }
    }

    Then when trying to instantiate the class:

    Counter a, b;
    QObject::connect(&a, SIGNAL(valueChanged(int)),
    &b, SLOT(setValue(int)));

    a.setValue(12); // a.value() == 12, b.value() == 12
    b.setValue(48); // a.value() == 12, b.value() == 48

    I get the following linking errors:

    error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Counter::metaObject(void)const " (?metaObject@Counter@@UBEPBUQMetaObject@@XZ)
    error LNK2001: unresolved external symbol "public: virtual void * __thiscall Counter::qt_metacast(char const *)" (?qt_metacast@Counter@@UAEPAXPBD@Z)
    error LNK2001: unresolved external symbol "public: virtual int __thiscall Counter::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Counter@@UAEHW4Call@QMetaObject@@HPA PAX@Z)
    error LNK2001: unresolved external symbol "protected: void __thiscall Counter::valueChanged(int)" (?valueChanged@Counter@@IAEXH@Z)

    From looking at forums (http://lists.trolltech.com/qt-intere.../msg00374.html), it seems that we need to moc the source – “You need to call "moc" on your Counter header file and make sure the compiler includes the output of moc into the binary. How you'd do that depends on the compiler being used and if you use custom Makefiles, qmake-projects or something else“.

    As far as I can see, I have a moc file and it is being updated. Would someone please help me with this probelm and let us know how to overcome it.

    Thank you for your time

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals and Slots - Moc QObject Problem

    I made sure: Your code is fine. Works for me:

    Qt Code:
    1. signalslot.pro:
    2.  
    3. QT += core
    4. TARGET = signalslot
    5. TEMPLATE = app
    6. HEADERS += Counter.h
    7. SOURCES += main.cpp
    8.  
    9. counter.h:
    10.  
    11. #ifndef COUNTER_H
    12. #define COUNTER_H
    13.  
    14. #include <QObject>
    15. #include <QDebug>
    16.  
    17. class Counter : public QObject
    18. { Q_OBJECT
    19. public:
    20. Counter() { m_value = 0; }
    21.  
    22. int value() const { return m_value; }
    23.  
    24. public slots:
    25. void setValue(int value)
    26. {
    27. if (value != m_value) {
    28. m_value = value;
    29. emit valueChanged(value);
    30. qDebug() << "ValueChanged: " << value;
    31. }
    32. }
    33.  
    34. signals:
    35. void valueChanged(int newValue);
    36.  
    37. private:
    38. int m_value;
    39. };
    40.  
    41.  
    42. #endif // COUNTER_H
    43.  
    44. main.cpp:
    45.  
    46. #include <QApplication>
    47. #include "Counter.h"
    48. #include <QDebug>
    49.  
    50. int main(int argc, char *argv[])
    51. {
    52. QApplication app(argc, argv);
    53.  
    54. Counter a, b;
    55. QObject::connect(&a, SIGNAL(valueChanged(int)),&b, SLOT(setValue(int)));
    56.  
    57. a.setValue(12); // a.value() == 12, b.value() == 12
    58. b.setValue(48); // a.value() == 12, b.value() == 48
    59.  
    60. return 0;//a.exec();
    61. }
    To copy to clipboard, switch view to plain text mode 
    Output:
    ValueChanged: 12
    ValueChanged: 12
    ValueChanged: 48

    Try deleting the makefiles. clean the project.

    What environment are you using? QtCreator?

    Johannes

  3. #3
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Signals and Slots - Moc QObject Problem

    I can suggest you to put your connections at the end of the scope

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals and Slots - Moc QObject Problem

    ?

    Connections at the end of the scope, might be a useful advice inside a constructor. But if we would establish the connection in this example after the calls to setValue.. the test program would not work as expected.

    However that might be, it won't solve the original problem..

    Joh

  5. #5
    Join Date
    Mar 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals and Slots - Moc QObject Problem

    Thank you for your rapid response.

    The environment question is a little tricky. We are working on a project using the H3D API (for haptics). We have QT integrated within H3D for the GUI part and running the program from Visual Studio 2008. Our GUI requirements are quite simple so therefore we are not using QTCreator or anything similar. All we are doing is deriving from a H3D window class and using some simple windows and buttons. I'm not too sure if you have experience with H3D but it could be the way we have QT integrated within H3D.

    Is there any informatin I can provide you with that might help you understand where the probelm lies??

  6. #6
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals and Slots - Moc QObject Problem

    Hi!

    I am out of my depth here. But I would guess your signal slot problem is probably not a question of the H3D API. Or does the problem only occur in a test project, once you include H3d?

    Otherwise I think, its a problem of your Qt integration into Visual Studio.

    Good luck!

    Johannes

  7. #7
    Join Date
    Feb 2007
    Posts
    49
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals and Slots - Moc QObject Problem

    Is moc'ed .obj file added to a linker? In this case it wiil be moc_counter.obj.

Similar Threads

  1. Problem with signals & slots
    By Palmik in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2009, 22:45
  2. Signals And Slots problem
    By ldiamond in forum Newbie
    Replies: 7
    Last Post: 23rd March 2008, 00:11
  3. Problem using SIGNALS/SLOTS
    By JimDaniel in forum Qt Programming
    Replies: 5
    Last Post: 10th September 2007, 04:59
  4. Problem with signals and slots
    By conexion2000 in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2006, 10:20
  5. Problem with Signals and Slots
    By Kapil in forum Newbie
    Replies: 11
    Last Post: 15th February 2006, 11:35

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
  •  
Qt is a trademark of The Qt Company.