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!*/
{
Q_OBJECT
public:
public slots:
void updatenewvalues()
{
if(this->text() == "Don't click me!")
{
setText("Click me!");
return;
}
setText("Don't click me!");
}
};
#endif // PUSHBUTTON_H
#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
To copy to clipboard, switch view to plain text mode
Then:
/*now we employ our new class*/
int main(int argc, char* argv[])
{
PushButton clickme("Click me!");
clickme.resize(200, 30);
QObject::connect(&clickme,
SIGNAL(clicked
()),
&clickme,
SLOT(updatenewvalues
()));
clickme.show();
return app.exec();
}
/*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();
}
To copy to clipboard, switch view to plain text mode
I suggest some reading to help with your learning c++. You can find several good books on Qt with c++ at Amazon.com.
Bookmarks