Qt Signals Example
← Qt Signals And Slots | ● | Qt Native Orientation Sensor →
Any Qt widget class has a set of predefined signals. For example, the QPushButton class has a signal “clicked()”, which is triggered whenever the button is clicked. If we want to react on a button click, we connect a slot to that signal:
{
Q_OBJECT
public:
ClickMe(QWidget *parent = NULL)
: QWidget(parent)
{
QPushButton *button = new QPushButton("Click me - pleeease!");
button->setStyleSheet("QPushButton { color: red; }");
connect(button, SIGNAL(clicked()), this, SLOT(click()));
label = new QLabel;
QVBoxLayout * layout = new QVBoxLayout;
layout->addWidget(button);
layout->addWidget(label);
setLayout(layout);
}
protected:
QLabel *label;
protected slots:
void click()
{
label->setText("OMG - you clicked it!");
}
};
Note the use of Qt style sheets to define visual properties of the widgets.
Q Why is the app crashing when pressing the Android back button?
Because the default behaviour is to delete the activity, which crashes the app. We need to catch the back button and exit cleanly in case it was pressed and released:
void keyReleaseEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Back)
exit(0);
else
QWidget::keyPressEvent(event);
}
Full source code available here:
svn co svn://schorsch.efi.fh-nuernberg.de/qt-android/simpleqt