1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <QtGui>
class myWidget : public QLabel
{
Q_OBJECT
int val;
QTimer timer;
public :
myWidget(QWidget * parent=0)
:QLabel(parent),val(0)
{
timer.start(50);
connect(&timer,SIGNAL(timeout()),this,SLOT(timeout()));
}
public slots:
void pressed()
{
timer.stop();
setText("pressed");
}
void released ()
{
timer.start(50);
setText("released");
}
void timeout()
{
val = (val+1)%100;
emit value(val);
}
signals :
void value(int);
};
#include "main.moc"
int
main(int argc,char**argv)
{
QApplication app(argc,argv);
QWidget w;
QSlider * slider = new QSlider;
myWidget *mw = new myWidget;
QObject::connect(slider,SIGNAL(sliderReleased()),mw,SLOT(released()));
QObject::connect(slider,SIGNAL(sliderPressed()),mw,SLOT(pressed()));
QObject::connect(mw,SIGNAL(value(int)),slider,SLOT(setValue(int)));
QHBoxLayout *l = new QHBoxLayout;
l->addWidget(slider);
l->addWidget(mw);
w.setLayout(l);
w.show();
return app.exec();
} |
Partager