PDA

View Full Version : signals and slots with little c++ knowledge



wenn32
18th October 2010, 02:58
hello, i am new to QT and i am trying to learn QT and its features.But as a C guy i am facing a bit difficult in understanding signals and slots except this feature the Docs provided by QT is more than enough for me

i use code blocks along with QT libraries to do the magic with C and it works great!.

i need help from you guys.can you please tell me how to create a new slots and how to manipulate data with it.i have some very small basic knowledge on c++ so i can understand little bit.

for example is this correct?


int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPushButton clickme("ClickeME");
clickme.resize(75, 30);
clickme.setFont(QFont("Times", 18, QFont::Bold));
QObject::connect(&clickme, SIGNAL(clicked()), &app, SLOT(updatenewvalues()));
clickme.show();
return app.exec();
}

void updatenewvalues()
{
/* some code here */
}



what i want this program to do is initially the value in clickme is "clickme" no when i press clickme i want the value of "clickme" to change to "dontclickme"

i have read the Docs from here
http://doc.trolltech.com/4.7/signalsandslots.html
but no help!

Timoteo
18th October 2010, 04:40
Here's your program done the c++ way:


#ifndef PUSHBUTTON_H
#define PUSHBUTTON_H
#include <QPushButton>
#include <QString>
/*Here, we subclass QPushButton so that we can define a new slot:
updatenewvalues().
It is worth noting that we could have subclassed QApplication
to add the new slot also. Qt is awesome like that!
For brevity's sake, the class is defined inline in a header. This is
generally a bad idea!*/

class PushButton: public QPushButton
{
Q_OBJECT
public:
PushButton(QWidget* parent = 0): QPushButton(parent){}
PushButton(const QString& text, QWidget* parent = 0) : QPushButton(text, parent){}
public slots:
void updatenewvalues()
{
if(this->text() == "Don't click me!")
{
setText("Click me!");
return;
}
setText("Don't click me!");
}
};
#endif // PUSHBUTTON_H


Then:


/*now we employ our new class*/
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
PushButton clickme("Click me!");
clickme.resize(200, 30);
clickme.setFont(QFont("Times", 18, QFont::Bold));
QObject::connect(&clickme, SIGNAL(clicked()), &clickme, SLOT(updatenewvalues()));
clickme.show();
return app.exec();
}

I suggest some reading to help with your learning c++. You can find several good books on Qt with c++ at Amazon.com.