Results 1 to 9 of 9

Thread: problem with slots

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default problem with slots

    hi
    in the following code the signal is not connected to slot.

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. class push : public QPushButton
    5. {
    6.  
    7. public slots:
    8. void hi();
    9.  
    10. public:
    11. push(QWidget *parent=0);
    12. };
    13.  
    14.  
    15. push::push(QWidget *parent) :
    16. QPushButton(parent)
    17. {
    18. connect(this, SIGNAL(clicked()), this, SLOT(hi()));
    19. }
    20.  
    21. void push::hi()
    22. {
    23. qDebug()<<"hioo";
    24. }
    25.  
    26. int main(int argc, char *argv[])
    27. {
    28. QApplication app(argc, argv);
    29.  
    30. push W;
    31. W.show();
    32.  
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 

    this code compiles and executes but gives the warning
    Object::connect: No such slot QPushButton::hi()

    where did the code wnt wrong

  2. #2
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with slots

    Hey babu198649,
    You are using the clicked() signal to connect to the slot hi(), but where is the button which after clicking will emit the signal ????

  3. #3
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem with slots

    How did u declared hi() slot?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with slots

    The Q_OBJECT macro needs to be present in every class declaring a signal or a slot and moc has to be run on the header file.

  5. The following user says thank you to wysota for this useful post:

    babu198649 (31st March 2008)

  6. #5
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with slots

    Ohh ya I overlooked the fault which Wysota has said.

    So Yeap as Wysota has mentioned you have to declare the Q_OBJECT macro in your class to use the signal !!!!

  7. #6
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: problem with slots

    thanks to all for u r reply

    You are using the clicked() signal to connect to the slot hi(), but where is the button which after clicking will emit the signal ????
    i have subclassed the QPushButton and in the connect function i am using this pointer .ie. the same pushbutton signal is connected to the slot.

    The Q_OBJECT macro needs to be present in every class declaring a signal or a slot and moc has to be run on the header file.
    it worked after declaring the class in the header file(with Q_OBJECT declaration in it) and defining the class in .cpp file.



    but if i do both class declaration and definition in same file ,and even after including the Q_OBJECT macro i am getting the following error.

    Qt Code:
    1. release/main.o(.text+0x1a): In function `push::push(QWidget*)':
    2. make[1]: Leaving directory `/home/bala/workspace/gg'
    3. : undefined reference to `vtable for push'
    4. release/main.o(.text+0x22): In function `push::push(QWidget*)':
    5. : undefined reference to `vtable for push'
    6. release/main.o(.text+0x8a): In function `push::push(QWidget*)':
    7. : undefined reference to `vtable for push'
    8. release/main.o(.text+0x92): In function `push::push(QWidget*)':
    9. : undefined reference to `vtable for push'
    10. release/main.o(.text+0x3dc): In function `main':
    11. : undefined reference to `vtable for push'
    12. release/main.o(.text+0x3e5): more undefined references to `vtable for push' follow
    13. collect2: ld returned 1 exit status
    14. make[1]: *** [gg] Error 1
    15. make: *** [release] Error 2
    To copy to clipboard, switch view to plain text mode 

    i have defined the class only after the declaration , even then why do i need two files(header and source file, when there is a single class).

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: problem with slots

    Add
    Qt Code:
    1. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    at the end of main.cpp or put the class declaration in a separate header file. Then, re-run qmake.
    J-P Nurmi

  9. The following user says thank you to jpn for this useful post:

    babu198649 (31st March 2008)

  10. #8
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: problem with slots

    hi
    its working after including #include "main.moc" at the end.

    if i use both header and source file for a class ,then do i dont have to bother about the moc issues.

  11. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: problem with slots

    Quote Originally Posted by babu198649 View Post
    if i use both header and source file for a class ,then do i dont have to bother about the moc issues.
    You will still have to re-run qmake after adding/removing Q_OBJECT macro to update necessary rules in the makefile(s). But you're right, you don't have to include the moc file by hand provided that the header is properly listed in the .pro file.
    J-P Nurmi

Similar Threads

  1. Signals And Slots problem
    By ldiamond in forum Newbie
    Replies: 7
    Last Post: 23rd March 2008, 00:11
  2. Memory Problem with SIGNALS and SLOTS
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2007, 20:39
  3. Problem with signals and slots
    By conexion2000 in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2006, 10:20
  4. Problem with Signals and Slots
    By Kapil in forum Newbie
    Replies: 11
    Last Post: 15th February 2006, 11:35
  5. Problem with Signal and Slots
    By Kapil in forum Installation and Deployment
    Replies: 2
    Last Post: 10th February 2006, 08:51

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.