|
1Qter豆
本帖最后由 judas1987 于 2013-10-10 15:56 编辑
这个是我在网上找到的一个代码,修修改改以后可以运行,但是唯一的问题就是托盘图标和程序图标显示不出来。
还有这个地方quitAct=new QAction(QString::fromUtf8("关闭"),this);,写成“退出”系统就报错……
下面贴出代码- #include"alarm.h"
- #include<QtGui>
- Alarm::Alarm(QWidget * parent)
- :QDialog(parent)
- {
- init();
- layout();
- con();
- };
- // ******
- // 初始化
- // ******
- void Alarm::init()
- {
- setWindowTitle("闹钟"); //设置标题
- setFixedSize(QSize(320,80)); //固定大小
- timer=new QTimer; //控制lcd的时间刷新
- uptimer=new QTimer; //控制对话框上升
- dtimer=new QTimer; //控制对话框下降
- itimer=new QTimer;
- lcdNumber=new QLCDNumber; //显示当前时间
- lcdNumber->setFixedSize(QSize(150,50)); //固定大小
- lcdNumber->setDigitCount(8); //显示八个数字,默认是显示5个数字
- lcdNumber->setSegmentStyle(QLCDNumber::Flat); //填充颜色,默认是白色的
- lcdNumber->display(QTime::currentTime().toString("hh:mm:ss")); //设置格式,还有其他形式的
- label=new QLabel(QString::fromUtf8("闹钟设置")); //提示设置下个闹钟
- //使用fromLocal8Bit()函数,实现了从本地字符集GB到Unicode的转换,主要应用于处理汉语显示乱码等问题
- //实验发现还是乱码,只有fromUtf8()函数可以实现汉字的正常显示
- timeEdit=new QTimeEdit; //设置下个闹钟
- timeEdit->setDisplayFormat("hh:mm:ss");
- button=new QPushButton(QString::fromUtf8("确定"));
- if (!QSystemTrayIcon::isSystemTrayAvailable()) //判断系统是否支持系统托盘图标
- {
- return;
- }
- trayIcon=new QSystemTrayIcon(this); //托盘icon
- trayIcon->setIcon(QIcon(":/ico.ico")); //设置图标图片,有问题,验证
- //trayIcon->show();
- setWindowIcon(QIcon(":/png.png")); //设置窗口图片
- trayIcon->setToolTip("闹钟 V1.0");
- //设置鼠标放在图标上的提示信息
- trayIcon->show();
- restoreAct=new QAction(QString::fromUtf8("显示"),this);
- quitAct=new QAction(QString::fromUtf8("关闭"),this);
- //不知道为什么,这里如果写成“退出”会报错,说常量中有换行符,改成其他的就没有问题
- trayIconMenu=new QMenu;
- trayIconMenu->addAction(restoreAct);
- trayIconMenu->addAction(quitAct);
- trayIcon->setContextMenu(trayIconMenu); //设置托盘上下文菜单,右键菜单
- setWindowFlags(Qt::WindowStaysOnTopHint);
- };
- // ****
- // 布局
- // ****
- void Alarm::layout()
- {
- QHBoxLayout * rightTopLay=new QHBoxLayout;//设置右边上方的layout,自左向右为label和timeEdit
- rightTopLay->addWidget(label);
- rightTopLay->addWidget(timeEdit);
- QHBoxLayout * rightButtomLay=new QHBoxLayout;//
- rightButtomLay->addStretch(); //有个弹簧顶着
- rightButtomLay->addWidget(button);
- QVBoxLayout * rightLay=new QVBoxLayout;//垂直布局,从上往下
- rightLay->addLayout(rightTopLay);
- rightLay->addLayout(rightButtomLay);
- rightLay->addStretch();
- QHBoxLayout * main=new QHBoxLayout;//主布局,左右布局
- main->addWidget(lcdNumber);
- main->addLayout(rightLay);
- setLayout(main);
- rect=QApplication::desktop()->rect();//
- x=rect.right()-width();//left()则是从左起,往右为加;right()是从右起,往左为减
- y=rect.bottom()+20;//从下自上,设置缓慢上升时的初始y位置
- //控制弹窗的初始位置
- setGeometry(x,y,width(),height());
- //从屏幕上(x,y)位置开始(即为最左上角的点),显示一个width()*height()的界面
- };
- // ****************
- // connect 信号与槽
- // ****************
- //Qt4的语法?
- void Alarm::con()
- {
- connect(timer,SIGNAL(timeout()),this,SLOT(clockChange()));
- timer->start(1000);
- //当timer到期,发出信号给clockChange接收,即更新LCD显示的时间,间隔为1000毫秒(一秒)
- //timeout()->This signal is emitted when the timer times out.
- connect(uptimer,SIGNAL(timeout()),this,SLOT(dialogUp()));
- uptimer->start(20);
- //对话框上升
- connect(dtimer,SIGNAL(timeout()),this,SLOT(down()));
- dtimer->start(20);
- //对话框下降
- connect(button,SIGNAL(clicked()),dtimer,SIGNAL(timeout()));
- connect(button,SIGNAL(clicked()),this,SLOT(mini()));
- //当button被点击时,对话框下降,并且最小化到托盘
- connect(itimer,SIGNAL(timeout()),this,SLOT(comToCur()));
- itimer->start(20);
- connect(restoreAct,SIGNAL(triggered(bool)),uptimer,SIGNAL(timeout()));
- connect(this,SIGNAL(isCurrentTime()),uptimer,SIGNAL(timeout()));
- //两个SIGNAL,指的是信号关联
- connect(itimer,SIGNAL(timeout()),this,SLOT(comToCur()));
- itimer->start(10);
- //itimer是一个判断,判断设定时间和真实时间差的。
- //itimer到期,发出信号给comToCur()接收。
- //comToCur()说明,当真实时间和设定时间一致时,执行函数,发送信号给isCurrentTime()
- //isCurrentTime()信号和uptimer()信号相关联,传递给uptimer(),执行对话框的上升
- connect(quitAct,SIGNAL(triggered(bool)),this,SLOT(close()));
- //退出
- };
- // **************
- // 显示变化的时间
- // **************
- // 和信号槽联合起来,每秒刷新一次,重置lcdNumber,实现时钟动起来
- void Alarm::clockChange()
- {
- lcdNumber->display(QTime::currentTime().toString("hh:mm:ss"));
- };
- // **************************
- // 闹钟到了,提示框慢慢升起来
- // **************************
- void Alarm::dialogUp()
- {
- show();
- uptimer->start(20);
- if(y>=rect.bottom()-height()-50)
- {
- y-=10;//设置升起的速度
- }
- else
- {
- uptimer->stop();//全部升起则停止
- }
- setGeometry(x,y,width(),height());
- };
- // *************************
- // 确定后,下降,然后隐藏起来
- // *************************
- void Alarm::down()
- {
- dtimer->start(20);
- if(y<rect.bottom())
- {
- y+=height()+20;//下降的深度
- }
- else
- {
- dtimer->stop();
- }
- setGeometry(x,y,width(),height());
- };
- // ************
- // 最小化到托盘
- // ************
- void Alarm::mini()
- {
- hide();
- };
- // ***********
- // close Event
- // ***********
- void Alarm::closeEvent(QCloseEvent *event)
- {
- QMessageBox::information(0,"Quit","Are you sure exit the Alarm?");
- };
- // ****************************
- // 对比现在时间,与所设置的时间
- // ****************************
- void Alarm::comToCur()
- {
- if( QVariant(QTime::currentTime()).toString() == QVariant(timeEdit->time()).toString() )
- {
- QApplication::beep();
- emit isCurrentTime();
- }
- };
复制代码 我的图标路径是D:\Qt\learn\alarm,也创建了images文件夹,在qrc中也能显示这两个图片……实在不知道了……请教各位
|
最佳答案
查看完整内容
qrc的路径?qrc好像加入工程就好,路径不打紧的。主要应该是qrc里面的前缀和文件名(我比较喜欢设置别名),例如你新建前缀:/image,加入图片start.png,然后设置别名start,程序里面就写":/image/start"就好。label载入图片,我比较喜欢放在本目录,这个百度qt qlabel显示图片之类,网上会有很多教程的。
|