I am trying to play a sound ,when a second
chages.
In my stopwatch
there are milliseconds
, and it plays under miliseconds'
changes, not seconds'
ones. I should create a condition for *seconds*
.
But can't do it correctly, can you advice this very condition?
Here is my method(stay the object of QMediaPlayer
for milliseconds
):
My constructor and SLOTS:
StopMainWindow::StopMainWindow(QWidget *parent) :
QMainWindow(parent),
mRunning(false)
, mStartTime()
, mTotalTime(0)
{
//ON THE TOP THERE ARE QPUSHBUTTONS AND QLABELS OBJECTS
connect(pushButton_Start, SIGNAL(clicked()), SLOT(start()));
connect(pushButton_go_on, SIGNAL(clicked()), SLOT(pause()));
connect(pushButton_Stop, SIGNAL(clicked()), SLOT(stop()));
connect(pushButton_Close, SIGNAL(clicked()), SLOT(close()));
connect(pushButton_Back,SIGNAL(clicked()),SLOT(back()));
///////////////////////////////////////////////////////////////////////////////
setCentralWidget(centralWidget);
///////////////////////////////////////////////////////////////////////////////////////////////
pushButton_Start->setEnabled(true);
pushButton_go_on->setEnabled(false);
pushButton_Stop->setEnabled(false);
startTimer(0);
}
StopMainWindow::~StopMainWindow()
{}
void StopMainWindow::start(void)
{
pushButton_Start->setEnabled(false);
pushButton_go_on->setEnabled(true);
pushButton_Stop->setEnabled(true);
mStartTime = QDateTime::currentDateTime();
mRunning = true;
QMediaPlayer *music=new QMediaPlayer();
music->setMedia(QUrl("qrc:/sounds/tik.mp3"));
music->play();
}
void StopMainWindow::stop(void)
{
pushButton_Start->setEnabled(true);
pushButton_go_on->setEnabled(true);
pushButton_Stop->setEnabled(false);
mTotalTime = 0;
mRunning = false;
}
void StopMainWindow::pause(void)
{
pushButton_Start->setEnabled(true);
pushButton_go_on->setEnabled(false);
pushButton_Stop->setEnabled(true);
timerEvent(new QTimerEvent(0));
mTotalTime += mSessionTime;
mRunning = false;
}
void StopMainWindow::timerEvent(QTimerEvent *)
{
if(mRunning)
{
mSessionTime = mStartTime.msecsTo(QDateTime::currentDateTime());
qint64 time = mTotalTime + mSessionTime;
time *= 111;
unsigned int h = time / 1000 / 60 / 60;
unsigned int m = (time / 1000 / 60) - (h * 60);
unsigned int s = (time / 1000) - ((m + (h * 60))* 60);
unsigned int ms = time - (s + ((m + (h * 60))* 60)) * 1000;
const QString diff = QString("%1:%2:%3,%4").arg(h, 2, 10, QChar('0')).arg(m, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0')).arg(ms, 3, 10, QChar('0'));
mLabel->setText(diff);
}
}