PDA

View Full Version : a countdown widget



anarsynd
16th November 2007, 19:02
Hi, I need a widget to countdown - to display it over a webcam display, before I capture a photo. Have you seen anything alike?

wysota
16th November 2007, 20:09
Count down what? Have you seen QLCDNumber?

anarsynd
19th November 2007, 14:48
To countdown, you know, like before a rocket launch ;)

I've seen the QLCDNumber and other standard Qt widgets. The question was if somebody has seen a widget meant precisely for countdown - it's all about reusability, isn't it? ;)

wysota
19th November 2007, 17:55
Hmm... what's wrong with QLCDNumber? Reusability means exactly that you should reuse an existing component for different tasks, not that you should look if someone already had a similar problem...

anarsynd
19th November 2007, 21:34
If somebody had a similar problem, and solved it, then a component doing a nice countdown already exists... and I would like to reuse it ;)

The thing with QLCDNumber is that it only displays a number. And I need to do an animation, like 3... 2... 1... boom - with a good timing and possibly some sound in parallel. Moreover, I'm not so sure if I would like to use an LCD-look.

wysota
19th November 2007, 21:57
Ok, tell me what do you want then, and I'll give you code that does what you want. Satisfied?

anarsynd
19th November 2007, 23:44
No no... It's not what I meant. I'll wait a couple of days and if I see that nobody had prepared such a widget before, I'll do it myself.

I wouldn't ask anybody to do my job for me - I only wander if I could reuse something somebody has already done for himself before. Isn't this forum a place of such exchange among developers using Qt? (I'm asking this question because from your posts, Wysota, I get an impression that I did something strange here...)

wysota
20th November 2007, 00:35
Long story short - you won't get a "countdown widget" from anyone, because if someone needed it, he would use QLCDNumber or QLabel with a QTimer - ten lines of code total (twenty if you want a more capable version). And that's exactly what I call code reuse - you use QLCDNumber or QLabel for different things.

Even shorter - you are wasting your time waiting a week for something you would acomplish yourself in 5 minutes.

anarsynd
20th November 2007, 12:19
You think I was doing nothing but waiting? It's just my idle time - looking at this forum ;)

Thanks for your opinion, anyway. It's curious, though, how you can answer in the name of all Qt users...

wysota
20th November 2007, 13:40
It's curious, though, how you can answer in the name of all Qt users...

If you hang out here for two years, you'll know why...

Here is the promised implementation, you can base on it to create your own:


class CountDownWidget : public QLCDNumber {
Q_OBJECT
public:
CountDownWidget(QWidget *parent=0) : QLCDNumber(parent){ m_id = -1;}
public slots:
void start(int val=-1){ if(val>=0) display(val); m_id = startTimer(1000); }
void stop(){ killTimer(m_id);}
signals:
void timeout();
protected:
void timerEvent(QTimerEvent *e){
display(value()-1);
if(value()<=0){ killTimer(m_id); m_id = -1; display(0); emit timeout(); return;}
}
};

anarsynd
20th November 2007, 14:06
Thanks for the code - I'll build on it :-)

And for the sound part? What would you use to play a wave file each time the displayed number changes?

wysota
20th November 2007, 14:15
And for the sound part? What would you use to play a wave file each time the displayed number changes?

Well... currently QSound is the only option. Since Qt 4.4 you'll be able to use anything Phonon (http://phonon.kde.org/) offers.

anarsynd
20th November 2007, 14:51
Then I guess I know all I need. Thanks.