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....
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....
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.
make (11th June 2011)
i have finally done that/ but it still does not work((( maybe there are new mistakes??
Qt Code:
//mybtn.h #ifndef MYBTN_H #define MYBTN_H #include <QtGui> #include <QString> { Q_OBJECT public: public slots: }; #endifTo copy to clipboard, switch view to plain text modeQt Code:
//mybtn.cpp #include <mybtn.h> { setText(txt); } { setText(txt); emit clicked(); }To copy to clipboard, switch view to plain text modeQt Code:
//w3.cpp #include <mybtn.h> #include <QApplication> #include <QObject> int main(int argc, char **argv) { mybtn *btn = new mybtn("Press me))"); btn->show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
the application compiles successfully but the button does not change its text((
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)
so what should i do with signal clicked()?? i just do not understand((
Look, here is your connect statement:
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?Qt Code:
To copy to clipboard, switch view to plain text mode
Last edited by wysota; 13th June 2011 at 12:29.
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:
//Read the documentation for signal and slots (i gave you a link in my first post)Qt Code:
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
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.
make (14th June 2011)
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:
//myheader.h #ifndef MYHEADER_H #define MYHEADER_H //........... #endifTo copy to clipboard, switch view to plain text mode
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 itEspecially 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?
Bookmarks