由 訪客 » 週一 7月 27, 2009 10:07 am
解決了,把QPainterPath先存到QList,傳一個id(thread長度)給wthread,whread回傳此id,若是QList(QPainterPath),已達id長度在畫出update();
frame.cpp
void Frame::mousePressEvent(QMouseEvent *event)//滑鼠事件press
{
//按下按鈕,就產生一個子彈thread,並加入bullet_list
if(bullet_list->length()<4)//判定有幾個thread
{
thread=new Wthread;
bullet_list->append(thread);
thread->set_x(this->get_body_x());
thread->set_id(bullet_list->length());
connect(thread, SIGNAL(setxy(int , int ,int ,int,QPainterPath*)),this, SLOT(setShape(int ,int ,int ,int,QPainterPath*)));
thread->start();
}
//this->bullet_paint(this->get_body_x(),move_y);//產生子彈
#include "frame.h"
#include "ui_frame.h"
#include <QPainter>
#include <math.h>
#include <QMouseEvent>
#include <stage1.h>
#include <QVector>
#include "wthread.h"
using namespace std;
//static const QSize resultSize(5, 5);
Frame::Frame(QWidget *parent) :
QFrame(parent),
m_ui(new Ui::Frame)
{
//初始化設定
m_ui->setupUi(this);
//setShape(20,20,120,120);
step_value=0;
//連結執行序的信號到繪圖程序,造成動畫效果
move_x=0;
move_y=300;
//setMouseTracking(true);//設一開始追蹤mouse
bullet_path=new QPainterPath; //一定要先初始化或者ERROR
bullet_list_Painter=new QList<QPainterPath*>;//一定要先初始化或者ERROR
bullet_list=new QList<Wthread*>;
}
Frame::~Frame()
{
delete m_ui;
}
void Frame::changeEvent(QEvent *e)
{
QFrame::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
m_ui->retranslateUi(this);
break;
default:
break;
}
}
//SLOT,更新繪圖的值(子彈)
void Frame::setShape(int id,int y,int w,int h,QPainterPath *bullet_path1)
{
bullet_path=bullet_path1;
bullet_list_Painter->append(bullet_path1);
if( y==300 ){ bullet_list->removeAt(0);thread->set_id(bullet_list->length());}
if(this->bullet_true(id)){repaint();}
}
//繪圖宣告
void Frame::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
///////////////////////////////////機台///////////////////////////
//設定筆刷
painter.setBrush(QColor(112, 186, 39));
//畫出機台
painter.drawPath(this->body_paint(new QPainterPath));
//////////////畫出
//設定筆刷
// painter.setBrush(QColor(112, 186, 39));
painter.drawPath(this->mask_pain(new QPainterPath));
///////////////////////////////////子彈////////////////////////////
painter.setBrush(QColor(212, 16, 39));
for(int i =0 ; i < bullet_list_Painter->length() ;i++)
{painter.drawPath(*bullet_list_Painter->at(i));}
bullet_list_Painter->clear();
//if(!bullet_path->isEmpty()){painter.drawPath(*bullet_path);}
}
//畫出機台
QPainterPath Frame::body_paint(QPainterPath* body)
{
body->addRect(move_x, move_y, 10, 20);
return *body;
}
//畫出障礙
QPainterPath Frame::mask_pain(QPainterPath* mask)
{
stage1 stage;//放障礙物的class
for (int i = 0; i < stage.get_mask_number(); i++)
{
int x=stage.get_mask_x(i);
int y=stage.get_mask_y(i);
mask->addRect(x,y,10,10);
}
return *mask;
}
//移動機台,繪圖
void Frame::move_body_paint(int x,int y)
{
move_x=x;
move_y=y;
if(bullet_list->length()==0){update();}
}
//回傳目前body的x,y值
int Frame::get_body_x()
{
return this->move_x;
}
int Frame::get_body_y()
{
return this->move_y;
}
void Frame::mouseMoveEvent(QMouseEvent *e)//滑鼠事件move
{
//move_x=e-> globalX();
//update();
int xx=e->x();
//int xx=e-> globalX(); //x
int yy=e->y(); //y
this->move_body_paint(xx,move_y);
}
void Frame::mousePressEvent(QMouseEvent *event)//滑鼠事件press
{
//按下按鈕,就產生一個子彈thread,並加入bullet_list
if(bullet_list->length()<4)//判定有幾個thread
{
thread=new Wthread;
bullet_list->append(thread);
thread->set_x(this->get_body_x());
thread->set_id(bullet_list->length());
connect(thread, SIGNAL(setxy(int , int ,int ,int,QPainterPath*)),this, SLOT(setShape(int ,int ,int ,int,QPainterPath*)));
thread->start();
}
//this->bullet_paint(this->get_body_x(),move_y);//產生子彈
}
//判定bullet
bool Frame::bullet_true(int id)
{
bool t;
if(bullet_list->length() == id){ t=true ;}
else {t=false;}
return t;
}
wthead.cpp
#include "wthread.h"
#include <QThread>
#include <frame.h>
#include <QPainterPath>
#include <QPainter>
#include <QtGui/QFrame>
Wthread::Wthread(QWidget *parent )
: QThread(parent)
{
//一定要先初始化或者ERROR
//p=new QPainterPath;
}
//執行續要處裡的事,每20毫秒,做一次信號
void Wthread::run() {
for(int i = 0; i < 310; i += 5) {
QPainterPath* p=new QPainterPath;
p->addRoundRect(x_int,300-i,10.0,10.0,100);
p->addRoundRect(x_int,310-i,10.0,10.0,100);
//p=this->bullet_paint(90,90);
emit setxy(id,i,i,i, p);
msleep(100);
}
exec();
}
Wthread::~Wthread()
{
}
void Wthread::set_x(int i)
{
this->x_int=i;
}
void Wthread::set_id(int id)//thred id
{
this->id=id;
}