Qt5实现打地鼠
2016-01-19 20:04
447 查看
最近放寒假在家闲来无事把以前写的东西写下,做下记录,实现打地鼠基本功能。
效果图:
百度网盘分享:http://pan.baidu.com/s/1eRtWDKe
最要是自定义的按钮,也就是老鼠出来的小窗体,还有线程,以及主窗口。
下面是主要代码,
自定义按钮:
#include “mybutton.h”
MyButton::MyButton(QWidget *parent) :
QPushButton(parent)
{
stopflag=true;
downup=true;
mouse_heigth=0;
timer_=new QTimer(this);
connect(timer_,SIGNAL(timeout()),this,SLOT(showimage()));
// this->setAutoFillBackground(true);
// QPixmap pix(“:/images/hole.jpg”);
// pix=pix.scaled(this->width(),this->height());
// QPalette pale;
// pale.setBrush(QPalette::Button,QBrush(pix));
// setPalette(pale);
// update();
}
void MyButton::paintEvent(QPaintEvent *)
{
QImage img;
img.load(“:/images/u.gif”);
}
void MyButton::mousePressEvent(QMouseEvent *e)
{
QPixmap pix(“:/images/bt03.png”);
if(e->button()==Qt::LeftButton)
this->setCursor(QCursor(pix));
e->accept();
this->click();
}
void MyButton::mouseReleaseEvent(QMouseEvent *e)
{
QPixmap pix(“:/images/bt001.png”);
if(e->button()==Qt::LeftButton)
this->setCursor(QCursor(pix));
e->accept();
}
void MyButton::showimage()
{
if(!downup&&mouse_heigth<=0){
timer_->stop();
}
void MyButton::timestart()
{
timer_->start(10);
}
线程函数:
#include “thread.h”
Thread::Thread(QObject *parent) :
QThread(parent)
{
stopFlag = true;
}
void Thread::stopThread()
{
stopFlag = true;
this->exit();
// this->quit();
// this->terminate();
}
void Thread::run()
{
if(!stopFlag){
// stopFlag=!stopFlag;
return;
}
stopFlag = false;
// qDebug()<
效果图:
百度网盘分享:http://pan.baidu.com/s/1eRtWDKe
最要是自定义的按钮,也就是老鼠出来的小窗体,还有线程,以及主窗口。
下面是主要代码,
自定义按钮:
#include “mybutton.h”
MyButton::MyButton(QWidget *parent) :
QPushButton(parent)
{
stopflag=true;
downup=true;
mouse_heigth=0;
timer_=new QTimer(this);
connect(timer_,SIGNAL(timeout()),this,SLOT(showimage()));
this->setMaximumSize(120,120); this->setMinimumSize(120,120); setEnabled(true); setChecked(true);
// this->setAutoFillBackground(true);
// QPixmap pix(“:/images/hole.jpg”);
// pix=pix.scaled(this->width(),this->height());
// QPalette pale;
// pale.setBrush(QPalette::Button,QBrush(pix));
// setPalette(pale);
// update();
}
void MyButton::paintEvent(QPaintEvent *)
{
QImage img;
img.load(“:/images/u.gif”);
QRectF s(0,this->height()-mouse_heigth,this->width(),this->height()); QRectF t(0,0,this->width(),this->height()); QPainter painter(this); painter.drawImage(s,img,t); update();
}
void MyButton::mousePressEvent(QMouseEvent *e)
{
QPixmap pix(“:/images/bt03.png”);
if(e->button()==Qt::LeftButton)
this->setCursor(QCursor(pix));
e->accept();
this->click();
}
void MyButton::mouseReleaseEvent(QMouseEvent *e)
{
QPixmap pix(“:/images/bt001.png”);
if(e->button()==Qt::LeftButton)
this->setCursor(QCursor(pix));
e->accept();
}
void MyButton::showimage()
{
if(!downup&&mouse_heigth<=0){
timer_->stop();
} if(downup){ mouse_heigth+=1; if(mouse_heigth>=this->height()) downup=!downup; } if(!downup){ mouse_heigth-=1; if(mouse_heigth<0) downup=!downup; } update();
}
void MyButton::timestart()
{
timer_->start(10);
}
线程函数:
#include “thread.h”
Thread::Thread(QObject *parent) :
QThread(parent)
{
stopFlag = true;
}
void Thread::stopThread()
{
stopFlag = true;
this->exit();
// this->quit();
// this->terminate();
}
void Thread::run()
{
if(!stopFlag){
// stopFlag=!stopFlag;
return;
}
stopFlag = false;
qsrand(QTime::currentTime().second()); int cur_index = 0; int tmp_index = 0; while(!stopFlag){ cur_index = qrand() % 9; if(tmp_index == cur_index) continue; tmp_index = cur_index; emit sendRandDigit(cur_index); usleep(1000*1000);
// qDebug()<
相关文章推荐
- QT学习 第一章:基本对话框
- 使用Shiboken为C++和Qt库创建Python绑定
- C#线程间不能调用剪切板的解决方法
- C#线程同步的三类情景分析
- C#获取进程或线程相关信息的方法
- C#停止线程的方法
- C#子线程更新UI控件的方法实例总结
- C#线程队列用法实例分析
- C++使用CriticalSection实现线程同步实例
- 基于C++实现的线程休眠代码
- VB读取线程、句柄及写入内存的API代码实例
- C#网络编程基础之进程和线程详解
- C#通过Semaphore类控制线程队列的方法
- C#多线程处理多个队列数据的方法
- C#实现线程安全的简易日志记录方法
- C#中线程同步对象的方法分析
- ASP.NET线程相关配置
- 浅析linux环境下一个进程最多能有多少个线程
- 再谈JavaScript线程
- Qt定时器和随机数详解