qt graphicsscene line subclass - c++

I added a Line subclass from the GraphicsScene class , to draw a line. From MainWindow I call the line function in that class, there are no errors, but the line isn't drawn. I know this must be lack of my c++ skills. But searching didn't help me on this one. What I want is to make different classes for drawing different shapes, instead of polluting the GraphicsScene with all that code, to keep things a bit structured. But what am I doing wrong? I posted my code on github github.com/JackBerkhout/QT_Draw001
line.h
#ifndef LINE_H
#define LINE_H
#include <QMainWindow>
#include <QObject>
#include <QDebug>
#include "graphicsscene.h"
class Line: public GraphicsScene
{
public:
Line();
void drawLine(int x1, int y1, int x2, int y2);
};
#endif // LINE_H
line.cpp
#include "line.h"
Line::Line():
GraphicsScene()
{
}
void Line::drawLine(int x1, int y1, int x2, int y2)
{
// Just draw something by clicking a button
qDebug() << "line"; // This debug message is shown
QColor color;
color.setRgb(128, 0, 255);
QPen pen;
pen.setColor(color);
pen.setWidth(20);
pen.setCapStyle(Qt::RoundCap);
this->addLine(x1, y1, x2, y2, pen); // Didn't draw the line on the scene
}
graphicsscene.cpp
#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
#include <QList>
class GraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit GraphicsScene(QObject *parent = 0);
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent);
QPointF getMousePoint(void);
int getMouseX(void);
int getMouseY(void);
int getNumber(void);
void setNumber(int num);
QPointF mousePoint;
int MouseX, MouseY;
int myNumber;
signals:
void changedMousePosition(QPointF mousePoint);
void changedNumber(int myNumber);
public slots:
private:
QList <QPointF> mousePoints;
// int Number;
};
#endif // GRAPHICSSCENE_H
graphicsscene.cpp
#include "mainwindow.h"
#include "graphicsscene.h"
#include <QDebug>
GraphicsScene::GraphicsScene(QObject *parent) :
QGraphicsScene(parent)
{
this->setBackgroundBrush(Qt::black);
myNumber = 0;
// this-> ->setMouseTracking(true);
}
void GraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
// mousePoint = mouseEvent->scenePos();
// MouseX = mousePoint.x();
// MouseY = mousePoint.y();
qDebug() << Q_FUNC_INFO << mouseEvent->scenePos();
QGraphicsScene::mouseDoubleClickEvent(mouseEvent);
}
void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
mousePoint = mouseEvent->scenePos();
MouseX = mouseEvent->scenePos().x();
MouseY = mouseEvent->scenePos().y();
emit changedMousePosition(mousePoint);
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
mousePoint = mouseEvent->scenePos();
MouseX = mouseEvent->scenePos().x();
MouseY = mouseEvent->scenePos().y();
mousePoints.append(mouseEvent->scenePos());
MainWindow *mainWindow = new MainWindow();
mainWindow->Count++;
if(mousePoints.size() == 2)
{
myNumber++;
emit changedNumber(myNumber);
QColor color;
color.setRgb(128, 0, 255);
QPen pen;
pen.setColor(color);
pen.setWidth(20);
pen.setCapStyle(Qt::RoundCap);
this->addLine(mousePoints.at(0).x(), mousePoints.at(0).y(), mousePoints.at(1).x(), mousePoints.at(1).y(), pen);
mousePoints.clear();
}
QGraphicsScene::mousePressEvent(mouseEvent);
}
void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
mousePoint = mouseEvent->scenePos();
MouseX = mouseEvent->scenePos().x();
MouseY = mouseEvent->scenePos().y();
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
QPointF GraphicsScene::getMousePoint(void)
{
return mousePoint;
}
int GraphicsScene::getMouseX(void)
{
MouseX = mousePoint.x();
return mousePoint.x();
}
int GraphicsScene::getMouseY(void)
{
MouseY = mousePoint.y();
return mousePoint.y();
}
void GraphicsScene::setNumber(int num)
{
myNumber = num;
}
int GraphicsScene::getNumber(void)
{
return myNumber;
}

You are complicating too much, besides that I think you do not understand what is the purpose of the inheritance, you just have to create a function in GraphicsScene called drawLine and use it when you need it.
GraphicsScene.h
public:
void drawLine(int x1, int y1, int x2, int y2);
GraphicsScene.cpp
void GraphicsScene::drawLine(int x1, int y1, int x2, int y2)
{
QColor color;
color.setRgb(128, 0, 255);
QPen pen;
pen.setColor(color);
pen.setWidth(20);
pen.setCapStyle(Qt::RoundCap);
addLine(x1, y1, x2, y2, pen);
}
And then you call in Mainwindow:
void MainWindow::on_toolButtonDraw_clicked()
{
scene->drawLine(300, 100, 500, 300);
}
When you create a Line object you are creating a new scene and it will be drawn in that scene, so you will not see it.

Related

How to draw a custom shaped element?

I'm currently working in an app which is essentially a pong game. The game features two paddles currently in rectangular format to ease collision detection against the ball(currently square).
I'm using a "QRect" element as paddle since it provides the ".intersect" method, making it easy to check for collisions.
My implementation for a rectangular paddle is as follows:
Paddle::Paddle(int initial_x, int initial_y) {
QImage image.load(":images/paddle.png");
QRect rect = image.rect();
resetState(initial_x, initial_y);
}
I'm trying to draw an arch like paddle and hitbox, similar to what the code below provides:
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
int startAngle = 30 * 16;
int spanAngle = 120 * 16;
QPainter painter(this);
painter.drawChord(rectangle, startAngle, spanAngle);
The only problem with the code above is it only works inside a paintEvent function and that won't work for me.
paddle.h
#ifndef PADDLE_H
#pragma once
#include <QImage>
#include <QRect>
class Paddle {
public:
Paddle(int, int);
~Paddle();
public:
void resetState(int, int);
void move();
void setDx(int);
void setDy(int);
QRect getRect();
QImage & getImage();
private:
QImage image;
QRect rect;
int dx;
int dy;
static const int INITIAL_X1 = 70;
static const int INITIAL_Y1 = 350;
};
#define PADDLE_H
#endif // PADDLE_H
paddle.cpp
#include <iostream>
#include "paddle.h"
Paddle::Paddle(int initial_x, int initial_y) {
dy = 0;
image.load(":images/paddle.png");
rect = image.rect();
resetState(initial_x, initial_y);
}
Paddle::~Paddle() {
std::cout << ("Paddle deleted") << std::endl;
}
void Paddle::setDy(int y) {
dy = y;
}
void Paddle::move() {
int x = rect.x();
int y = rect.top() + dy;
rect.moveTo(x, y);
}
void Paddle::resetState(int initial_x, int initial_y) {
rect.moveTo(initial_x, initial_y);
}
QRect Paddle::getRect() {
return rect;
}
QImage & Paddle::getImage() {
return image;
}
mainGame.h
#ifndef BREAKOUT_H
#pragma once
#include <QWidget>
#include <QKeyEvent>
#include <QFrame>
#include "ball.h"
#include "brick.h"
#include "paddle.h"
class Breakout : public QFrame {
Q_OBJECT
public:
Breakout(QWidget *parent = 0);
~Breakout();
signals:
void leftScoreChanged(int leftScore);
void rightScoreChanged(int rightScore);
void ballLost(int ballsLeft);
protected:
void paintEvent(QPaintEvent *);
void timerEvent(QTimerEvent *);
void keyPressEvent(QKeyEvent *);
void keyReleaseEvent(QKeyEvent *);
void drawObjects(QPainter *);
void finishGame(QPainter *, QString);
void moveObjects();
void startGame();
void pauseGame();
void stopGame();
void victory();
void validateScoreChange(int);
void checkCollision();
private:
int x;
int timerId;
int ballsLeft;
int leftScore;
int rightScore;
static const int N_OF_BRICKS = 30;
static const int DELAY = 5;
static const int TOP_EDGE = 0;
static const int LEFT_EDGE = 0;
static const int BOTTOM_EDGE = 700;
static const int RIGHT_EDGE = 1200;
Ball *ball;
Paddle *leftPaddle;
Paddle *rightPaddle;
Brick *bricks[N_OF_BRICKS];
bool gameOver;
bool gameWon;
bool gameStarted;
bool paused;
};
#define BREAKOUT_H
#endif // BREAKOUT_H
Breakout::paintEvent()
void Breakout::paintEvent(QPaintEvent *e) {
Q_UNUSED(e);
QPainter painter(this);
if (gameOver) {
finishGame(&painter, "Game lost");
} else if(gameWon) {
finishGame(&painter, "Victory");
}
else {
drawObjects(&painter);
}
QWidget::paintEvent(e);
}
From what I understand you do not want to load the QImage from a .png file but you want to draw it, if so you can use QPainter to create the image as I show below:
Paddle::Paddle(int initial_x, int initial_y) {
// draw image
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
int startAngle = 30 * 16;
int spanAngle = 120 * 16;
image = QImage(QSize(100, 100), QImage::Format_ARGB32);
image.fill(Qt::transparent);
QPainter painter(&image);
painter.drawChord(rectangle, startAngle, spanAngle);
painter.end();
dy = 0;
rect = image.rect();
resetState(initial_x, initial_y);
}

Qt: Custom derived class not declared in scope

Qt Newbie here experiencing a problem where it claims that I did not declare a class which I'm pretty sure is declared.
I cannot for the life of me figure this out. Please help.
Commenting out the problematic lines in Connection.h and .cpp results in compilable output.
Here is a slimmed down version of my code where the problem still exists:
Btw, Map.ui only has a Graphics View dragged into it.
Error message:
In file included from ../Map/Map.h:17:0,
from ../Map/City.cpp:1:
../Map/Connection.h:14:11: error: 'City' was not declared in this scope
QPair<City*, City*> cities;
^
../Map/Connection.h:14:18: error: 'City' was not declared in this scope
QPair<City*, City*> cities;
^
../Map/Connection.h:14:23: error: template argument 1 is invalid
QPair<City*, City*> cities;
^
../Map/Connection.h:14:23: error: template argument 2 is invalid
../Map/Connection.h:19:22: error: 'City' was not declared in this scope
Connection(QPair<City*, City*> cities, int cost);
^
../Map/Connection.h:19:29: error: 'City' was not declared in this scope
Connection(QPair<City*, City*> cities, int cost);
^
../Map/Connection.h:19:34: error: template argument 1 is invalid
Connection(QPair<City*, City*> cities, int cost);
Map.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Map
TEMPLATE = app
SOURCES +=\
City.cpp \
Connection.cpp \
Map.cpp \
Driver.cpp
HEADERS += \
City.h \
Connection.h \
Map.h
FORMS += \
Map.ui
Map.h:
#ifndef MAP_H
#define MAP_H
#include <QApplication>
#include <QGraphicsItem>
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
#include <QGraphicsScene>
#include <QGraphicsEllipseItem>
#include <QDebug>
#include <QPainter>
#include <QString>
#include <QVector>
#include "ui_Map.h"
#include "Connection.h"
#include "City.h"
namespace Ui
{
class Map;
}
class Map : public QMainWindow
{
Q_OBJECT
public:
explicit Map(QWidget *parent = 0);
~Map();
private:
Ui::Map *ui;
QGraphicsScene *scene;
};
#endif // MAP_H
Map.cpp:
#include "Map.h"
#include "ui_Map.h"
Map::Map(QWidget *parent) : QMainWindow(parent), ui(new Ui::Map)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
City *c1 = new City(30, 30, 30, "K");
City *c2 = new City(30, 90, 30, "O");
Connection *conn1 = new Connection(45, 45, 45, 105, 1);
c1->setZValue(4);
c2->setZValue(4);
conn1->setZValue(2);
scene->addItem(c1);
scene->addItem(c2);
scene->addItem(conn1);
}
Map::~Map()
{
delete ui;
}
City.h:
#ifndef CITY_H
#define CITY_H
#include "Map.h"
class City : public QGraphicsItem
{
private:
int x;
int y;
int r;
QString name;
QRectF bounds;
QVector<QPair<City*, int> > neighbors;
public:
City();
City(int x, int y, int r, QString name);
int getX() { return this->x; }
int getY() { return this->y; }
int getR() { return this->r; }
QString getName() { return this->name; }
QVector<QPair<City*, int> > getNeighbors() { return this->neighbors; }
void setNeighbors(QVector<QPair<City*, int> >) { this->neighbors = neighbors; }
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
#endif // CITY_H
City.cpp:
#include "Map.h"
City::City()
{
this->bounds = QRectF(0, 0, 0, 0);
}
City::City(int x, int y, int r, QString name)
{
this->x = x;
this->y = y;
this->r = r;
this->name = name;
this->bounds = QRectF(x, y, r, r);
}
QRectF City::boundingRect() const
{
return QRectF(x, y, r, r);
}
void City::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec;
if (this->bounds == QRectF(0, 0, 0, 0))
rec = boundingRect();
else
rec = this->bounds;
QBrush brush(Qt::blue);
QPen pen(Qt::blue);
painter->setBrush(brush);
painter->setPen(pen);
painter->drawEllipse(rec);
}
Connection.h:
#ifndef CONNECTION_H
#define CONNECTION_H
#include "Map.h"
class Connection : public QGraphicsItem
{
private:
int x1, x2;
int y1, y2;
int cost;
QRectF bounds;
QPair<City*, City*> cities; // PROBLEMATIC LINE -- CLAIMS CITY WAS NOT DECLARED
public:
Connection();
Connection(int x1, int y1, int x2, int y2, int cost);
Connection(QPair<City*, City*> cities, int cost); // PROBLEMATIC LINE -- CLAIMS CITY WAS NOT DECLARED
int getCost() { return this->cost; }
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
#endif // CONNECTION_H
Connection.cpp:
#include "Map.h"
Connection::Connection()
{
this->bounds = QRectF(0, 0, 0, 0);
}
Connection::Connection(int x1, int y1, int x2, int y2, int cost)
{
this->x1 = x1;
this->x2 = x2;
this->y1 = y1;
this->y2 = y2;
this->bounds = QRectF(x1, x2, y1, y2);
}
Connection::Connection(QPair<City*, City*> cities, int cost) // PROBLEMATIC BLOCK
{
int r = cities.first->getR();
this->x1 = cities.first->getX() + r/2;
this->x2 = cities.second->getX() + r/2;
this->y1 = cities.first->getY() + r/2;
this->y2 = cities.second->getY() + r/2;
this->cost = cost;
this->bounds = QRectF(x1, x2, y1, y2);
}
QRectF Connection::boundingRect() const
{
return QRectF(0, 0, 0, 0);
}
void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec;
if (this->bounds == QRectF(0, 0, 0, 0))
rec = boundingRect();
else
rec = this->bounds;
QBrush brush(Qt::red);
QPen pen(Qt::red);
painter->setPen(pen);
painter->drawLine(QLine(this->x1, this->y1, this->x2, this->y2));
}
Driver.cpp:
#include "Map.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Map w;
w.show();
return a.exec();
}
Thank you for taking the time to look at this. I appreciate any feedback whatsoever. The problematic lines were compilable for a while but it seems that adding certain things "broke" it.
The answer to this thread got me thinking it might be something silly, but my attempts to change the order of things have not fixed it.
Just looking at the error message one can see that the City class is indeed not defined at Connection.
In file included from ../Map/Map.h:17:0,
from ../Map/City.cpp:1:
City.cpp starts out with a #include "Map.h" which is odd because there are no references to the Map type in the cpp file. City.cpp should start out with an #include "City.h".
City.h starts out with an #include "Map.h" which is even stranger because the City class make no mention of Map.
So at this point City.cpp:1 -> Map.h :17 you are including "Connection.h". At this point, there are no defined types. Not City, not Map. The compiler approaches the class Connection with no types defined.
Fixing it:
It is hard to guess how your dependencies actually look like but one solution for this particular include might be.
Start by including City.h first in your City.cpp. You should #include "X.h" for all X.cpp files.
Remove the #include "Map.h" from the City.h file.
If the Map type is required in City.cpp, include it.
The Connection.h obviously requires at least a forward declared class class City because of the pointers. So either do a forward declaration or an `#include "City.h"
In conclusion: Only include files in the header that your class actually requires. In the corresponding cpp, first include the header. Any type the header requires will also be required by the implementation. After that you include the additional types your implementation requires.

How should i create C-wrapper for c++ library which uses QtGui?

I need to create C wrapper around qt-project, which will be used as a library. It uses simple functions of QPainter.
I created a class Qtx:
// -- Qtx.h --
#ifndef QTX_H
#define QTX_H
#include <QWidget>
#include <QPainter>
class Qtx
{
private:
QPainter *painter;
QPixmap *pix;
public:
Qtx();
QPixmap* getPixmap();
void setPainterColor(int r, int g, int b);
void drawLine(int x1, int y1, int x2, int y2);
void drawCircle(int x, int y, int radius);
void drawEllipse(int x1, int y1, int x2, int y2);
void drawRectangle(int x1, int y1, int x2, int y2);
};
#endif // QTX_H
// -- Qtx.cpp --
#include "qtx.h"
Qtx::Qtx()
{
pix = new QPixmap(400,280);
QPalette palette;
pix->fill(palette.color(QPalette::Window));
painter = new QPainter(pix);
}
QPixmap* Qtx :: getPixmap()
{
return pix;
}
void Qtx::setPainterColor(int r, int g, int b)
{
QPen *pen = new QPen(QColor(r, g, b));
painter->setPen(*pen);
}
void Qtx :: drawLine(int x1, int y1, int x2, int y2)
{
painter->drawLine(x1,y1,x2,y2);
}
void Qtx :: drawCircle(int x, int y, int radius)
{
QPoint center(x, y);
painter->drawEllipse(center, radius, radius);
}
void Qtx :: drawEllipse(int x1, int y1, int x2, int y2)
{
QRect rect(x1, y1, x2, y2);
painter->drawEllipse(rect);
}
void Qtx :: drawRectangle(int x1, int y1, int x2, int y2)
{
QRect rect(x1, y1, x2, y2);
painter->drawRect(rect);
}
And created C-wrapper for this class:
/*qtx-c-wrapper.h*/
struct HQTx;
typedef struct HQTx HQTx;
HQTx * create();
void destroy(HQTx * h);
void setPainterColor(HQTx* v, int r, int g, int b);
void drawLine(HQTx* v, int x1, int y1, int x2, int y2);
void drawCircle(HQTx* v, int x, int y, int radius);
void drawEllipse(HQTx* v, int x1, int y1, int x2, int y2);
void drawRectangle(HQTx* v, int x1, int y1, int x2, int y2);
But members of Qtx can only be used in MainWindow (as far as I know), for example:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qtx.h"
#include <QGridLayout>
#include <QPushButton>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
Qtx* tx = new Qtx();
QPixmap* p;
QLabel *label = new QLabel(this);
tx->setPainterColor(255,0,0);
tx->drawCircle(100,100,90);
p = tx->getPixmap();
ui->setupUi(this);
if(p != NULL)
{
label->setGeometry(0,0,400,280);
label->setPixmap(*p);
ui->centralWidget = label;
}
}
Is it possible to create C-wrapper for MainWindow? How should i do it?
I'm trying to create a library use it in C++ projects approximately as follows (just example):
#include "QtxLib.h"
int main()
{
QtxCreateWindow w = QtxCreateWindow (800, 600);
QtxDrawLine (w, 320, 290, 320, 220);
QtxCircle (w, 320, 190, 30);
return 0;
}
Thanks

Qt Display tooltip after mouse hover on QGraphicsPixmapItem

I'm using QGraphicView to show game map that consist QGraphicsPixmapItems. I need to show tooltip on mouse hover at QGraphicsPixmapItem.
For saving QGraphicsPixmapItem position I using MazeItem:
#ifndef MAZEITEM_H
#define MAZEITEM_H
#include <QPoint>
#include <QGraphicsItem>
class MazeItem
{
private:
QPoint myPosition;
QString myToolTip;
public:
MazeItem();
QPoint position() const;
QString toolTip() const;
void setToolTip(const QString &toolTip);
void setPosition(const QPoint &position);
QPoint getPosition();
QGraphicsPixmapItem * pixmap;
};
#endif // MAZEITEM_H
I have widget class to display game map:
#include <QWidget>
#include <QtGui>
#include <QGraphicsView>
#include <QToolTip>
#include "mazeitem.h"
class MazeGUI : public QWidget
{
Q_OBJECT
private:
QGraphicsView * graphicsView;
QGraphicsScene * graphicsScene;
QString sceneString;
int imageSize;
QList<MazeItem> mazeItems;
void addItem(int x, int y, QPixmap picture);
bool event(QEvent *event);
int itemAt(const QPoint &pos);
public:
explicit MazeGUI(QWidget *parent = 0);
void setScene(QString sceneString);
signals:
public slots:
void redraw();
};
#endif // MAZEGUI_H
In constructor I set mouse tracking.
MazeGUI::MazeGUI(QWidget *parent) :
QWidget(parent)
{
setMouseTracking(true);
...
}
This is how I add new maze item.
void MazeGUI::addItem(int x, int y, QPixmap picture)
{
MazeItem mazeItem;
mazeItem.setPosition(QPoint(x, y));
mazeItem.setToolTip("text");
mazeItem.pixmap = this->graphicsScene->addPixmap(picture);
mazeItem.pixmap->setPos(y, x);
mazeItems.append(mazeItem);
}
And this I have from Qt tutorials,
bool MazeGUI::event(QEvent *event)
{
if (event->type() == QEvent::ToolTip) {
// HERE - it never goes here!!
QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
int index = itemAt(helpEvent->pos());
if (index != -1) {
QToolTip::showText(helpEvent->globalPos(), mazeItems[index].toolTip());
} else {
QToolTip::hideText();
event->ignore();
}
return true;
}
return QWidget::event(event);
}
int MazeGUI::itemAt(const QPoint &pos)
{
for (int i=0; i < mazeItems.size(); ++i)
{
if (mazeItems[i].getPosition() == pos)
return i;
}
return -1;
}
Was adding the tooltip on wrong object:
Instead of:
mazeItem.setToolTip("text");
It should be:
mazeItem.pixmap->setToolTip("text");

how to make parallel events in c++ (specific Qt 4.6)

I have a problem with the execution of multiple functions at once.
Specific , I have two classes (MyRect and space). The Idea is similar to space invaders but I stuck on the beginning. In class MyRect I have defined two rectangles :: body (ship body) and bullet (ship bullet).In the main class space is created ship as MyRect new object with body && bullet rectangles.Also there is definition for keyPress events.
Problem is that when I fire bullet , everythig stops excetp moving of bullet and until loop MyRect::fireBullet(int x, int y) is done , i cant move ship during this event.Obviously, I do some fundamental mistakes so if someone is willing to clarify this.
here is sample code ::
MyRect.h
#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
#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
#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
#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
#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();
}
Thanks for the answers.
You have an architectural problem. What you are doing is moving the bullet in a loop in its fireBullet method. While that loop is running, the rest of the program isn't, since a single thread can only be doing one thing at a time.
The solution is to refactor your code so that everything on the screen gets updated by one frame-worth of animation every time you call some kind of update method. Basically, you just need to retain enough state, where you are, how fast you are moving, how far you can go before disappearing, so that you can move by the desired amount each frame.
Another thing that you would change is to have the keyPressEvent update the state of the spaceship to know which way it should be moving, so that it can move on its regular paintEvent. For that you can use a QTimer