Results 1 to 20 of 22

Thread: QPushButton and connect problem(((

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPushButton and connect problem(((

    hi! I am new to qt and now I have a little problem((
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QWidget wnd;
    6. wnd.resize(200, 100);
    7. wnd.setWindowTitle("w3.cpp");
    8. wnd.show();
    9.  
    10. QPushButton *btn = new QPushButton("Press Me", &wnd);
    11. QObject::connect(btn, SIGNAL(clicked()), btn, SLOT(setText("hi!")));
    12. btn->move(50, 40);
    13. btn->show();
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    i just want to do such thing - after clicking button its text changes to "hi"))
    where is my mistake??? help please((

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

    Default Re: QPushButton and connect problem(((

    Signal void my_signal() must be connected to slot void my_slot(). Read about QSignalMapper

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

    Default Re: QPushButton and connect problem(((

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QPushButton and connect problem(((

    There are a lot of issues there:
    1) the signature of signal must match with the one of the slot (or the slot must be available to be called with the arguments of the signal - in case the slot has default values for arguments)
    2) on the connect statement you don't pass variable names, you don't pass variable values - you can write there only type names

    What you want it can be done by inheriting your own button from QPushButton and write it a slot SetDefaultText and code the string parameter to have a default value.

    You can read more about signals and slots here

  5. #5
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton and connect problem(((

    i've rewritten my code a bit but it still does not work((
    Qt Code:
    1. //w3.cpp
    2. #include <mybutton.h>
    3. #include <QObject>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. mybutton *btn = new mybutton("Press me)))");
    10. QObject::connect(btn, SIGNAL(clicked()), btn, SLOT(setTxt("hi!")));
    11. btn->show();
    12.  
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //mybutton.h
    2. #include <QtGui>
    3. #include <QString>
    4.  
    5. class mybutton : public QPushButton
    6. {
    7. public:
    8. mybutton();
    9. mybutton(QString txt);
    10.  
    11. public slots:
    12. void setTxt(QString txt);
    13. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //mybutton.cpp
    2. #include <mybutton.h>
    3.  
    4. mybutton::mybutton()
    5. {
    6. }
    7.  
    8. mybutton::mybutton(QString txt)
    9. {
    10. setText(txt);
    11. }
    12.  
    13. void mybutton::setTxt(QString txt)
    14. {
    15. setText(txt);
    16. emit clicked();
    17. }
    To copy to clipboard, switch view to plain text mode 

    what should i do??

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

    Default Re: QPushButton and connect problem(((

    Follow the link I gave you. Pay attention to how your signal/slot connection looks like.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton and connect problem(((

    Can someone tell me what should i do with signals and slots to make my app working??? because i just do not know what to to((( please....

  8. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QPushButton and connect problem(((

    In your class declaration you forgot some things:
    1) the Q_OBJECT macro (you need that for signals and slots to work)
    2) you didn't put a default parameter for the slot. (slot declaration should be something like: public slots: void setTxt(QString txt = "Hi!"); ) and connect doesn't have anything else but type - no variable name, no value

    //don't forget to Rebuild after you add the Q_OBJECT macro (maybe clean all then run qmake and rebuild)

    And also you might want to take a QWidget* as a parent and pass it to the QPushButton constructor (so that you won't have memory leaks in your class)
    Last edited by Zlatomir; 11th June 2011 at 17:26.

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

    make (11th June 2011)

  10. #9
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton and connect problem(((

    i have finally done that/ but it still does not work((( maybe there are new mistakes??
    Qt Code:
    1. //mybtn.h
    2. #ifndef MYBTN_H
    3. #define MYBTN_H
    4.  
    5. #include <QtGui>
    6. #include <QString>
    7.  
    8. class mybtn : public QPushButton
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. mybtn(QString txt);
    14.  
    15. public slots:
    16. void setTxt(QString txt = "hi!");
    17. };
    18.  
    19. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //mybtn.cpp
    2. #include <mybtn.h>
    3.  
    4. mybtn::mybtn(QString txt)
    5. {
    6. setText(txt);
    7. }
    8.  
    9. void mybtn::setTxt(QString txt)
    10. {
    11. setText(txt);
    12. emit clicked();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //w3.cpp
    2. #include <mybtn.h>
    3. #include <QApplication>
    4. #include <QObject>
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. mybtn *btn = new mybtn("Press me))");
    11. QObject::connect(btn, SIGNAL(clicked()), btn, SLOT(setTxt(QString)));
    12. btn->show();
    13.  
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    the application compiles successfully but the button does not change its text((

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

    Default Re: QPushButton and connect problem(((

    Did You read my previous post ? I think : NO.
    This is a warning from Qt when Yout try make connection :

    QObject::connect: Incompatible sender/receiver arguments
    mybtn::clicked() --> mybtn::setTxt(QString)

  12. #11
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton and connect problem(((

    so what should i do with signal clicked()?? i just do not understand((

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

    Default Re: QPushButton and connect problem(((

    Look, here is your connect statement:
    Qt Code:
    1. QObject::connect(btn, SIGNAL(clicked()), btn, SLOT(setTxt(QString)));
    To copy to clipboard, switch view to plain text mode 
    It says -- whenever btn emits signal clicked(), call method setTxt() on btn, passing it a QString argument. Now the basic question is, what argument (value) should Qt pass to the setTxt() call in this particular situation? Do you think your statement makes sense? Why? If not, what needs to be changed for it to make sense?
    Last edited by wysota; 13th June 2011 at 12:29.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Mar 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton and connect problem(((

    but i thought that:

    Quote Originally Posted by Zlatomir View Post
    2) on the connect statement you don't pass variable names, you don't pass variable values - you can write there only type names
    so i have written the default value in class implementation...

Similar Threads

  1. Replies: 2
    Last Post: 2nd May 2011, 08:10
  2. connect a QPushButton matrix with a function
    By harmodrew in forum Newbie
    Replies: 6
    Last Post: 6th August 2010, 11:11
  3. connect a qpushbutton a slot
    By Lycus HackerEmo in forum Newbie
    Replies: 13
    Last Post: 29th March 2010, 09:14
  4. QSS problem on a QPushButton
    By ber0y in forum Newbie
    Replies: 6
    Last Post: 21st July 2009, 07:56
  5. QPushButton problem
    By sincnarf in forum Qt Programming
    Replies: 4
    Last Post: 20th August 2007, 21:02

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
  •  
Qt is a trademark of The Qt Company.