Qt 포인터 시계 구현 Qt 동적 시계 구현

9220 단어 Qt클 록
본 논문 의 사례 는 Qt 가 지침 식 시계,동적 시 계 를 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
먼저 위의 그림:

실행 을 누 르 면 먼저 포인터 시계 창 입 니 다.Digital Clock->을 누 르 면 디지털 시계 창 으로 이동 할 수 있 습 니 다.Move Clock->를 누 르 면 포인터 시계 창 으로 복원 할 수 있 습 니 다.
전체 프로그램 에 대한 설명 은 코드 주석 에 있 습 니 다.상세 합 니 다~
개요:
저 는 두 개의 창 을 설계 합 니 다.하나의 메 인 창 은 하나의 키 창 입 니 다.버튼+신호 와 슬롯 체 제 를 이용 하여 두 창의 상호 전환 을 실현 합 니 다.그 중에서 메 인 창 은 포인터 시 계 를 표시 하고 세 가지 기본 적 인 요 구 를 완성 합 니 다.
1.시스템 시 계 를 정확하게 표시 합 니 다.
2.시계의 눈금 과 시간 초침 의 위 치 를 정확하게 정할 수 있다.
3.창 크기 에 따라 변화 할 수 있 습 니 다. 
주 창의 실현 에 대해 서 는 먼저 Qt 자체 시간 함수 QTime:currentTime()을 이용 하여 시스템 시간 을 가 져 온 다음 paintEvent(QPaintEvent*)함 수 를 이용 하여 가 져 온 시스템 시간 에 따라 시침,분침,초침 의 그림 을 그리고 해당 하 는 시간 눈금 선,분 눈금 선,초 각 을 그립 니 다. 도선 은 기본 시계의 스타일 을 실현 하고 마지막 으로 scale()함 수 를 넣 어 상응하는 비례 크기 를 조정 하여 시계 가 창의 크기 에 따라 변화 할 수 있 도록 한다.
하위 창 에 대해 서 는 내 가 추가 한 모듈 입 니 다.전자 시계 입 니 다.lcd 액정 표시 장치 로'시:분:초'형식 으로 현재 시간 을 표시 합 니 다.마찬가지 로 자체 시간 함수 인 QTime:currentTime()을 이용 하여 현재 시간 을 가 져 오고 lcd 를 통 해 표시 합 니 다.마지막 으로 두 창 을 두 단 추 를 통 해 연결 하여 서로 전환 하 는 기능 을 실현 합 니 다.
main window.h(주 창)

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include<QLCDNumber>
#include<QLabel>
#include<sub.h>
#include<QPushButton>
 
QT_BEGIN_NAMESPACE
QT_END_NAMESPACE
 
class MainWindow : public QMainWindow
{
 Q_OBJECT
 
public:
 MainWindow(QWidget *parent = nullptr);//    
 ~MainWindow();//    
 void paintEvent(QPaintEvent *);//     
public:
 void dealsub();//      
 void changeback();//      
private:
 sub w;//   
 QPushButton b;//  
};
#endif // MAINWINDOW_H
main window.cpp(주 창)

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPainter>
#include<QPen>
#include<QTime>
#include<QTimer>
#include<QLabel>
#include<QPushButton>
#include<QLCDNumber>
MainWindow::MainWindow(QWidget *parent)
 : QMainWindow(parent)
{
 setWindowIcon(QIcon(":/new/prefix1/v2-d858191577356128b31c88e186eea0db_r.jpg"));//    
 QTimer *timer = new QTimer(this);//       
 connect(timer, SIGNAL(timeout()), this, SLOT(update()));//          
 timer->start(1000);//     ,        
 resize(600,600);//    
 b.setParent(this);//       
 b.setGeometry(0,0,160,40);//      
 b.setText("Digital clock->");//      
 b.setStyleSheet("QPushButton{background-color: rgba(205,214,216,0);color:rgb(0,0,0);}");//      ,rgba      rgb,            ,0~1  
 connect(&b,&QPushButton::released,this,&MainWindow::dealsub);//       ,          
 void(sub::*funsignal)()=&sub::mysignal;
 connect(&w,funsignal,this,&MainWindow::changeback);//      ,            
}
 
MainWindow::~MainWindow()
{
 
}
 
void MainWindow::paintEvent(QPaintEvent *)
{
 static const QPoint hour[3] = {
  QPoint(14, 15),
  QPoint(-14, 15),
  QPoint(0, -110)
 };
 static const QPoint minute[3] = {
  QPoint(11, 13),
  QPoint(-11, 13),
  QPoint(0, -170)
 };
 static const QPoint second[3] = {
  QPoint(7, 8),
  QPoint(-7, 8),
  QPoint(0, -210)
 };
 int size=qMin(width(),height());
 QTime time=QTime::currentTime();//        
 QPainter p(this);//      
 p.setRenderHint(QPainter::Antialiasing);//      
 p.translate(width()/2,height()/2);//        
 p.scale(size/600.0,size/600.0);//  
 
 QBrush brush;//    
 brush.setColor(QColor(245,182,96));//      
 brush.setStyle(Qt::SolidPattern);//    
 
 
 QPen pen;//    
 pen.setWidth(18);//      
 pen.setColor(QColor(205,214,216));//rgb    
 pen.setStyle(Qt::SolidLine);//    
 p.setPen(pen);//       
 p.drawEllipse(QPoint(0,0),280,280);//  
 pen.setColor(Qt::white);
 pen.setWidth(160);//      
 p.setPen(pen);//       
 p.drawEllipse(QPoint(0,0),160,160);//  
 //   
 p.setBrush(brush);//       
 p.setPen(Qt::NoPen);
 p.save();//      
 p.rotate(30.0*(time.hour()+time.minute()/60.0));//    ,        ,              
 p.drawConvexPolygon(hour,3);//         ,          ,          
 p.restore();//          , save()    
 
 //     
 pen.setStyle(Qt::SolidLine);
 pen.setWidth(5);
 pen.setColor(Qt::black);
 p.setPen(pen);
 for(int i=0;i<12;i++)
 {
  p.drawLine(0,268,0,276);//    
  p.drawText(-5,-235,QString::number(i));//     
  p.rotate(30);//      30 
 }
 
 //   
 p.setPen(Qt::NoPen);
 p.setBrush(QColor(144,199,247));
 p.save();//      
 p.rotate(6.0*(time.minute()+time.second()/60.0));//           
 p.drawConvexPolygon(minute,3);//         ,          ,          
 p.restore();//          , save()    
 
 //     
 pen.setStyle(Qt::SolidLine);
 pen.setColor(QColor(0,0,0));
 pen.setWidth(1);
 p.setPen(pen);
 for(int i=0;i<60;i++)
 {
  if((i%5)!=0)
  p.drawLine(0,265,0,276);//5      ,      
  p.rotate(6);//      6 
 }
 
 //   
 p.setPen(Qt::NoPen);
 p.setBrush(QColor(119,217,175));
 p.save();
 p.rotate(6*time.second());//           
 p.drawConvexPolygon(second, 3);//         ,          ,          
 p.restore();
 
 //   
 p.setBrush(Qt::black);
 p.setPen(Qt::white);
 p.save();
 p.drawEllipse(QPoint(0,0),3,3);//   
 p.restore();
 
 //        
 p.setPen(Qt::black);
 if(time.hour()>=12)
 p.drawText(-6,-50,"PM");//    
 else
 p.drawText(-6,-50,"AM");//    
 p.drawText(-60,-130,"Made By ZSR");//    
}
 
void MainWindow::dealsub()
{
 w.show();//     
 this->hide();//     
}
 
void MainWindow::changeback()
{
 w.hide();//     
 this->show();//     L
}
sub.h(하위 창)

#ifndef SUB_H
#define SUB_H
 
#include <QMainWindow>
#include<QPushButton>
#include<QLCDNumber>
class sub : public QMainWindow
{
 Q_OBJECT
public:
 explicit sub(QWidget *parent = nullptr);
 void sentsignal();//    
 void paintEvent(QPaintEvent *event);//     
signals://  
 void mysignal();
public slots:// 
 void showtime();//      
private:
 QPushButton b1;//  
 QLCDNumber *lcd;//lcd
};
 
#endif // SUB_H
sub.cpp(하위 창)

#include "sub.h"
#include<QTime>
#include<QTimer>
#include<QLCDNumber>
#include<QPainter>
sub::sub(QWidget *parent) : QMainWindow(parent)
{
 setWindowIcon(QIcon(":/new/prefix1/f56513788384645db768d0ec542dec33_r.jpg"));//    
 this->setWindowTitle("Digital clock");//      
 this->resize(900,500);//      
 b1.setParent(this);//       
 b1.setText("Move clock->");//      
 b1.setGeometry(0,0,140,40);//      
 b1.setStyleSheet("QPushButton{background-color: rgba(205,214,216,0);color:rgb(0,0,0);}");//      
 connect(&b1,&QPushButton::clicked,this,&sub::sentsignal);//      ,                 ,     ,   changeback()  ,         
 QTimer *timer1=new QTimer(this);////       
 timer1->start(1000);//     ,          
 connect(timer1,SIGNAL(timeout()),this,SLOT(showtime()));//          ,1s        
 lcd=new QLCDNumber();//    lcd     
 lcd->setSegmentStyle(QLCDNumber::Filled);//       
 lcd->setParent(this);//        
 lcd->move(0,50);//       
 lcd->setDigitCount(8);//         8 
 lcd->resize(200,50);//       
 showtime();//    
}
 
void sub::sentsignal()//    
{
 emit mysignal();
}
 
void sub::showtime()
{
 QTime time1=QTime::currentTime();//      
 QString text=time1.toString("hh:mm:ss");//        
 if((time1.second()%2)==0)
  text[5]=' ';// 2s      
 lcd->display(text);//lcd    
}
 
void sub::paintEvent(QPaintEvent *event)//
{
 QPainter p(this);//        
 p.drawPixmap(rect(),QPixmap(":/new/prefix1/f8fa6c0b00b51e33e8949627d52942ea.jpg"));//     
}
main.cpp(주 함수)

#include "mainwindow.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 MainWindow w;//       
 w.setWindowTitle("Move clock");//       
 w.show();//     
 return a.exec();
}
THE END
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기