如何在槽函数中播放音乐-时钟程序
--------------------编程问答--------------------
#ifndef MYCLOCK_H
#define MYCLOCK_H
#include <QtGui/QWidget>
//#include "ui_mClock.h"
class QTime;
class QTimer;
class QLCDNumber;
class QTimeEdit;
class QAction;
class QLabel;
class QPushButton;
class myClock : public QWidget
{
Q_OBJECT
public:
myClock(QWidget *parent = 0);
//~myClock();
signals:
void isCurrentTime();
private slots:
void clockChange(); //更新时间
void alarmOut(); //闹钟时间到,响起音乐
void mini();
void comeToCurrent(); //比较时间
protected:
void closeEvent(QCloseEvent * event);
private:
void init();//初始化
void layout();//布局
void myconnect();//链接信号与槽
QLCDNumber * lcdNumber;//显示当前时间
QTimeEdit * timeEdit; //设置闹钟
QPushButton * button;
QTimer * timer;
QTimer * uptimer;
QLabel * label;
//Ui::myClockClass ui;
};
#endif // MYCLOCK_H
--------------------编程问答--------------------
#include "myClock.h"
#include <QtGui>
myClock::myClock(QWidget *parent)
: QWidget(parent)
{
init();
layout();
myconnect();
}
//初始化
void myClock::init()
{
setWindowTitle("Alarm Clock"); //设置标题
setFixedSize(QSize(600,200)); //设置程序框大小
timer = new QTimer; //控制LCD的时间刷新
uptimer = new QTimer; //闹钟时间到,对话框上升
lcdNumber = new QLCDNumber; //显示当前时间
lcdNumber->setNumDigits(8);
lcdNumber->display(QTime::currentTime().toString("hh:mm:ss"));
label = new QLabel(QString::fromLocal8Bit("Alarm:"));
timeEdit = new QTimeEdit;
timeEdit->setDisplayFormat("hh:mm:ss");
button = new QPushButton("Sure");
}
//布局
void myClock::layout()
{
QHBoxLayout *righttopLayout = new QHBoxLayout;
righttopLayout->addWidget(label);
righttopLayout->addWidget(timeEdit);
QHBoxLayout *rightLayout = new QHBoxLayout;
rightLayout->addLayout(righttopLayout);
rightLayout->addWidget(button);
QHBoxLayout *mainLayout = new QHBoxLayout; //程序出错
mainLayout->addWidget(lcdNumber);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
}
//链接信号与槽
void myClock::myconnect()
{
connect(timer, SIGNAL(timeout()), this, SLOT(clockChange()));
timer->start(1000);
connect(uptimer, SIGNAL(timeout()), this, SLOT(comeToCurrent()));
uptimer->start(20);
connect(this, SIGNAL(isCurrentTime()), this, SLOT(alarmOut()));
connect(button, SIGNAL(clicked()), this, SLOT(mini()));
}
//比较现在的时间与设置的时间
void myClock::comeToCurrent()
{
if(QVariant(QTime::currentTime()).toString() == QVariant(timeEdit->time()).toString())
{
//QApplication::beep();
emit isCurrentTime();
//alarmOut();
}
}
//显示时间变化
void myClock::clockChange()
{
lcdNumber->display(QTime::currentTime().toString("hh:mm:ss"));
}
//闹钟时间到
void myClock::alarmOut()
{
QSound::play("001.wma");
uptimer->stop();
}
//
void myClock::mini()
{
showMinimized();
//hide();
}
void myClock::closeEvent(QCloseEvent * event)
{
QMessageBox::information(0,"Quit","Are you sure exit the Application");
}
//闹钟时间到
void myClock::alarmOut()
{
QSound::play("001.wma");
uptimer->stop();
}
这个槽函数来播放音乐,但是触发后只能响一声,不能完整播放音乐,不知为何?
补充:移动开发 , Qt