【发布时间】:2017-04-21 12:17:34
【问题描述】:
当second 更改时,我正在尝试播放声音。
在我的stopwatch 中有milliseconds,它在miliseconds' 变化下播放,而不是seconds'。我应该为*seconds* 创建一个条件。
但是不能正确地做到这一点,你能建议这种情况吗?
这是我的方法(保留QMediaPlayer 的对象 for milliseconds
):
我的构造函数和插槽:
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);
}
}
【问题讨论】:
-
timerEvent期间是什么? -
@eyllanesc 它是用我的按钮
countinue为 cennection 创建的。这是我的 SLOT,其中使用了timerEvent:void StopMainWindow::pause(void) { **timerEvent**(new QTimerEvent(0)); mTotalTime += mSessionTime; mRunning = false; } -
你可以把你的完整代码,因为它看起来不正确。
-
@eyllanesc 我已经编辑了,请检查一下。
-
@eyllanesc 在我看来,我不应该在
timerEvent中玩music。我想,我必须使用QDateTime::currentDateTime();函数。但我无法理解,我应该如何将它与音乐联系起来..