Results 1 to 8 of 8

Thread: doubts in signal and slots

  1. #1
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default doubts in signal and slots

    hi everyone,

    i had written an application wherein there will be single pushbutton and when i click the button and set of comments will appear. i had written as the following:

    Qt Code:
    1. class demo: public QWidget
    2. {
    3. public:
    4. demo(QWidget *parent=0,const char *name=0);
    5. ~demo();
    6. public slots:
    7. void change();
    8.  
    9. };
    10.  
    11. demo::demo(QWidget *parent,const char *name):
    12. QWidget(parent,name)
    13. {
    14. setMinimumSize(640,480);
    15.  
    16. QPushButton *clear= new QPushButton("clear",this,"clear");
    17. clear->setGeometry(10,10,50,20);
    18. clear->setFont(QFont("Times",12,QFont::Bold));
    19. connect(clear,SIGNAL(clicked()),this,SLOT(change()));
    20. }
    21.  
    22. demo::~demo()
    23. {
    24. }
    25.  
    26. void demo::change()
    27. {
    28. printf("\n change \n");
    29. }
    To copy to clipboard, switch view to plain text mode 

    this is all the code. but when i execute the program i get the error as

    "QObject::connect: No such slot QWidget::change()
    QObject::connect: (sender name: 'clear')
    QObject::connect: (receiver name: 'unnamed')
    ".
    but the same code works if i had declared the class in an header file and defined it in a cpp file and the main program in a main.cpp file.
    can anyone say me why is this not working ? is my coding correct ?

    thanks in advance,

    saravanan

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: doubts in signal and slots

    The Qt Meta Object Compiler needs the class definition in a header. It generates a source file from this header that will map the signals in your class to the slots you connect them to.

    Also, you have to add the Q_OBJECT macro to your class definition.

    I hope this helps.

    Regards,
    Marcel.

  3. #3
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: doubts in signal and slots

    hi,

    i tried that too. but it said
    "
    In function `ConnectWidget::~ConnectWidget()':
    undefined reference to `vtable for ConnectWidget'
    undefined reference to `vtable for ConnectWidget'
    .obj/release-shared-mt-emb-x86/connect.o: In function `ConnectWidget::~ConnectWidget()':
    undefined reference to `vtable for ConnectWidget'
    undefined reference to `vtable for ConnectWidget'
    .obj/release-shared-mt-emb-x86/connect.o: In function `ConnectWidget::~ConnectWidget()':
    collect2: ld returned 1 exit status
    make: *** [drawlines] Error 1".

    as you said the moc file is required i think. but i gave as follows also:
    "#include "drawlines.moc" "

    before main function. then also it said

    "error: drawlines.moc: No such file or directory".

  4. #4
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: doubts in signal and slots

    Use this
    Qt Code:
    1. class demo: public QWidget
    2. {
    3. Q_OBJECT
    4. public: demo(QWidget *parent=0,const char *name=0);
    5. ~demo();
    6. public slots:
    7. void change();
    8. };
    To copy to clipboard, switch view to plain text mode 

    Then rerun qmake again.
    qmake *.pro
    make
    Last edited by vermarajeev; 2nd April 2007 at 08:34.

  5. #5
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: doubts in signal and slots

    hi,

    i did that but still the same error .

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: doubts in signal and slots

    You must not include the generated moc in your source file.
    Implement everything normally( h + cpp ), and let qmake to its job.

    Also, take a look at your project file to see if everything is ok.

  7. #7
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: doubts in signal and slots

    Quote Originally Posted by sar_van81 View Post
    hi,

    i did that but still the same error .
    Follow this link

    http://www.qtcentre.org/forum/f-qt-p...?highlight=moc

  8. #8
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: doubts in signal and slots

    hi,

    thank you for that link. its working fine. actually i added the moc file before the main after the class definition.so thats why it did not work. now i added the moc file at the end of the cpp file.

    Also thank you to all them who gave suggestion.

Similar Threads

  1. Signal and slots
    By villy in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2007, 10:10
  2. Replies: 2
    Last Post: 17th May 2006, 21:01
  3. no such signal QListBox::currentChanged()
    By jopie bakker in forum Newbie
    Replies: 2
    Last Post: 2nd March 2006, 15:17
  4. Emiting signal, which NOT connected to any slots
    By krivenok in forum Qt Programming
    Replies: 7
    Last Post: 27th February 2006, 16:32
  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.