Results 1 to 6 of 6

Thread: How to use a slot in Qt like a function in C++

  1. #1
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default How to use a slot in Qt like a function in C++

    I have a small exercise purely in C++ with a slot this way:

    Calculator.h:

    Qt Code:
    1. #ifndef CALCULATOR_H
    2. #define CALCULATOR_H
    3.  
    4. #include <QDialog>
    5.  
    6. class QLineEdit;
    7.  
    8. class Calculator : public QDialog
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit Calculator(QWidget *parent = 0);
    14. private slots:
    15. void myslot(QString);
    16. private:
    17. QLineEdit* lineEdit;
    18. QPushButton* oneButton;
    19. QPushButton* twoButton;
    20. QPushButton* quitButton;
    21. };
    22.  
    23. #endif // CALCULATOR_H
    To copy to clipboard, switch view to plain text mode 


    Calculator.cpp is this:

    Qt Code:
    1. #include "calculator.h"
    2. #include <QPushButton>
    3. #include <QLineEdit>
    4. #include <QVBoxLayout>
    5.  
    6. Calculator::Calculator(QWidget *parent)
    7. : QDialog(parent)
    8. {
    9. lineEdit = new QLineEdit;
    10. oneButton = new QPushButton("1");
    11. twoButton = new QPushButton("2");
    12. quitButton = new QPushButton("Quit");
    13.  
    14. QVBoxLayout* vbox = new QVBoxLayout;
    15. vbox -> addWidget (lineEdit);
    16. vbox -> addWidget (oneButton);
    17. vbox -> addWidget (twoButton);
    18. vbox -> addWidget (quitButton);
    19.  
    20. setLayout(vbox);
    21.  
    22. QString st1 = "1";
    23. QString st2 = "2";
    24. connect(oneButton, SIGNAL(clicked()), this, SLOT(myslot(st1)));
    25. connect(twoButton, SIGNAL(clicked()), this, SLOT(myslot(st2)));
    26. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    27. }
    28.  
    29. void Calculator::myslot(QString str)
    30. {
    31. lineEdit -> setText(str);
    32. }
    To copy to clipboard, switch view to plain text mode 


    And main.cpp is this way:

    Qt Code:
    1. #include <QApplication>
    2. #include "calculator.h"
    3.  
    4. int main(int argc, char* argv[])
    5. {
    6. QApplication app(argc, argv);
    7. Calculator calculator;
    8. calculator.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    The app runs without errors but nothing will be shown in the lineEdit when clicking oneButton and twoButton! And also there are warning messages in the Application Output saying:


    QObject::connect: No such slot Calculator::myslot(st1) in ..\Calculator\calculator.cpp:24
    QObject::connect: No such slot Calculator::myslot(st2) in ..\Calculator\calculator.cpp:25


    What is the reason and how to use a slot in Qt like a function in C++ please?

  2. #2
    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: How to use a slot in Qt like a function in C++

    You can't simply connect signal without parameters to slot with parameters. Read about QSignalMapper.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use a slot in Qt like a function in C++

    Quote Originally Posted by franky View Post
    QObject::connect: No such slot Calculator::myslot(st1) in ..\Calculator\calculator.cpp:24
    QObject::connect: No such slot Calculator::myslot(st2) in ..\Calculator\calculator.cpp:25


    What is the reason and how to use a slot in Qt like a function in C++ please?
    The reason is, as the output already said, no slot in Calculator that has the name "myslot" and takes an argument of type "st1".

    Calculator has a slot named "myslot" but it takes a QString, not a st1.

    To call a slot like function you do exactly that: call it like a function

    Qt Code:
    1. myslot("foo");
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  4. #4
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to use a slot in Qt like a function in C++

    Thank you for the replies.

    But both st1 and st2 are of type Qstring.
    And I like to call that slot when I click on the buttons. So what do I need to do for that purpose please?

  5. #5
    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: How to use a slot in Qt like a function in C++

    Quote Originally Posted by franky View Post
    Thank you for the replies.

    But both st1 and st2 are of type Qstring.
    And I like to call that slot when I click on the buttons. So what do I need to do for that purpose please?
    Read about QSignalMapper. Example in documentation is exactly your problem.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use a slot in Qt like a function in C++

    Quote Originally Posted by franky View Post
    But both st1 and st2 are of type Qstring.
    You put "st1" as the type argument of the slot signature, so the connect was looking for a slot with an argument of type "st1"

    Quote Originally Posted by franky View Post
    And I like to call that slot when I click on the buttons.
    Since the clicked() signal as no argument (well, it has a bool), you connect it to a slot matching its signature.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 26th October 2013, 06:40
  2. Replies: 2
    Last Post: 26th August 2011, 09:51
  3. Replies: 4
    Last Post: 14th August 2011, 16:37
  4. My Slot Function
    By qtoptus in forum Qt Programming
    Replies: 3
    Last Post: 3rd November 2010, 17:38
  5. slot control of new function
    By tommy in forum Qt Programming
    Replies: 3
    Last Post: 11th November 2007, 04:58

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.