基本事件:
鼠标事件
键盘事件
绘制事件
1.QT中的事件:
事件是对各应用程序需要知道的由应用程序内部或外部产生的事情或动作的通称。
QT中事件的处理:
在QT中使用一个对象来表示一个事件,继承自QEvent类
QT把事件定义 为固定的virtual保护函数,这些函数自动被系统调用
鼠标事件:
virtual void mouseDoubleClickEvent(QMouseEvent * event)
virtual void mouseMoveEvent(QMouseEvent * event)
virtual void mousePressEvent(QMouseEvent * event)
virtual void mouseReleaseEvent(QMouseEvent * event)
按下的是哪个键(左键,右键,中键,...)
例:event->button() == Qt::LeftButton
按下时的坐标
例:
int x = event->x();
int y = event->y();
键盘事件:
virtual void keyPressEvent(QKeyEvent * event)
virtual void keyReleaseEvent(QKeyEvent * event)
e->key() == Qt::Key_A//获取单个键
if(e->modifiers() == Qt::ControlModifier)//获取组合键
if(e->key() == Qt::Key_M)
2.定时器与随机数
QTimer
QTimer *timer = new QTimer(this);//创建一个定时器
connect(timer, SIGNAL(timeout()), this, SLOT(update()));//当定时器超时,发出信号timeout()
timer->start(1000)//启动定时器,同时设置超时时间,这里设置为1000毫秒
timer->stop();//停止定时器
srand(time(0));//设置随机数种子,一般参数使用当前系统时间,time(0) rand()%100;//伪随机,随机生成0-99之间的整数
案例:模拟抽奖
思路:
1.打开文件,读取文件内容
2.保存文件内容,比如保存到 QStringList
3.生成一个随机数,随机数的范围 由文件内容的项目数决定
4.以生成的随机数为下标,去List中取相应的内容
5.显示相应的内容。
3.绘制事件
virtual void paintEvent(QPaintEvent * event);
2D绘图
绘图三要素:
画家 QPainter
画笔/刷 QPen/QBrush
画布(绘画设备)
绘制函数: 图形:线,圆,矩形,弧,扇形,多边形...
图像:image,picture,pixmap
字符串:text
作业:
在一个窗体设置属性
在另一个窗体画图形
答案
dialob.h
#ifndef DIALOG_H
#define DIALOG_H
#include
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
void setPenWidth(int);
void setPencolor(QString);
void setPentu(QString);
void setPenfg(QString);
protected:
void paintEvent(QPaintEvent *);
private:
Ui::Dialog *ui;
int w;
QString s;
QString s1;
QString s2;
};
#endif // DIALOG_H mainwindom.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include "dialog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
Dialog *d;
};
#endif // MAINWINDOW_H dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include
#include
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
this->setFixedSize(400,400);
this->setWindowTitle("画图界面");
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::setPenWidth(int w)
{
this->w = w;
}
void Dialog::setPencolor(QString s)
{
this->s = s;
}
void Dialog::setPentu(QString s1)
{
this->s1 = s1;
}
void Dialog::setPenfg(QString s2)
{
this->s2 = s2;
}
void Dialog::paintEvent(QPaintEvent *)
{
QPainter pter(this);
QPen p;
if(s=="yellow")
{
qDebug("hello");
p.setColor(Qt::yellow);
}
else if(s=="red")
{
p.setColor(Qt::red);
}
p.setWidth(w);
if(s2=="SolidLine")
{
p.setStyle(Qt::SolidLine);
}
else if(s2=="DotLine")
{
p.setStyle(Qt::DotLine);
}
else if(s2=="DashDotLine")
{
p.setStyle(Qt::DashDotLine);
}
//SolidLine
//DashDotLine
//p.setStyle(Qt::DotLine);
pter.setPen(p);
QBrush brush(Qt::blue,Qt::SolidPattern);//设置画刷
pter.setBrush(brush);
if(s1=="yuan")
{
pter.drawEllipse(QPoint(200,200),100,100);
}
else if(s1=="juxing")
{
pter.drawRect(100,100,50,80);
}
} mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("属性设置");
this->setFixedSize(400,400);
d = new Dialog;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
int w = ui->comboBox->currentText().toInt();
QString s=ui->comboBox_2->currentText();
QString s1=ui->comboBox_3->currentText();
QString s2=ui->comboBox_4->currentText();
d->setPenWidth(w);
d->setPencolor(s);
d->setPentu(s1);
d->setPenfg(s2);
d->show();
d->update();
} main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
} 代码二 .h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
class QLabel;namespace Ui { class MainWindow;}class MainWindow : public QMainWindow
{ Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0); ~MainWindow();protected: void mousePressEvent(QMouseEvent * event);//鼠标按下事件 void mouseMoveEvent(QMouseEvent * event);//移动事件private:
Ui::MainWindow *ui; QLabel *lbl; QLabel *lbl2;};#endif // MAINWINDOW_H
.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include
#include
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
this->setFixedSize(400,400);
this->setWindowTitle("画图界面");
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::setPenWidth(int w)
{
this->w = w;
}
void Dialog::setPencolor(QString s)
{
this->s = s;
}
void Dialog::setPentu(QString s1)
{
this->s1 = s1;
}
void Dialog::setPenfg(QString s2)
{
this->s2 = s2;
}
void Dialog::paintEvent(QPaintEvent *)
{
QPainter pter(this);
QPen p;
if(s=="yellow")
{
qDebug("hello");
p.setColor(Qt::yellow);
}
else if(s=="red")
{
p.setColor(Qt::red);
}
p.setWidth(w);
if(s2=="SolidLine")
{
p.setStyle(Qt::SolidLine);
}
else if(s2=="DotLine")
{
p.setStyle(Qt::DotLine);
}
else if(s2=="DashDotLine")
{
p.setStyle(Qt::DashDotLine);
}
//SolidLine
//DashDotLine
//p.setStyle(Qt::DotLine);
pter.setPen(p);
QBrush brush(Qt::blue,Qt::SolidPattern);//设置画刷
pter.setBrush(brush);
if(s1=="yuan")
{
pter.drawEllipse(QPoint(200,200),100,100);
}
else if(s1=="juxing")
{
pter.drawRect(100,100,50,80);
}
} .cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("属性设置");
this->setFixedSize(400,400);
d = new Dialog;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
int w = ui->comboBox->currentText().toInt();
QString s=ui->comboBox_2->currentText();
QString s1=ui->comboBox_3->currentText();
QString s2=ui->comboBox_4->currentText();
d->setPenWidth(w);
d->setPencolor(s);
d->setPentu(s1);
d->setPenfg(s2);
d->show();
d->update();
} .cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
} 代码三 .h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;}class MainWindow : public QMainWindow
{ Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0); ~MainWindow();protected:
void keyPressEvent(QKeyEvent *e);private slots:
void on_pushButton_clicked();private:
Ui::MainWindow *ui;};#endif // MAINWINDOW_H
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QKeyEvent>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this);}MainWindow::~MainWindow()
{ delete ui;}void MainWindow::keyPressEvent(QKeyEvent *e)
{ if(e->key() == Qt::Key_A) { int x = ui->pushButton->x(); int y = ui->pushButton->y(); ui->pushButton->move(x-10,y); } if(e->key() == Qt::Key_D) { int x = ui->pushButton->x(); int y = ui->pushButton->y(); ui->pushButton->move(x+10,y); }if(e->modifiers() == Qt::ControlModifier)
{ if(e->key() == Qt::Key_M) { qDebug("ctrl + m"); this->setWindowState(Qt::WindowFullScreen); } if(e->key() == Qt::Key_Q) { this->setWindowState(Qt::WindowNoState); } }}void MainWindow::on_pushButton_clicked()
{}
.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
} 代码三 .h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTimer>namespace Ui { class MainWindow;}class MainWindow : public QMainWindow
{ Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0); ~MainWindow();public slots: void timeoutSlot();private slots:
void on_pushButton_clicked();void on_pushButton_2_clicked();
private:
Ui::MainWindow *ui; QTimer *timer;};#endif // MAINWINDOW_H
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QDateTime>#include <QDebug>#include <ctime>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ui->setupUi(this);
qsrand(time(0)); timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(timeoutSlot()));}MainWindow::~MainWindow()
{ delete ui;}void MainWindow::timeoutSlot()
{ QString curTime = QDateTime::currentDateTime().toString(); ui->label->setText(curTime);}void MainWindow::on_pushButton_clicked()
{ timer->start(1000); int i = qrand()%100; qDebug() << i;}void MainWindow::on_pushButton_2_clicked()
{ timer->stop();}
.cpp
#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{ QApplication a(argc, argv); MainWindow w; w.show();return a.exec();
}
代码四
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QPaintEvent>namespace Ui { class MainWindow;}class MainWindow : public QMainWindow
{ Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0); ~MainWindow();protected:
void paintEvent(QPaintEvent * event);private:
Ui::MainWindow *ui;};#endif // MAINWINDOW_H
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QDebug>#include <QPainter>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this);}MainWindow::~MainWindow()
{ delete ui;}void MainWindow::paintEvent(QPaintEvent *event)
{ //1.产生绘图对象 QPainter tang(this);//2.设置画笔、画刷属性
QPen p; p.setColor(Qt::red);//设置画笔颜色 p.setWidth(10);//设置笔画宽度 // p.setStyle(Qt::DotLine);//设置画笔风格 tang.setPen(p);//把画笔交给画家 QBrush brush(Qt::blue,Qt::Dense5Pattern);//设置画刷 tang.setBrush(brush); //3.开始画// tang.drawLine(0,0,100,100);//画线 tang.drawEllipse(100,100,80,80);//画(椭)圆// tang.drawRect(50,50,100,80);//画矩形 //tang.drawArc(100,100,100,100,30*16,120*16);//弧 // tang.drawChord(100,100,100,100,30*16,120*16);//弦弧// tang.drawPie(100,100,100,100,30*16,300*16);//画饼状图(扇形)// tang.drawPoint(200,200);//画点// QPointF points[4] = { // QPointF(10.0, 80.0),// QPointF(20.0, 10.0),// QPointF(80.0, 30.0),// QPointF(90.0, 70.0)// };// tang.drawConvexPolygon(points,4);//画多边形// QPainterPath path;
// path.moveTo(0,200);// path.lineTo(400,200);// path.lineTo(100,400);// path.lineTo(200,0);// path.lineTo(300,400);// path.lineTo(0,200);// tang.drawPath(path);//画路径,比如画五角星
}
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QDebug>#include <QPainter>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this);}MainWindow::~MainWindow()
{ delete ui;}void MainWindow::paintEvent(QPaintEvent *event)
{ //1.产生绘图对象 QPainter tang(this);//2.设置画笔、画刷属性
QPen p; p.setColor(Qt::red);//设置画笔颜色 p.setWidth(10);//设置笔画宽度 // p.setStyle(Qt::DotLine);//设置画笔风格 tang.setPen(p);//把画笔交给画家 QBrush brush(Qt::blue,Qt::Dense5Pattern);//设置画刷 tang.setBrush(brush); //3.开始画// tang.drawLine(0,0,100,100);//画线 tang.drawEllipse(100,100,80,80);//画(椭)圆// tang.drawRect(50,50,100,80);//画矩形 //tang.drawArc(100,100,100,100,30*16,120*16);//弧 // tang.drawChord(100,100,100,100,30*16,120*16);//弦弧// tang.drawPie(100,100,100,100,30*16,300*16);//画饼状图(扇形)// tang.drawPoint(200,200);//画点// QPointF points[4] = { // QPointF(10.0, 80.0),// QPointF(20.0, 10.0),// QPointF(80.0, 30.0),// QPointF(90.0, 70.0)// };// tang.drawConvexPolygon(points,4);//画多边形// QPainterPath path;
// path.moveTo(0,200);// path.lineTo(400,200);// path.lineTo(100,400);// path.lineTo(200,0);// path.lineTo(300,400);// path.lineTo(0,200);// tang.drawPath(path);//画路径,比如画五角星
}
.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}