首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在c++中创建并行事件(特定Qt4.6)

如何在c++中创建并行事件(特定Qt4.6)
EN

Stack Overflow用户
提问于 2010-12-15 21:19:25
回答 1查看 246关注 0票数 0

我在一次执行多个函数时遇到了问题。具体来说,我有两个类(MyRect和Space类)。这个想法类似于太空入侵者,但我坚持从一开始。在类MyRect中,我定义了两个矩形::body (船体)和bullet (船体).In,主要的类空间是创建ship as MyRect带有body && bullet的新对象rectangles.Also,还有keyPress事件的定义。问题是,当我发射子弹时,所有的东西都停止了子弹的移动,直到MyRect::fireBullet(int x,int y)循环完成,我不能在这个event.Obviously期间移动船,我做了一些基本的错误,所以如果有人愿意澄清这一点。

以下是示例代码::

MyRect.h

代码语言:javascript
复制
#include <QWidget>
#include <QRect>
#include <QMainWindow>
#include <QPainter>
#include <QLabel>
#include <ctime>
#include <QtGui/QMainWindow>

class space;  

class MyRect : public QObject {

Q_OBJECT

public:
MyRect(int in_x, int in_y, int in_w, int in_h, QWidget* parent) 
    {
        itsx = in_x;
        itsy = in_y;
        itsw = in_w;
        itsh = in_h;

        body = new QRect(itsx, itsy, itsw, itsh);
        bullet = new QRect(itsx+41, itsy-15, itsw/8, itsh/2);
        itsParent = parent;
    }
~MyRect() {}

void move(int x ,int y);

public slots:
    void fireBullet(int x, int y);

private:
int itsx;
int itsy;
int itsw;
int itsh;
QWidget* itsParent;
QRect* body;
QRect* bullet;
friend class space;
};

MyRect.cpp

代码语言:javascript
复制
#include "MyRect.h"

void wait( float seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

void MyRect::move(int x, int y)
{
body->moveTo(x,y);
bullet->moveTo(x+35, y-15);


}

void MyRect::fireBullet(int x, int y)
{
y = y-15;
for(int i=0 ; i<200 ; i++)
{
    bullet->moveTo(x+41, y--);
    itsParent->repaint();
    wait(0.001);

}
}

space.h

代码语言:javascript
复制
    #include <QKeyEvent>
    #include <QMouseEvent>
    #include "MyRect.h" 

class space : public QMainWindow
{
Q_OBJECT

public:
    space(QWidget *parent = 0);
    ~space(){}


protected:
    void paintEvent(QPaintEvent *event);
    void keyPressEvent(QKeyEvent* event);

private:
private:
int x;
int y;
int w;
int h;
MyRect* ship;

signals:
void fireBullet(int x, int y); 

};

space.cpp

代码语言:javascript
复制
#include "space.h"
#include <QApplication>



space::space(QWidget *parent)
    : QMainWindow(parent)
{
x = 170;
y = 250;
w = 90;
h = 25;

ship = new MyRect(x,y,w,h, this);
connect(this, SIGNAL(fireBullet(int,int)), ship, SLOT(fireBullet(int,int)) );


}


void space::paintEvent(QPaintEvent *event)
{

  QPen pen(Qt::black, 2, Qt::SolidLine);
  QColor hourColor(0, 255, 0);


  QPainter painter(this);

  painter.setBrush(hourColor);  
  painter.setPen(pen);
  painter.drawRect( *(ship->body) );
 painter.drawRect( *(ship->bullet) );



}



void space::keyPressEvent(QKeyEvent* event)
{

switch(event->key())  {

    case Qt::Key_D :
    {
        x = x+10;
        ship->move(x,y);
        this->update();
        break;
    }
    case Qt::Key_A :
    {
        x = x-10;
        ship->move(x,y);
        this->update();
        break;
    }
    case Qt::Key_M :
    {
        emit fireBullet(x,y);


        break;
    }

} 

}

main.cpp

代码语言:javascript
复制
#include "space.h"
#include <QDesktopWidget>
#include <QApplication>



int main(int argc, char *argv[])
{
  QApplication app(argc, argv);

  space window;

  window.setWindowTitle("Lines");
  window.resize(500,500);
  window.show();

  return app.exec();
}

谢谢你的回答。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-16 00:11:24

你有一个架构问题。您正在做的是在其fireBullet方法中循环移动项目符号。当该循环运行时,程序的其余部分不会运行,因为单个线程一次只能做一件事。

解决方案是重构您的代码,以便每次调用某种更新方法时,屏幕上的所有内容都会通过一帧动画进行更新。基本上,你只需要保持足够的状态,你在哪里,你移动的速度有多快,在消失之前你可以走多远,这样你就可以在每一帧中按所需的量移动。

您要更改的另一件事是让keyPressEvent更新宇宙飞船的状态,以了解它应该移动的方向,以便它可以在其常规paintEvent上移动。为此,您可以使用QTimer

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4450291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档