Qt编程指南 ■ QTimeEvent ■ Qtimer ■ QDateTimeEdit ■ QDateTime ■ ■
■ QTimeEvent
1. 启动定时器
定时器ID startTimer ( 时间间隔) ;
int idt = startTimer ( 250 ) ;
每隔指定的时间间隔,触发一次定时器事件。
2. 定时器事件处理
virtual void timerEvent ( QTimeEvent* envet) ;
每当定时器事件触发时,该虚函数被自动调用。
通过QTimeEvent :: timerId ( ) 区分不同的定时器。
3. 关闭定时器
killTimer ( 定时器ID) ;
■ Qtimer
timerLiquid = new QTimer ( this ) ;
timerLiquid-> start ( 60000 ) ;
connect ( timerLiquid, SIGNAL ( timeout ( ) ) , this , SLOT ( slots_timerLiquidHandle ( ) ) ) ;
void MainWindow :: slots_timerLiquidHandle ( )
{ static float value = 0.0001f ; for ( int i= 1 ; i<= PUMPMAXNUMBER; i++ ) { value += PumpVect[ i] -> getTotalVolume ( ) ; } emit emit_TotalVolumeToChart ( value+= 1 ) ;
}
private slots: void slots_timerLiquidHandle ( ) ;
public slots: void slots_timerLiquidHandle ( ) ; timer1 = new QTimer ( this ) ; timer1-> start ( 1000 ) ; connect ( timer1, & QTimer:: timeout, [ = ] ( ) { pumpALeve = m_alarmInfo. Level ( ) ; setAlarmBackColour ( pumpALeve) ; setAlarmString ( getRoundAlarmStr ( pumpALeve) ) ; } ) ;
QTimer * timer = new QTimer ( this ) ;
connect ( timer, SIGNAL ( timeout ( ) ) , this , SLOT ( processOneThing ( ) ) ) ;
timer-> start ( ) ;
这时,processOneThing ( ) 将会被重复调用并且应该很快返回(通常在处理一个数据项之后),这样Qt可以把事件传送给窗口部件,并且一旦它完成这个工作就停止这个定时器。这是在图形用户界面应用程序中实现繁重的工作的一个典型方法QTimer * timer = new QTimer ( this ) ;
connect ( timer, SIGNAL ( timeout ( ) ) , this , SLOT ( update ( ) ) ) ;
timer-> start ( 1000 ) ; QTimer :: singleShot ( 100 , this , SLOT ( initData ( ) ) ) ;
QTimer :: singleShot ( 200 , this , SLOT ( updateCaption ( ) ) ) ;
可以通过设置setSingleShot ( true ) 来让定时器只执行一次。也可以使用静态函数QTimer :: singleShot ( ) :
class Widget2 : public QWidget
{ Q_OBJECT
public : Widget2 ( QWidget* parent = 0 ) : QWidget ( parent) , timeID ( 0 ) { QPalette palette ( this -> palette ( ) ) ; palette. setBrush ( QPalette:: Background, QBrush ( QColor ( 100 , 12 , 130 ) ) ) ; this -> setPalette ( palette ) ; timeID = startTimer ( 100 , Qt:: PreciseTimer) ; } ~ Widget2 ( ) { }
protected : void timerEvent ( QTimerEvent * event) { if ( event-> timerId ( ) == timeID) { if ( timeID) killTimer ( timeID) ; timeID = 0 ; } }
private : int timeID;
} ;
■ QDateTimeEdit
dateTimeEdit = new QDateTimeEdit ( QDateTime :: currentDateTime ( ) , this ) ;
dateTimeEdit-> setGeometry ( 300 , 200 , 200 , 30 ) ;
timeEdit = new QTimeEdit ( QTime :: currentTime ( ) , this ) ;
timeEdit-> setGeometry ( 300 , 240 , 200 , 30 ) ;
dateEdit = new QDateEdit ( QDate :: currentDate ( ) , this ) ;
dateEdit-> setGeometry ( 300 , 280 , 200 , 30 ) ;
QDateTimeEdit. setDisplayFormat ( ) #指定显示格式
QDateTimeEdit{
background: transparent;
border: 1 px solid rgb ( 82 , 143 , 199 ) ;
border- radius: 2 px;
min- height: 26 px;
max- height: 26 px;
} QDateTimeEdit:: up- arrow{
image: url ( : / commonImg/ Resources/ image/ common/ upArrow. png) ;
} QDateTimeEdit:: down- arrow {
image: url ( : / commonImg/ Resources/ image/ common/ downArrow. png) ;
}
QDateTimeEdit{
color: rgb ( 255 , 255 , 255 ) ;
background: transparent;
border: 1 px solid rgb ( 82 , 143 , 199 ) ;
border- radius: 5 px;
} 用qt 方式修改系统时间ui-> dateTimeEdit-> setProperty ( "noinput" , true ) ;
ui-> dateTimeEdit-> setDisplayFormat ( "yyyy-MM-dd HH:mm" ) ;
ui-> dateTimeEdit-> setDateTime ( QDateTime :: currentDateTime ( ) ) ; QObject :: connect ( ui-> dateTimeEdit, & QDateTimeEdit:: dateTimeChanged, [ & ] ( const QDateTime & dateTime) { QString dateTimeStr = dateTime. toString ( "yyyy-MM-dd HH:mm:ss" ) ; QString cmd = QString ( "date -s \"%1\"" ) . arg ( dateTimeStr) ; qDebug ( ) << "--------" << cmd; QProcess :: execute ( cmd) ; } ) ; C语言 使用系统调用方式修改系统时间
void User :: uphwclock ( int y, int m, int d, int h, int min, int s)
{ char data[ 100 ] ; sprintf ( data, "date -s \"%d-%d-%d %d:%d:%d\"" , y, m, d, h, min, s) ; # if __arm__ system ( data) ; system ( "hwclock -w" ) ;
# endif
}
■ QDateTime
qint64 m_cursec = QDateTime :: currentDateTime ( ) . toMSecsSinceEpoch ( ) ;
QDate dd = QDateTime :: fromString ( "2023/6/6 10:6:10" , "yyyy/MM/dd hh:mm:ss" ) . date ( ) ;
■
■