User Tools

Site Tools


qt:signal_slot_examples

Signal and slot examples

From Molkentin:

main.cpp
// signalSlot/main.cpp
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPushButton button("Quit");
    button.show();
    QObject::connect(&button, SIGNAL(clicked()),
                     &app, SLOT(quit()));
    return app.exec();
}
main.cpp
// signalSlot2/main.cpp
#include <QApplication>
#include <QVBoxLayout>
#include <QLabel>
#include <QSpinBox>
#include <QSlider>
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget window;
    QVBoxLayout* mainLayout = new QVBoxLayout(&window);
    QLabel* label = new QLabel("0");
    QSpinBox* spinBox = new QSpinBox;
    QSlider* slider = new QSlider(Qt::Horizontal);
    mainLayout->addWidget(label);
    mainLayout->addWidget(spinBox);
    mainLayout->addWidget(slider);
    QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                     label, SLOT(setNum(int)));
    QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                     slider, SLOT(setValue(int)));
    QObject::connect(slider, SIGNAL(valueChanged(int)),
                     label, SLOT(setNum(int)));
    QObject::connect(slider, SIGNAL(valueChanged(int)),
                     spinBox, SLOT(setValue(int)));
    window.show();
    return app.exec();
}
qt/signal_slot_examples.txt · Last modified: 2010/12/09 14:51 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki