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

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

    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)

  3. #3
    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((

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


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

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

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

    make (14th June 2011)

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

  9. #8
    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.


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.