Results 1 to 2 of 2

Thread: signals and slots with little c++ knowledge

  1. #1
    Join Date
    Jun 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default signals and slots with little c++ knowledge

    hello, i am new to QT and i am trying to learn QT and its features.But as a C guy i am facing a bit difficult in understanding signals and slots except this feature the Docs provided by QT is more than enough for me

    i use code blocks along with QT libraries to do the magic with C and it works great!.

    i need help from you guys.can you please tell me how to create a new slots and how to manipulate data with it.i have some very small basic knowledge on c++ so i can understand little bit.

    for example is this correct?
    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QPushButton clickme("ClickeME");
    5. clickme.resize(75, 30);
    6. clickme.setFont(QFont("Times", 18, QFont::Bold));
    7. QObject::connect(&clickme, SIGNAL(clicked()), &app, SLOT(updatenewvalues()));
    8. clickme.show();
    9. return app.exec();
    10. }
    11.  
    12. void updatenewvalues()
    13. {
    14. /* some code here */
    15. }
    To copy to clipboard, switch view to plain text mode 

    what i want this program to do is initially the value in clickme is "clickme" no when i press clickme i want the value of "clickme" to change to "dontclickme"

    i have read the Docs from here
    http://doc.trolltech.com/4.7/signalsandslots.html
    but no help!
    Last edited by wenn32; 18th October 2010 at 03:09.

  2. #2
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signals and slots with little c++ knowledge

    Here's your program done the c++ way:

    Qt Code:
    1. #ifndef PUSHBUTTON_H
    2. #define PUSHBUTTON_H
    3. #include <QPushButton>
    4. #include <QString>
    5. /*Here, we subclass QPushButton so that we can define a new slot:
    6.   updatenewvalues().
    7.   It is worth noting that we could have subclassed QApplication
    8.   to add the new slot also. Qt is awesome like that!
    9.   For brevity's sake, the class is defined inline in a header. This is
    10.   generally a bad idea!*/
    11.  
    12. class PushButton: public QPushButton
    13. {
    14. Q_OBJECT
    15. public:
    16. PushButton(QWidget* parent = 0): QPushButton(parent){}
    17. PushButton(const QString& text, QWidget* parent = 0) : QPushButton(text, parent){}
    18. public slots:
    19. void updatenewvalues()
    20. {
    21. if(this->text() == "Don't click me!")
    22. {
    23. setText("Click me!");
    24. return;
    25. }
    26. setText("Don't click me!");
    27. }
    28. };
    29. #endif // PUSHBUTTON_H
    To copy to clipboard, switch view to plain text mode 

    Then:

    Qt Code:
    1. /*now we employ our new class*/
    2. int main(int argc, char* argv[])
    3. {
    4. QApplication app(argc, argv);
    5. PushButton clickme("Click me!");
    6. clickme.resize(200, 30);
    7. clickme.setFont(QFont("Times", 18, QFont::Bold));
    8. QObject::connect(&clickme, SIGNAL(clicked()), &clickme, SLOT(updatenewvalues()));
    9. clickme.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    I suggest some reading to help with your learning c++. You can find several good books on Qt with c++ at Amazon.com.

Similar Threads

  1. Signals and Slots
    By Maluko_Da_Tola in forum Newbie
    Replies: 3
    Last Post: 29th July 2010, 23:57
  2. regarding signals/slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 09:32
  3. Signals and slots
    By sylvarant in forum Newbie
    Replies: 4
    Last Post: 11th September 2007, 15:48
  4. help with signals and slots
    By superutsav in forum Qt Programming
    Replies: 3
    Last Post: 4th May 2006, 12:49
  5. Signals and Slots in dll
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2006, 08:12

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.