Results 1 to 15 of 15

Thread: Custom signal and slots

  1. #1
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Custom signal and slots

    Hello again,


    I am trying to make a simple application where when I click a button the count is displayed in the QLineEdit. But I cant get the signal going. I have read different documentations but for custom signal and slots everywhere I see I get a different account or method.

    I am posting my files. Please help me understand the concept

    click.h
    Qt Code:
    1. #ifndef CLICK_H
    2. #define CLICK_H
    3.  
    4. #include<QMainWindow>
    5. #include<QLineEdit>
    6. #include<QPushButton>
    7.  
    8. namespace Ui
    9. {
    10. class click_me;
    11. }
    12.  
    13.  
    14. class click_me:public QMainWindow
    15. {
    16. Q_OBJECT
    17. public:
    18. click_me(QWidget *Parent=0);
    19. signals:
    20. void click1(int);
    21. private slots:
    22. void disp();
    23. private:
    24. Ui::click_me *ui;
    25. private:
    26. QLineEdit *l1;
    27. };
    28. #endif
    To copy to clipboard, switch view to plain text mode 

    click.cpp
    Qt Code:
    1. #include"click.h"
    2. #include"ui_click.h"
    3.  
    4. int count=0;
    5.  
    6. click_me::click_me(QWidget *parent):QMainWindow(parent),ui(new Ui::click_me)
    7. {
    8. ui->setupUi(this);
    9.  
    10. //int count=0;
    11. connect(ui->p1,SIGNAL(click1(int)),ui->l1,SLOT(disp(int)));
    12. }
    13.  
    14. void click_me::disp(int)
    15. {
    16.  
    17. //emit click1(count);
    18. count = count +1;
    19.  
    20. QString s=QString::number(count);
    21.  
    22.  
    23. ui->l1->setText(s);
    24. }
    25.  
    26. //int click_me::click1(int co)
    27. //{
    28. //
    29. // emit click1();
    30. //return co;
    31.  
    32. //}
    To copy to clipboard, switch view to plain text mode 
    click.zip

    I am just unable to understand how do I pass parameters in the signal and even if I am passing one.... How do I go about declaring, defining and passing them.

    Clearing its a off day for me :-\
    Last edited by amitahire; 27th June 2012 at 11:17.

  2. #2
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Custom signal and slots

    This is completely wrong. You didn't get the idea of signals and slots. What you've done is:
    - you created signal and slot for class click_me not for QLineEdit and QPushButton so you cannot connect them as SIG & SLOT for instances of those classes
    This is wrong:
    connect(ui->p1,SIGNAL(click1(int)),ui->l1,SLOT(disp(int)));

    I know what you want to do
    You wanna click the button and invoke slot which put value into QLineEdit. You can do it this way:
    - use one of Qt's original signals for QPushButton - clicked() and connect to your own slot disp(). clicked() doesn't get any value so if you wanna have signal with value you will have to create new class for button derived from QPushButton but i think that it isn't necessary in this case.

    It has to look like that:
    connect(ui->p1, SIGNAL(clicked()), this, SLOT(disp()));

    You don't need any value passed to signal because you don't used it in disp() method. QPushButton's signal is invoked automatically by QPushButton when you click on instance of this class or derived class.

    And your own signals are invoked by using: emit name_of_signal();

  3. #3
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Custom signal and slots

    So in order to pass parameter in my custom signal I need to create a new class from QPushButton.

    Well you have got exactly what I am trying to achieve. But I am still confused as to if I dont pass any value to disp() how is it gonna setText in the LineEdit?? Sorry still confused.

    Quote Originally Posted by code_err View Post
    This is completely wrong. You didn't get the idea of signals and slots. What you've done is:
    - you created signal and slot for class click_me not for QLineEdit and QPushButton so you cannot connect them as SIG & SLOT for instances of those classes
    This is wrong:
    connect(ui->p1,SIGNAL(click1(int)),ui->l1,SLOT(disp(int)));

    I know what you want to do
    You wanna click the button and invoke slot which put value into QLineEdit. You can do it this way:
    - use one of Qt's original signals for QPushButton - clicked() and connect to your own slot disp(). clicked() doesn't get any value so if you wanna have signal with value you will have to create new class for button derived from QPushButton but i think that it isn't necessary in this case.

    It has to look like that:
    connect(ui->p1, SIGNAL(clicked()), this, SLOT(disp()));

    You don't need any value passed to signal because you don't used it in disp() method. QPushButton's signal is invoked automatically by QPushButton when you click on instance of this class or derived class.

    And your own signals are invoked by using: emit name_of_signal();

  4. #4
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: Custom signal and slots

    Why you're declaring pushbutton and lineEdit in header file if you're dragging it into your ui file. Directly use it. Here is the code see this:------>

    Qt Code:
    1. #ifndef CLICK_H
    2. #define CLICK_H
    3.  
    4. #include<QMainWindow>
    5. #include<QLineEdit>
    6. #include<QPushButton>
    7.  
    8. namespace Ui
    9. {
    10. class click_me;
    11. }
    12.  
    13.  
    14. class click_me:public QMainWindow
    15. {
    16. Q_OBJECT
    17. public:
    18. click_me(QWidget *Parent=0);
    19. private slots:
    20. void disp();
    21. private:
    22. Ui::click_me *ui;
    23. int count;
    24. };
    25. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include"click.h"
    2. #include"ui_click.h"
    3.  
    4.  
    5. click_me::click_me(QWidget *parent):QMainWindow(parent),ui(new Ui::click_me)
    6. {
    7.  
    8. count=0;
    9. ui->setupUi(this);
    10.  
    11. //int count=0;
    12. connect(ui->p1,SIGNAL(clicked()),this,SLOT(disp()));
    13. }
    14.  
    15. void click_me::disp()
    16. {
    17.  
    18. //emit click1(count);
    19. count = count +1;
    20.  
    21. QString s=QString::number(count);
    22.  
    23.  
    24. ui->l1->setText(s);
    25. }
    To copy to clipboard, switch view to plain text mode 

    Read Documentation properly.

    If you're declaring it in the class then why you've to pass. Just directly use it. Clear some C++ concept.

  5. #5
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Custom signal and slots

    Well you have got exactly what I am trying to achieve. But I am still confused as to if I dont pass any value to disp() how is it gonna setText in the LineEdit?? Sorry still confused.
    I see, you have problems with basic c++, variable count has to be declared before you can use it so you have to add this variable to your class definition and initialize it in constructor. Then method disp() should work.

    And also this:
    void click_me::disp(int)
    In declaration there was no int argument, and even if it was you always have to give name to passed value in definition of method.

  6. #6
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom signal and slots

    hey, that i have declared count in the header file. Why dont you see that?????

  7. #7
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Custom signal and slots

    Sorry, you made count global variable (but it's not declaration in header), didn't see it before.

  8. #8
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Custom signal and slots

    Quote Originally Posted by amitahire View Post
    Hello again,


    I am trying to make a simple application where when I click a button the count is displayed in the QLineEdit. But I cant get the signal going. I have read different documentations but for custom signal and slots everywhere I see I get a different account or method.

    I am posting my files. Please help me understand the concept

    click.h
    Qt Code:
    1. #ifndef CLICK_H
    2. #define CLICK_H
    3.  
    4. #include<QMainWindow>
    5. #include<QLineEdit>
    6. #include<QPushButton>
    7.  
    8. namespace Ui
    9. {
    10. class click_me;
    11. }
    12.  
    13.  
    14. class click_me:public QMainWindow
    15. {
    16. Q_OBJECT
    17. public:
    18. click_me(QWidget *Parent=0);
    19. signals:
    20. void click1(int);
    21. private slots:
    22. void disp();
    23. private:
    24. Ui::click_me *ui;
    25. private:
    26. QLineEdit *l1;
    27. };
    28. #endif
    To copy to clipboard, switch view to plain text mode 

    click.cpp
    Qt Code:
    1. #include"click.h"
    2. #include"ui_click.h"
    3.  
    4. int count=0;
    5.  
    6. click_me::click_me(QWidget *parent):QMainWindow(parent),ui(new Ui::click_me)
    7. {
    8. ui->setupUi(this);
    9.  
    10. //int count=0;
    11. connect(ui->p1,SIGNAL(click1(int)),ui->l1,SLOT(disp(int)));
    12. }
    13.  
    14. void click_me::disp(int)
    15. {
    16.  
    17. //emit click1(count);
    18. count = count +1;
    19.  
    20. QString s=QString::number(count);
    21.  
    22.  
    23. ui->l1->setText(s);
    24. }
    25.  
    26. //int click_me::click1(int co)
    27. //{
    28. //
    29. // emit click1();
    30. //return co;
    31.  
    32. //}
    To copy to clipboard, switch view to plain text mode 
    click.zip

    I am just unable to understand how do I pass parameters in the signal and even if I am passing one.... How do I go about declaring, defining and passing them.

    Clearing its a off day for me :-\
    Hello guys -- thanks for your reply but I think you didnt get what I was asking for and also my code last time was horrible.

    Now check this is out

    click_init.cpp
    Qt Code:
    1. #include<QtGui>
    2. #include"click.h"
    3.  
    4. int main(int argc,char *argv[])
    5. {
    6. QApplication a(argc,argv);
    7. click_me w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    click.h
    Qt Code:
    1. #ifndef CLICK_H
    2. #define CLICK_H
    3.  
    4.  
    5.  
    6. #include<QMainWindow>
    7. #include<QLineEdit>
    8. #include<QPushButton>
    9.  
    10. namespace Ui
    11. {
    12. class click_me;
    13. }
    14.  
    15.  
    16. class click_me:public QMainWindow
    17. {
    18. Q_OBJECT
    19. public:
    20. click_me(QWidget *parent=0);
    21. //{ count = 0; }
    22.  
    23. int value() const { return count;}
    24. signals:
    25. void click1(int val);
    26. private slots:
    27. void disp(int val);
    28. private:
    29. Ui::click_me *ui;
    30. private:
    31. //QPushButton *p1;
    32. //QLineEdit *l1;
    33. int count;
    34.  
    35. };
    36.  
    37. #endif // CLICK_H
    To copy to clipboard, switch view to plain text mode 
    click.cpp
    Qt Code:
    1. #include"click.h"
    2. #include"ui_click.h"
    3.  
    4. //int count=0;
    5.  
    6. click_me::click_me(QWidget *parent):QMainWindow(parent),ui(new Ui::click_me)
    7. {
    8. ui->setupUi(this);
    9.  
    10. count=0;
    11. connect(this,SIGNAL(click1(int)),this,SLOT(disp(int)));
    12. ui->l1->setText("0");
    13.  
    14. }
    15.  
    16. void click_me::disp(int val)
    17. {
    18. //if (val != count) {
    19. count = val+1;
    20. QString s=QString::number(count);
    21. ui->l1->setText(s);
    22. emit click1(count);
    23. //}
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    Now this works in the Creatoe but the slots just doesnt work. I am trying to increment and keep the updated value in textbox.

    Please can you check whether the signal and slots are defined properly?
    And whether my logic is correct, if not please explain it to me. Thanks

  9. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom signal and slots

    But this is "never ending loop". In slot disp You emit signal click1 which is connected to slot disp which is emit......

  10. #10
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Custom signal and slots

    So where do I emit click1? Some signal of QPushButton? like click() or something?

    EDIT - I can call QPushButton::click() and call emit from there but it gives me error saying " mulltiple definition" and ofcourse I cannot connect click() and disp(int) because of the parameter mismatch. So how do I go about it?
    Last edited by amitahire; 2nd July 2012 at 08:54.

  11. #11
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom signal and slots

    Hey amitahire,

    why you are creating signal click1(int) as there is already an inbuilt signal in qt.
    remove click1(int val) signal from header file and in connect() method write this,
    connect(ui->p1,SIGNAL(clicked()),this,SLOT(disp())); see my code.
    why you want to send the argument if you already get it without sending it.
    And also, if you want to emit your own signal then create your own class inheriting QPushButton class and there emit your signal.

  12. #12
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Custom signal and slots

    Ok if I dont go passing around arguments in my signal and slots.How do I go about updating the count variable?

    And in order to emit my own signal - I need to create my own class inherting QPushButton. Thanks for this.


    Added after 5 minutes:


    Quote Originally Posted by sonulohani View Post
    Hey amitahire,

    why you are creating signal click1(int) as there is already an inbuilt signal in qt.
    remove click1(int val) signal from header file and in connect() method write this,
    connect(ui->p1,SIGNAL(clicked()),this,SLOT(disp())); see my code.
    why you want to send the argument if you already get it without sending it.
    And also, if you want to emit your own signal then create your own class inheriting QPushButton class and there emit your signal.
    You think this will work?? Its not. Any suggestions?
    Qt Code:
    1. #include"click.h"
    2. #include"ui_click.h"
    3.  
    4. //int count=0;
    5.  
    6. click_me::click_me(QWidget *parent):QMainWindow(parent),ui(new Ui::click_me)
    7. {
    8. ui->setupUi(this);
    9.  
    10. count=0;
    11. connect(this,SIGNAL(click()),this,SLOT(disp()));
    12. ui->l1->setText("0");
    13.  
    14. }
    15.  
    16. void click_me::disp()
    17. {
    18. //if (val != count) {
    19. count = count +1;
    20.  
    21. QString s=QString::number(count);
    22. ui->l1->setText(s);
    23. //emit click1(count);
    24. //}
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by amitahire; 2nd July 2012 at 09:14.

  13. #13
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: Custom signal and slots

    hey, what are you trying to do??? Please specify.... What output at last do you want???? I will show you.....

  14. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom signal and slots

    @amitahire
    I am just unable to understand how do I pass parameters in the signal and even if I am passing one.... How do I go about declaring, defining and passing them.
    Your problem is not a Qt problem, but understanding of what a function call is.
    We can't help you with that - you will have to learn C++ first.

    There is no need to use signal and slot inside the same object - it CAN be done, but why do it?
    When you connect a signal to a slot, you are performing a call back.
    This makes sense mostly in cases where the caller doesn't know about the called object.
    But when you are in your own class, you can just directly call your slot:
    Qt Code:
    1. void click_me::disp()
    2. {
    3. //if (val != count) {
    4. count = count +1;
    5.  
    6. QString s=QString::number(count);
    7. ui->l1->setText(s);
    8. //emit click1(count); //this will call your slot, so why emit a signal if you can just do:
    9. disp();
    10. //}
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    Leave Qt for now, its too much to take on when you are not yet familiar with C/C++ and general programming concepts.
    Start with the basics, once you know them, Qt will make much more sense to you.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  15. #15
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom signal and slots

    Dont leave it, but you've to clear some of your concept in Qt.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

Similar Threads

  1. Signal/Slots, Where can i use it?
    By Tio in forum Newbie
    Replies: 2
    Last Post: 25th May 2010, 01:36
  2. Signal and Slots
    By waynew in forum Newbie
    Replies: 3
    Last Post: 20th November 2009, 03:50
  3. signal and slots
    By vermarajeev in forum Qt Programming
    Replies: 4
    Last Post: 16th October 2007, 08:31
  4. Signal and slots
    By villy in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2007, 10:10
  5. Problem with Signal and Slots
    By Kapil in forum Installation and Deployment
    Replies: 2
    Last Post: 10th February 2006, 08:51

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.