Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: QPushButton and connect problem(((

  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...

  15. #14
    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(((

    Yes, what i said remains valid, but in your particular case you don't pass anything - because the clicked signal has void parameters and if the slot (member function) has default arguments (void setTxt(QString txt = "hi!"); ) so it can be called with void parameter ( just setTxt() ) - and it will just use the default value.

    So the connect statement will be:
    Qt Code:
    1. QObject::connect(btn, SIGNAL(clicked()), btn, SLOT(setTxt())); //no QString here (it will use the default)
    To copy to clipboard, switch view to plain text mode 
    //Read the documentation for signal and slots (i gave you a link in my first post)

    LE: Also the setTxt slot should not emit clicked signal (you end up with an infinite loop) so delete/comment the line: emit clicked(); from void mybtn::setTxt(QString txt) {...}
    Last edited by Zlatomir; 13th June 2011 at 13:26.

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

    make (14th June 2011)

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

    Default Re: QPushButton and connect problem(((

    Yes!!! thanks to all!! if finally works)) a bit later i will post full working code for other beginners)) (i should say that such little program is done much more simple using gtk)
    but now i have the last question:
    why is it necessary to write in header file such definitions like:
    Qt Code:
    1. //myheader.h
    2. #ifndef MYHEADER_H
    3. #define MYHEADER_H
    4.  
    5. //...........
    6.  
    7. #endif
    To copy to clipboard, switch view to plain text mode 

  18. #16
    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(((

    It is not necessary to write such definitions. Remove it, try compiling the project and if it fails, read the error message.

    As for what you said about GTK -- I somehow doubt it Especially that GTK is C-based. We can even make a contest -- you post GTK code that does what you want and I post Qt code that does the same. Then I will post some short Qt program that does something and you'll post its GTK equivalent and we'll compare them together, ok?
    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.


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

    Default Re: QPushButton and connect problem(((

    As for gtk i can say now that i was wrong((( So there will not be any challange because i just do not know gtk on the good level))) some month ago i was choosing DE for using (gnome xfce or kde) and i also looked for information about development native programs for these DE)) that is why i was a bit interested in gtk and Vala)))
    here is equivalent in vala
    Qt Code:
    1. //gtk1.vala
    2. using Gtk;
    3.  
    4. int main(string argc[])
    5. {
    6. Gtk.init(ref argc);
    7.  
    8. var wnd = new Window();
    9. wnd.title = "Gtk1";
    10. wnd.set_default_size(300, 50);
    11. wnd.destroy.connect(Gtk.main_quit);
    12.  
    13. var btn = new Button.with_label("Click me :))");
    14. btn.clicked.connect(() =>
    15. {
    16. btn.label = "Thank you!";
    17. });
    18. wnd.add(btn);
    19.  
    20. wnd.show_all();
    21.  
    22. Gtk.main();
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 

    but now i am using kde that is why i started with qt))) that is why i am here)))

    here is FULL QT CODE for this simple program))

    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();
    14. mybtn(QString title);
    15. mybtn(QString title, QWidget *wnd);
    16.  
    17. public slots:
    18. void setTxt(QString txt = "hi!");
    19. };
    20.  
    21. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //mybtn.cpp
    2. #include <mybtn.h>
    3.  
    4. mybtn::mybtn()
    5. {
    6. }
    7.  
    8. mybtn::mybtn(QString title)
    9. {
    10. setText(title);
    11. }
    12.  
    13. mybtn::mybtn(QString title, QWidget *wnd) : QPushButton(title, wnd)
    14. {
    15. setText(title);
    16. }
    17.  
    18. void mybtn::setTxt(QString txt)
    19. {
    20. setText(txt);
    21. }
    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. QWidget wnd;
    11. wnd.resize(200, 100);
    12. wnd.setWindowTitle("w3");
    13. wnd.show();
    14.  
    15. mybtn *btn = new mybtn("Press me))", &wnd);
    16. QObject::connect(btn, SIGNAL(clicked()), btn, SLOT(setTxt()));
    17. btn->resize(100, 25);
    18. btn->move(50, 35);
    19. btn->show();
    20.  
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

  20. #18
    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(((

    Cheating by using a language with lambda functions doesn't prove superiority of GTK (try doing the same in C). Especially when Qt can use JavaScript or Python to do a similar cheat

    But let's focus on C++ and cheat there:
    Qt Code:
    1. #include <QtGui>
    2. int main(int argc, char **argv){
    3. QApplication app(argc, argv);
    4. QPushButton b("text");
    5. m.setMapping(&b, "hi");
    6. connect(&b, SIGNAL(clicked()), &m, SLOT(map()));
    7. connect(&m, SIGNAL(mapped(QString)), &b, SLOT(setText(QString)));
    8. b.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    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.


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

    Default Re: QPushButton and connect problem(((

    WOW))) I like it )) thanks for sample))) i could not follow your advice earlier because i just did not understand)

  22. #20
    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(((

    Remember it is just a cheat that is not good for production use. Normally you wouldn't want to expose a button like that and surely using a QSignalMapper for such thing is an overkill.

    Now let's reverse the situation, implement this using GTK/Vala:

    Qt Code:
    1. #include <QtGui>
    2. int main(int argc, char **argv){
    3. QApplication app(argc, argv);
    4. QWidget window;
    5. QHBoxLayout *l = new QHBoxLayout(&window);
    6. QPushButton *b1 = new QPushButton("b1");
    7. QPushButton *b2 = new QPushButton("b2");
    8. l->addWidget(b1);
    9. l->addWidget(b2);
    10. QSizePolicy policy = b1->sizePolicy();
    11. policy.setHorizontalStretch(1);
    12. b1->setSizePolicy(policy);
    13. policy.setHorizontalStretch(3);
    14. b2->setSizePolicy(policy);
    15. window.show();
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    In text: prepare a window with two push buttons that occupy the whole width of the window in such a way that the width of the button on the left is always 1/3 the width of the button on the right.
    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.


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.