stage内容
- 画图
- 使用定时器
- 系统事件(键盘,鼠标,拖拉)
1、画图
头文件
#include <QPainter>
#include <QPaintEvent>
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void draw1();
protected:
void paintEvent(QPaintEvent *e);
widget.cpp
void Widget::draw1()
{
QPainter painter(this);
painter.setPen(QPen(Qt::green,5));
painter.drawLine(100,100,500,500);
painter.setPen(QPen(Qt::blue,3));
painter.drawLine(QPoint(100,200),QPoint(500,600));
painter.drawRect(500,10,280,160);
painter.setPen(QPen(Qt::yellow,3));
painter.drawRect(500,180,160,160);
painter.setBrush(Qt::yellow);
painter.drawEllipse(QRect(500,10,280,160));
painter.setBrush(Qt::green);
painter.drawEllipse(QRect(500,180,160,160));
}
void Widget::paintEvent(QPaintEvent *e)
{
draw1();
}
2、定时器
头文件
定时器1 周期性画园
#include <QTimer>
public:
explicit Widget(QWidget *parent = 0);
~Widget();
QTimer *timer;
int radius;
private slots:
void timerdo();
widget.cpp
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
radius=40;
timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(timerdo()));
timer->start(1000);
}
Widget::~Widget()
{
timer->stop();
delete ui;
}
void Widget::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
painter.setPen(QPen(Qt::blue,3));
radius=(radius+40)%240;
painter.drawEllipse(QPoint(400,300),radius,radius);
}
void Widget::timerdo()
{
update();
}
效果图
定时器2 在图片上周期性画圆、写文本
头文件
#include <QTimer>
#include <QImage>
#include <QPixmap>
public:
explicit Widget(QWidget *parent = 0);
~Widget();
QTimer *timer;
int radius;
QPainter pt;
QImage img;
private slots:
void timerdo();
void on_btn_start_clicked();
void on_btn_stop_clicked();
widget.cpp
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
radius=40;
timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(timerdo()));
img=QImage(400,300,QImage::Format_ARGB32);
img.fill(0);
}
Widget::~Widget()
{
if(timer->isActive())
timer->stop();
delete ui;
}
void Widget::timerdo()
{
ui->label->clear();
img.fill(0);
pt.begin(&img);
pt.setPen(QPen(Qt::green,5));
radius=(radius+20)%240;
QString ss = "the num is:"+QString::number(radius);
pt.drawText(QPoint(20,20),ss);
pt.setPen(QPen(Qt::blue,3));
pt.drawEllipse(QPoint(200,150),radius,radius);
pt.end();
ui->label->setPixmap(QPixmap::fromImage(img));
}
void Widget::on_btn_start_clicked()
{
timer->start(1000);
}
void Widget::on_btn_stop_clicked()
{
timer->stop();
ui->label->clear();
}
效果图
定时器3 在图片上周期性画圆、写文本、画随机柱状图
头文件
#include <QPainter>
#include <QPaintEvent>
#include <QTimer>
#include <QImage>
#include <QPixmap>
#include<QTime>
#include<QThread>
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void draw1();
QTimer *timer,*mt;
int radius;
QPainter pt,pt1;
QImage img,timg;
QTime t;
int hei;
int x1,y1,w,h;
private slots:
void timerdo();
void mtdo();
void on_btn_start_clicked();
void on_btn_stop_clicked();
widget.cpp
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
radius=40;
timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(timerdo()));
mt = new QTimer();
connect(mt,SIGNAL(timeout()),this,SLOT(mtdo()));
img=QImage(400,300,QImage::Format_ARGB32);
timg=QImage(400,300,QImage::Format_ARGB32);
}
void Widget::timerdo()
{
// update();
ui->label1->clear();
img.fill(0);
pt.begin(&img);
pt.setPen(QPen(Qt::green,5));
radius=(radius+20)%240;
QString ss = "the num is:"+QString::number(radius);
pt.drawText(QPoint(20,20),ss);
pt.setPen(QPen(Qt::blue,3));
pt.drawEllipse(QPoint(200,150),radius,radius);
pt.end();
ui->label1->setPixmap(QPixmap::fromImage(img));
}
void Widget::mtdo()
{
ui->label2->clear();
timg.fill(0);
pt1.begin(&timg);
for(int i=0; i<5; i++)
{
t=QTime::currentTime();
qsrand(t.msec()+t.second()*1000);
hei = rand()%201;
if(hei < 120)
{
pt1.setPen(QPen(Qt::red,1));
pt1.setBrush(QBrush(Qt::red));
}else
{
pt1.setPen(QPen(Qt::green,1));
pt1.setBrush(QBrush(Qt::green));
}
x1=20+i*80;
w=40;
y1=300-hei;
pt1.drawRect(x1,y1,w,hei);
pt1.setPen(QPen(Qt::blue,3));
pt1.drawText(x1,60,QString::number(hei));
QThread::msleep(50);
}
pt1.end();
ui->label2->setPixmap(QPixmap::fromImage(timg));
}
void Widget::on_btn_start_clicked()
{
timer->start(1000);
mt->start(500);
}
void Widget::on_btn_stop_clicked()
{
timer->stop();
mt->stop();
ui->label1->clear();
ui->label2->clear();
}
效果图
3、鼠标事件
头文件
#include <QMouseEvent>
#include <QMouseEventTransition>
#include <QColor>
#include <QColorDialog>
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void draw1();
QPainter pt,pt1;
int shape_type;
int pen_size;
QColor pen_color;
QPoint sp,ep;
QImage t_img;
int weight,height;
void drawShape();
void drawLine();
void drawRect();
void drawCircle();
private slots:
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void on_cbx_shape_currentIndexChanged(int index);
void on_cbx_pensize_currentIndexChanged(int index);
void on_btn_color_clicked();
widget.cpp
void Widget::mousePressEvent(QMouseEvent *e)
{
sp=e->pos();
}
void Widget::mouseMoveEvent(QMouseEvent *e)
{
ep=e->pos();
drawShape();
}
void Widget::mouseReleaseEvent(QMouseEvent *e)
{
ep=e->pos();
drawShape();
}
void Widget::drawShape()
{
if(shape_type < 1) return;
if(shape_type == 1) drawLine();
if(shape_type == 2) drawRect();
if(shape_type == 3) drawCircle();
}
void Widget::on_cbx_shape_currentIndexChanged(int index)
{
shape_type=index;
ui->lab3->clear();
}
void Widget::drawLine()
{
ui->lab3->clear();
t_img.fill(0);
pt.begin(&t_img);
pt.setPen(QPen(pen_color,pen_size));
if(ep.x() - sp.x() > 10 || ep.y() - sp.y() > 10)
{
pt.drawLine(sp,ep);
}
pt.end();
ui->lab3->setPixmap(QPixmap::fromImage(t_img));
}
void Widget::drawRect()
{
ui->lab3->clear();
t_img.fill(0);
pt.begin(&t_img);
pt.setPen(QPen(pen_color,pen_size));
if(ep.x() - sp.x() > 10 || ep.y() - sp.y() > 10)
{
weight = ep.x()-sp.x();
height = ep.y()-sp.y();
pt.drawRect(QRect(sp.x(),sp.y(),weight,height));
}
pt.end();
ui->lab3->setPixmap(QPixmap::fromImage(t_img));
}
void Widget::drawCircle()
{
ui->lab3->clear();
t_img.fill(0);
pt.begin(&t_img);
pt.setPen(QPen(pen_color,pen_size));
if(ep.x() - sp.x() > 10 || ep.y() - sp.y() > 10)
{
weight = ep.x()-sp.x();
height = ep.y()-sp.y();
pt.drawEllipse(QRect(sp.x(),sp.y(),weight,height));
}
pt.end();
ui->lab3->setPixmap(QPixmap::fromImage(t_img));
}
void Widget::on_cbx_pensize_currentIndexChanged(int index)
{
pen_size=ui->cbx_pensize->currentText().toInt();
}
void Widget::on_btn_color_clicked()
{
pen_color = QColorDialog::getColor(Qt::white,this);
}
效果图
4、键盘事件
头文件
#include <QKeyEvent>
#include <QKeyEventTransition>
private slots:
void keyPressEvent(QKeyEvent *e);
widget.cpp
void Widget::drawShape()
{
if(shape_type < 1) return;
if(shape_type == 1) drawLine();
if(shape_type == 2) drawRect();
if(shape_type == 3) drawCircle();
ui->lab3->setFocus();
}
void Widget::keyPressEvent(QKeyEvent *e)
{
if(e->key() == Qt::Key_A)
{
ep = QPoint(ep.x()+10,ep.y()+10);
drawShape();
}
if(e->key() == Qt::Key_B)
{
ep = QPoint(ep.x()-10,ep.y()-10);
if(ep.x()-sp.x()>10 || ep.y()-sp.y()>10)
{
drawShape();
}
}
}
效果图
按A键
按B键