#include#include int main(int argc, char *argv[]) { QApplication a(argc, argv); QSpinBox spinBox; spinBox.setMinimum(0); spinBox.setMaximum(100); spinBox.setValue(42); int value = spinBox.value(); qDebug() << "SpinBox value:" << value; return a.exec(); }
#includeThis example creates a subclass of QSpinBox called MySpinBox that defines a custom onValueChanged() slot. It then creates an instance of MySpinBox and connects its valueChanged() signal to its own slot using QObject::connect(). Whenever the user changes the value of the spin box, the onValueChanged() slot gets triggered, which prints the new value to the console. Note that the Q_OBJECT macro and #include "main.moc" are required for the signals and slots mechanism to work. The package library for QSpinBox is QtWidgets.#include class MySpinBox : public QSpinBox { Q_OBJECT public slots: void onValueChanged(int value) { qDebug() << "SpinBox value changed:" << value; } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MySpinBox spinBox; spinBox.setMinimum(0); spinBox.setMaximum(100); spinBox.setValue(42); QObject::connect(&spinBox, SIGNAL(valueChanged(int)), &spinBox, SLOT(onValueChanged(int))); return a.exec(); } #include "main.moc"