Qt学习之旅---2.绘图,时钟小例子

时间:2022-03-14 00:30:01

学习笔记,小白可以相互学习,大佬看到能告诉咱理解不对的地方就好了。


/*********.h**********************************/
#ifndef WIDGET_H
#define WIDGET_H

#include<QWidget>
#include<QPaintEvent>
#include<QPainter>
#include<QPen>
#include<QTimer>
#include<iostream>
#include<QLabel>

class Widget : public QWidget
{
Q_OBJECT

public:
Widget(QWidget *parent = 0);
~Widget();
protected:
void paintEvent(QPaintEvent *event)
{
setFixedSize(500,500);
static int i = 0;
QPainter ppp(this);//定义一个画家
QPen p(QColor(Qt::red));//画笔,颜色红
p.setWidth(4);//粗细10

ppp.setPen(p);//画家拿到笔
ppp.translate(200,200);//移动中心点(移动画家的中心点位置)

//ppp.drawEllipse(QPoint(0,0),100,100);//以画家为中心点,画椭圆
ppp.rotate(i*6);//转多少度,参数为转的度数
i++;
ppp.drawLine(0,0,0,-160);//画线
//ppp.drawText(0,0,"小可爱");

static int j = 0;
QPainter ppp1(this);
QPen p1(QColor(Qt::blue));
p1.setWidth(4);
ppp1.setPen(p1);
ppp1.translate(200,200);
ppp1.rotate(j*0.5);
j++;
ppp1.drawLine(0,0,0,-120);

static int k = 0;
QPainter ppp3(this);
QPen p3(QColor(Qt::green));
p3.setWidth(6);
ppp3.setPen(p3);
ppp3.translate(200,200);
ppp3.rotate(k*0.25/60);
k++;
ppp3.drawLine(0,0,0,-80);

QPainter ppp4(this);
QPen p4(QColor(Qt::black));
p4.setWidth(4);
ppp4.setPen(p4);
ppp4.translate(200,200);
ppp4.drawEllipse(QPoint(0,0),190,190);

QPainter ppp2(this);
QPen p2(QColor(Qt::black));
p2.setWidth(20);
ppp2.setPen(p4);
ppp2.translate(200,200);
ppp2.drawPoint(0,0);

QPainter ppp5(this);
QPen p5(QColor(Qt::black));
p5.setWidth(2);
ppp5.setPen(p5);
ppp5.translate(350,350);
ppp5.translate(rand()%20,rand()%20);
ppp5.drawPixmap(0,0,QPixmap("ooopic_1504102052.ico"));
// ppp5.scale(0,0);//放大

QPainter ppp6(this);
QPen p6(QColor(Qt::black));
p6.setWidth(4);
ppp6.setPen(p6);
ppp6.translate(200,200);
int a;
QString b;
for(a = 0; a < 12 ;a++)
{
b = QString::number(a);
ppp6.drawLine(0,-180,0,-190);
ppp6.drawText(0,-165,b);
ppp6.rotate(30);
}

QPainter ppp7(this);
QPen p7(QColor(Qt::blue));
p7.setWidth(2);
ppp7.setPen(p7);
ppp7.translate(200,200);
for(a = 0; a < 60 ;a++)
{
ppp7.drawLine(0,-183,0,-190);
ppp7.rotate(6);
}

// 求时间数字
sec = i%60 - 1;
min = i/60%60;
hour = i/3600%24;
std::cout<<":"<<hour<<" :"<<min<<" :"<<" :"<<sec<<std::endl;

QString x,y,z;
x = QString::number(hour);
y = QString::number(min);
z = QString::number(sec);

QPainter ppp8(this);
QPen p8(QColor(Qt::red));
p8.setWidth(30);
ppp8.setPen(p8);
ppp8.translate(0,480);
ppp8.drawText(0,0,x);
ppp8.drawText(20,0," : ");
ppp8.drawText(40,0,y);
ppp8.drawText(60,0," : ");
ppp8.drawText(80,0,z);

}
public slots:
void paint()
{
update();//人工调用
}


private:
QTimer *time;
int sec,min,hour;
QLabel *lab;
};

#endif // WIDGET_H


/********.cpp**********************************/
#include "widget.h"

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
time = new QTimer;
time->start(1000);

connect(time,SIGNAL(timeout()),SLOT(paint()));

}

Widget::~Widget()
{

}


小时钟:

实现了鼠标键盘滚轮事件改变其时间


/********myclock.h*****************************/

#ifndef MYCLOCK_H
#define MYCLOCK_H

#include <QObject>
#include <QWidget>
#include <QTimer>

class myclock : public QWidget
{
Q_OBJECT
public:
explicit myclock(QWidget *parent = nullptr);

signals:

public slots:
void setvalue(int);
void paint()
{
update();
}
protected:
void paintEvent(QPaintEvent *event);

private:
// int pos;
int s;
QTimer *time;
};

#endif // MYCLOCK_H

/*************widget.h***************************************/

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QSlider>
#include"myclock.h"
#include<QMouseEvent>
#include<QKeyEvent>
#include<QWheelEvent>
#include<QTimer>
#include<QLCDNumber>

class Widget : public QWidget
{
Q_OBJECT

public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void shownum(int num);

private:
myclock *clock;
QSlider *sd;
int click;
QLCDNumber *lcd;

protected:
void mousePressEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
void keyPressEvent(QKeyEvent *event);

};

#endif // WIDGET_H

/******************myclock.cpp********************/

#include "myclock.h"
#include<QWidget>
#include<QPaintEvent>
#include<QPainter>
#include<QString>
#include<QDebug>
#include<QBrush>

myclock::myclock(QWidget *parent) : QWidget(parent)
{
//update();
s = 0;
time = new QTimer;
time->start(1000);
connect(time,SIGNAL(timeout()),SLOT(paint()));
}

void myclock::setvalue(int i)
{
s = i;
update();
}

void myclock::paintEvent(QPaintEvent *event)
{
setFixedSize(600,600);
QPainter pp(this);
QBrush b(QColor(Qt::yellow));
QPen p(QColor(Qt::black));
p.setWidth(4);
pp.setPen(p);
pp.translate(this->width()/2,this->height()/2);
pp.drawEllipse(QPoint(0,0),200,200);

QPainter pp1(this);
QPen p1(QColor(Qt::black));
p1.setWidth(4);
pp1.setPen(p1);
pp1.translate(this->width()/2,this->height()/2);
for(int i = 0;i < 12 ;i++)
{
pp1.drawLine(0,-190,0,-200);
pp1.drawText(0,-170,QString::number(i));
pp1.rotate(30);
}

QPainter pp2(this);
QPen p2(QColor(Qt::black));
p2.setWidth(2);
pp2.setPen(p2);
pp2.translate(this->width()/2,this->height()/2);
for(int j = 0;j < 60 ;j++)
{
pp2.drawLine(0,-193,0,-200);
pp2.rotate(6);
}

QPainter pp5(this);
QPen p5(QColor(Qt::green));
p5.setWidth(6);
pp5.setPen(p5);
pp5.translate(this->width()/2,this->height()/2);
pp5.rotate(s*0.25/60);
pp5.drawLine(0,0,0,-70);

QPainter pp4(this);
QPen p4(QColor(Qt::blue));
p4.setWidth(5);
pp4.setPen(p4);
pp4.translate(this->width()/2,this->height()/2);
pp4.rotate(s*0.5);
pp4.drawLine(0,0,0,-120);

QPainter pp3(this);//定义一个画家
QPen p3(QColor(Qt::red));//画笔,颜色红
p3.setWidth(4);//粗细10
pp3.setPen(p3);//画家拿到笔
pp3.translate(this->width()/2,this->height()/2);
pp3.rotate(s*6);
pp3.drawLine(0,0,0,-170);
s++;
qDebug()<<"s ="<<s;

}



/************widget.cpp************************************************/

#include "widget.h"
#include <QVBoxLayout>
#include<QDebug>
int now = 0;

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
click = 0;

lcd = new QLCDNumber;
lcd->setMinimumHeight(45);

sd = new QSlider(this);
sd->setOrientation(Qt::Horizontal);
sd->setRange(0,864399);

clock = new myclock(this);
clock->setFixedSize(300,300);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(clock);
vbox->addWidget(lcd);
vbox->addWidget(sd);
setLayout(vbox);

connect(sd,SIGNAL(valueChanged(int)),clock,SLOT(setvalue(int)));
connect(sd,SIGNAL(valueChanged(int)),this,SLOT(shownum(int)));

}

void Widget::shownum(int num)
{
now = num;
lcd->display(num);
}

void Widget::mousePressEvent(QMouseEvent *event)
{
if( Qt::LeftButton == event->button())
{
click++;
int c = click + now;
sd->setValue(c);
if(c == 864399)
{
click = 0;
sd->setValue(1*click);
}
}
else if( Qt::RightButton == event->button())
{
click--;
sd->setValue(1*click);
if(click == 0)
{
click = 359;
sd->setValue(1*click);
}
}

}

void Widget::wheelEvent(QWheelEvent *event)
{
if( Qt::WA_TranslucentBackground == event->delta())
{
click++;
sd->setValue(1*click);
if(click == 359)
{
click = 0;
sd->setValue(1*click);
}
}
else if( -120 == event->delta())
{
click--;
sd->setValue(1*click);
if(click == 0)
{
click = 359;
sd->setValue(1*click);
}
}

}

void Widget::keyPressEvent(QKeyEvent *event)
{

if( (Qt::Key_Up == event->key()) || (Qt::Key_Right == event->key()))
{
click++;
sd->setValue(1*click);
if(click == 359)
{
click = 0;
sd->setValue(1*click);
}
}
else if( (Qt::Key_Down == event->key()) || (Qt::Key_Left == event->key()))
{
click--;
sd->setValue(1*click);
if(click == 0)
{
click = 359;
sd->setValue(1*click);
}
}

}


Widget::~Widget()
{

}