Results 1 to 5 of 5

Thread: trouble linking to public slot

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default trouble linking to public slot

    I am having trouble calling a slot directly from subclass of QListWidget. It fails to link claiming an undefined reference to MyClass::MySlot. If instead of calling it, I just connect to slot and emit the appropriate signal everything works fine. It seems to me that it's probably a compiler/qmake/makefile issue but I'm not sure. Below is a rough approximation of the issue I'm having. I have omitted all the #include for the sake of brevity.

    I have a subclass of QObject that looks roughly like

    Qt Code:
    1. class MyClass : public QObject {
    2.  
    3. Q_OBJECT
    4.  
    5. public:
    6. MyClass(QObject* parent = NULL) : QObject(parent) { }
    7. void MyFunc() {
    8. // do some work
    9. }
    10.  
    11. public slots:
    12. void MySlot() {
    13. // do some work
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    And...

    Qt Code:
    1. class MyListWidget : public QListWidget {
    2.  
    3. Q_OBJECT
    4.  
    5. private:
    6. MyClass* foo;
    7.  
    8. public:
    9. MyListWidget(QWidget* parent = NULL) : QListWidget(parent) { }
    10.  
    11. public slots:
    12. void bar() {
    13. // won't compile, linker fails - undefined reference to 'MyClass::MySlot'
    14. foo->MySlot();
    15. }
    16.  
    17. void baz() {
    18. // this compiles and works fine
    19. QObject::connect(this, SIGNAL(callSlot), foo, SLOT(MySlot()));
    20. emit callSlot();
    21. QObject::disconnect(this, SIGNAL(callSlot), foo, SLOT(MySlot()));
    22.  
    23. // also works
    24. foo->MyFunc();
    25. }
    26.  
    27. signals:
    28. void callSlot();
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: trouble linking to public slot

    could you provide a more complete sample. it is not clear what you include and where.

  3. #3

    Default Re: trouble linking to public slot

    MyListWidget does in fact include the header for MyClass if that's what you were getting at. Besides, it's a linker problem, not a compiler problem so I don't think #includes are the issue. I can provide more detail if you still think it will help.

  4. #4
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: trouble linking to public slot

    check out this code. it compiles fine and gives linker error. and of course, there is an error in the code
    Qt Code:
    1. #include <QtCore>
    2.  
    3. class A
    4. {
    5. public:
    6. void testMethod();
    7. };
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QCoreApplication app(argc, argv);
    12.  
    13. A a;
    14. a.testMethod();
    15.  
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

  5. #5

    Default Re: trouble linking to public slot

    I'm not sure I understand what point you are making. Your example won't link because you have not written the implementation of testMethod() anywhere, correct? The function that refuses won't link in mine has a body so that's not the problem. I can even run the function in question using signals, I just won't link when I try and call it directly.


    Added after 10 minutes:


    fixed it. problem with the .pro file.
    Last edited by jiggersplat; 23rd February 2012 at 14:53.

Similar Threads

  1. Replies: 2
    Last Post: 2nd November 2011, 03:08
  2. Replies: 0
    Last Post: 13th October 2010, 20:28
  3. Replies: 5
    Last Post: 24th May 2010, 21:20
  4. Trouble linking static library
    By russdot in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2009, 10:56
  5. Why Qt can not find a public slot?
    By Alex Snet in forum Qt Programming
    Replies: 13
    Last Post: 13th April 2009, 08:31

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.