Qt事件--数字时钟
2011-12-01 23:31
141 查看
实验:
1)用Timer显示时间
2)用Timer定时
3)鼠标左键按下控件可随意在桌面上拖动
main.cpp
view plaincopy
to clipboardprint?
#include <qapplication.h>
#include "form2.h"
int main( int argc,char ** argv )
{
QApplication a( argc, argv );
Form2 w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
转载自:/article/2459277.html
1)用Timer显示时间
2)用Timer定时
3)鼠标左键按下控件可随意在桌面上拖动
main.cpp
view plaincopy
to clipboardprint?
#include <qapplication.h>
#include "form2.h"
int main( int argc,char ** argv )
{
QApplication a( argc, argv );
Form2 w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
view plaincopy to clipboardprint? /**************************************************************************** ** Form implementation generated from reading ui file 'form2.ui' ** ** Created by: The User Interface Compiler ($Id: qt/main.cpp 3.3.6 edited Aug 31 2005 $) ** ** WARNING! All changes made in this file will be lost! ****************************************************************************/ #include "form2.h" #include <qvariant.h> #include <qlcdnumber.h> #include <qlayout.h> #include <qtooltip.h> #include <qwhatsthis.h> #include <qimage.h> #include <qpixmap.h> #include<qtimer.h> #include<qdatetime.h> #include <qpoint.h> /* * Constructs a Form2 as a child of 'parent', with the * name 'name' and widget flags set to 'f'. * * The dialog will by default be modeless, unless you set 'modal' to * TRUE to construct a modal dialog. */ DigiClock::DigiClock(QWidget * parent, const char * name) : QLCDNumber(parent,name) { QPalette p = palette(); p.setColor(QPalette::Normal, QColorGroup::Background, Qt::blue); setPalette(p); QTimer *timer = new QTimer(this); connect(timer,SIGNAL(timeout()),SLOT(showTime())); timer->start(1000); showColon = true; showTime(); } void DigiClock::showTime() { QTime time = QTime::currentTime(); QString text = time.toString("hh:mm"); if(showColon) { text[2] = ':'; showColon = false; } else { text[2] = ' '; showColon = true; } display(text); } Form2::Form2( QWidget* parent, const char* name, bool modal, WFlags fl ) : QDialog( parent, name, modal, fl ) { setMouseTracking (true); if ( !name ) setName( "Form2" ); lCDNumber2 = new DigiClock( this, "lCDNumber2"); lCDNumber2->setGeometry( QRect( 0, 0, 370, 70 ) ); languageChange(); resize( QSize(369, 70).expandedTo(minimumSizeHint()) ); clearWState( WState_Polished ); LbuttonFlag = false; } /* * Destroys the object and frees any allocated resources */ Form2::~Form2() { // no need to delete child widgets, Qt does it all for us } /* * Sets the strings of the subwidgets using the current * language. */ void Form2::languageChange() { setCaption( tr( "Form2" ) ); } void Form2::mousePressEvent(QMouseEvent *e) { if(e->button()==QMouseEvent::LeftButton) { dragPositon = e->globalPos()-frameGeometry().topLeft(); LbuttonFlag = true; e->accept(); } } void Form2::mouseReleaseEvent(QMouseEvent * e) { LbuttonFlag = false; } void Form2::mouseMoveEvent(QMouseEvent *e) { if(LbuttonFlag) { move(e->globalPos()-dragPositon); qDebug("gx=%d,gy%d,x=%d,y=%d",e->globalX(),e->globalY(),dragPositon.x(),dragPositon.y()); e->accept(); } } /**************************************************************************** ** Form implementation generated from reading ui file 'form2.ui' ** ** Created by: The User Interface Compiler ($Id: qt/main.cpp 3.3.6 edited Aug 31 2005 $) ** ** WARNING! All changes made in this file will be lost! ****************************************************************************/ #include "form2.h" #include <qvariant.h> #include <qlcdnumber.h> #include <qlayout.h> #include <qtooltip.h> #include <qwhatsthis.h> #include <qimage.h> #include <qpixmap.h> #include<qtimer.h> #include<qdatetime.h> #include <qpoint.h> /* * Constructs a Form2 as a child of 'parent', with the * name 'name' and widget flags set to 'f'. * * The dialog will by default be modeless, unless you set 'modal' to * TRUE to construct a modal dialog. */ DigiClock::DigiClock(QWidget * parent, const char * name) : QLCDNumber(parent,name) { QPalette p = palette(); p.setColor(QPalette::Normal, QColorGroup::Background, Qt::blue); setPalette(p); QTimer *timer = new QTimer(this); connect(timer,SIGNAL(timeout()),SLOT(showTime())); timer->start(1000); showColon = true; showTime(); } void DigiClock::showTime() { QTime time = QTime::currentTime(); QString text = time.toString("hh:mm"); if(showColon) { text[2] = ':'; showColon = false; } else { text[2] = ' '; showColon = true; } display(text); } Form2::Form2( QWidget* parent, const char* name, bool modal, WFlags fl ) : QDialog( parent, name, modal, fl ) { setMouseTracking (true); if ( !name ) setName( "Form2" ); lCDNumber2 = new DigiClock( this, "lCDNumber2"); lCDNumber2->setGeometry( QRect( 0, 0, 370, 70 ) ); languageChange(); resize( QSize(369, 70).expandedTo(minimumSizeHint()) ); clearWState( WState_Polished ); LbuttonFlag = false; } /* * Destroys the object and frees any allocated resources */ Form2::~Form2() { // no need to delete child widgets, Qt does it all for us } /* * Sets the strings of the subwidgets using the current * language. */ void Form2::languageChange() { setCaption( tr( "Form2" ) ); } void Form2::mousePressEvent(QMouseEvent *e) { if(e->button()==QMouseEvent::LeftButton) { dragPositon = e->globalPos()-frameGeometry().topLeft(); LbuttonFlag = true; e->accept(); } } void Form2::mouseReleaseEvent(QMouseEvent * e) { LbuttonFlag = false; } void Form2::mouseMoveEvent(QMouseEvent *e) { if(LbuttonFlag) { move(e->globalPos()-dragPositon); qDebug("gx=%d,gy%d,x=%d,y=%d",e->globalX(),e->globalY(),dragPositon.x(),dragPositon.y()); e->accept(); } }
转载自:/article/2459277.html
相关文章推荐
- Qt事件--数字时钟
- QT 文件拖放事件dropEvent和dragEnterEvent
- QT事件处理 (一)之 重载事件处理器
- Qt 多线程之逐线程事件循环 下篇
- QT编写DLL给外部程序调用,提供VC/C#/C调用示例(含事件)good
- 自学QT之QML实现响应鼠标和键盘事件
- Qt中使用ActiveX(二)【方法调用和事件处理】
- Qt中的键盘事件
- qt鼠标事件总结
- [GUI] QT事件与X11的关系
- Qt事件系统基本概念
- Qt 学习之路:事件总结
- Qt键盘事件
- Qt事件处理的5个层次
- Qt之QEvent(所有事件的翻译)
- Qt鼠标响应事件
- [Q学习]9 Qt事件系统——投递事件
- 转:QT中窗口刷新事件的学习总结
- QT学习之阻拦退出程序函数以及事件过滤器的使用,以及文件的操作。
- QT鼠标事件