QPixmap Image doesnt appear - c++

Im new with Qt and im making a game in it. I want to display icon but after putting there some code i cant find it anywhere. Im confused because i dont get any errors, so im pretty sure that i just didnt add something. Help.
Combat.h file
#ifndef COMBAT
#define COMBAT
#include <QImage>
#include <QGraphicsPixmapItem>
class Combat: public QGraphicsPixmapItem{
public:
// constructors
//Combat(QPixmap *parent=NULL);
Combat(const QString x);
// setters/getters
void getOwner(QString x);
private:
QString owner;
};
Combat.cpp
#include "Combat.h"
#include <QGraphicsScene>
Combat::Combat(const QString x){
// draw graphics
setPixmap(QPixmap(x));
}
void Combat::getOwner(QString x){
owner = x;
}
#endif // COMBAT
Interface.h
#ifndef INTERFACE
#define INTERFACE
#include <QList>
#include "Hex.h"
#include "Combat.h"
class Interface{
public:
// constructors
Interface();
// getters/setters
QList<Hex*> getHexes();
void getOwner(int x);
// public methods
void placeHexes();
void placeCombat();
private:
void createHexColumn(int x, int y, int numOfRows);
QList<Hex*> hexes;
void createCombatIcon(int x, int y, QString z);
int owner;
};
#endif // INTERFACE
Interface.cpp
#include "Interface.h"
#include "Game.h"
extern Game* game;
Interface::Interface(){
}
QList<Hex *> Interface::getHexes(){
return hexes;
}
void Interface::placeHexes(){
createHexColumn(100,100,6);
}
void Interface::placeCombat()
{
createCombatIcon(100,100,":/grafika/atak_magiczny.png");
}
void Interface::createCombatIcon(int x, int y, QString z)
{
Combat* icon = new Combat(z);
icon->getOwner("player1");
icon->setPos(x,y);
game->scene->addItem(icon);
}
Im not sure where something is missing

I solved the problem by adding my graphics to resource file in Qt Creator...

Related

getting an declaration error in header file

i am developing a breakout game using C++ in qt creator. i am getting an error saying "Game has not been declared". i have declared it using the game.h the error is in the header file. i cannot figure out where the problem is. please, any help would be highly appriciated.
#ifndef BALL_H
#define BALL_H
#include<QCloseEvent>
#include <QGraphicsRectItem>
#include "game.h" //i have declared it here.
class Ball: public QObject, public QGraphicsRectItem{
Q_OBJECT
public:
// constructors
Ball(QGraphicsItem* parent=NULL);
QTimer *runTimer;
// public methods
double getCenterX();
public slots:
// public slots
void move();
void start_timer();
void stop_timer();
void call_game_fuction(Game *gm); //here i am getting the error(Game)
private:
// private attributes
double xVelocity;
double yVelocity;
int counter = 0;
// private methods
void resetState();
bool reverseVelocityIfOutOfBounds();
void handlePaddleCollision();
void handleBlockCollision();
};
#endif // BALL_H
and this is the fuction of the CPP file
Game *obj1 =new Game();
game_function *obj2 = new game_function();
void Ball::call_game_fuction(Game *gm)
{
gm->set_background();
}
sir this is my game.h file
#ifndef GAME_H
#define GAME_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include "Ball.h"
#include "Paddle.h"
#include "Block.h"
#include<QPushButton>
class Game:public QGraphicsView{
Q_OBJECT
public:
// constructors
Game(QWidget* parent=0);
QPushButton *button;
QPushButton *button1;
// public methods
void start();
void createBlockCol(double x);
void creatBlockGrid();
void set_background();
void background_Gamewon();
void set_buttons();
QGraphicsScene* scene2;
// public attributes
QGraphicsScene* scene;
QGraphicsView* view;
private slots:
void startgame();
void stopgame();
private:
bool gameOver;
Ball *ball;
Paddle *pad;
Block *bl;
};
#endif // GAME_H
You have a cyclic dependency. Ball.h includes game.h, and game.h includes Ball.h. This is an impossible situation for the compiler to resolve, as neither one can be included before the other.
It appears that game.h does not need to #include "Ball.h". Instead, use a forward declaration:
class Ball;
That should be enough to compile game.h.

C++ public inheritance [SFML]

I've been having trouble drawing a sprite of hero to the screen(which I solved with a different approach). Problem was caused because of inheritance. Now I'm gonna put all the code and tell the story alongside with codes.
Character.h (the parent class for hero and npcs)
#pragma once
#include <SFML/Graphics.hpp>
#include "TextureHolder.h"
using namespace sf;
using namespace std;
class Character
{
protected:
Vector2f m_Position;
int m_Speed;
public:
Character();
void spawn(int x, int y, string direction);
Sprite m_Sprite;
Texture m_Texture;
void virtual update(float elapsedTime);
};
Character.cpp has nothing except constructor and update function declerations
#include "stdafx.h"
#include "Character.h"
Character::Character(){}
void Character::update(float elapsedTime) {}
Hero.h
#pragma once
#include "Character.h"
using namespace sf;
using namespace std;
class Hero : public Character
{
private:
public:
Hero();
void virtual update(float elapsedTime);
};
And finally Hero.cpp
#include "stdafx.h"
#include "Hero.h"
Hero::Hero()
{
m_Texture = TextureHolder::GetTexture("images/chars/hero.png");
m_Sprite.setTexture(m_Texture);
}
void Hero::update(float elapsedTime)
{
}
With the classes this way, When I try to draw hero.m_Sprite it gives access violation.
As you see I'm using public inheritance from character to hero(which should have worked perfectly, since when I type "hero." it shows me list of it's public members and m_sprite is among them)
But when I try to add m_Sprite and m_Texture in Hero.h(instead of inheriting them from Character class , I got no errors.
What is the reason behind this?
Edit: draw.cpp
#include "stdafx.h"
#include "Engine.h"
void Engine::draw()
{
m_Window.clear(Color::Black);
m_Window.setView(m_MainView);
m_Window.draw(hero.m_Sprite);
m_Window.display();
}
EDIT 2: IT doesn't give errors anymore. I didn't change anything and I don't have any idea what caused and what fixed the problem.

Register a class with a manager, then call the sub classes overridden methods

OK so i have a few problems.
1. I need to be able to register the childclass when its created with game for broadcasting.
2. I need to be able to call the child class's method from an array of GameObject Pointers.
3. I need it to be the actual classes, not just copys of them.
In the file systemvars.h i have my global methods and some variables.
#pragma once
//=================================
// include guard
#ifndef _SYSTEMVARS_H_
#define _SYSTEMVARS_H_
//=================================
// forward declared dependencies
class Game;
//=================================
// included dependencies
#include <iostream>
using namespace std;
//global enums
const enum CLASSTYPE{CLASSTYPE_NULL,CLASSTYPE_PLAYER,CLASSTYPE_DUNGION,CLASSTYPE_ENTITY,CLASSTYPE_MAP,CLASSTYPE_MENU};
//Global methods
void setGameRefrence(Game*);
Game* getGameRefrence();
systemvars.cpp: definitions
#include "SystemVars.h"
static Game* curentgame;
void setGameRefrence(Game* mygame)
{
curentgame = mygame;
}
Game* getGameRefrence()
{
return curentgame;
}
In my Game.h: a holder for gameobjects
#pragma once
//=================================
// include guard
#ifndef _GAME_H_
#define _GAME_H_
//=================================
// forward declared dependencies
class GameObject;
//class Player;
//=================================
// included dependencies
#include "SystemVars.h"
#include "GameObject.h"
//#include "Player.h"
#include <iostream>
using namespace std;
//=================================
// the actual class
class Game
{
public:
Game(void);
void registerGameObject(GameObject*);
void unregisterGameObject(GameObject*);
void sendMessageToAllObjects(string message,CLASSTYPE recipeint);
~Game(void);
private:
GameObject *gameobjects2[1000];
int numberofobject;
};
#endif
the game.cpp gameclass definition
#include "Game.h"
//#include "Player.h"
Game::Game(void)
{
setGameRefrence(this);
//logHelperMessage(INFO,1,"Registerd game");
numberofobject = 0;
}
void Game::registerGameObject(GameObject* newobj)
{
//logHelperMessage(INFO,1,"Registerd");
newobj->setId(numberofobject);
gameobjects2[numberofobject] = newobj;
numberofobject++;
//gameobjects.push_back(newobj);
}
void Game::unregisterGameObject(GameObject* objtodie)
{
//logHelperMessage(INFO,1,"Unregister");
for(int i = objtodie->getId();i < numberofobject - 1;i++)
{
gameobjects2[i] = gameobjects2[i+1];
gameobjects2[i]->setId(i);
}
gameobjects2[numberofobject-1] = nullptr;
numberofobject--;
}
void Game::sendMessageToAllObjects(string message,CLASSTYPE recipeint)
{
for(int i = 0; i < numberofobject;i++)
{
cout << "Sent the message from game");
//((Player *)gameobjects2[i])->sendMessage(message);
//static_cast<Player*>(gameobjects2[i])->sendMessage(message);
}
}
Game::~Game(void)
{
}
Gameobject.h: the parent of my inner game classes.
#pragma once
//=================================
// include guard
#ifndef _GAMEOBJECT_H_
#define _GAMEOBJECT_H_
//=================================
// forward declared dependencies
enum CLASSTYPE;
//=================================
// included dependencies
#include <iostream>
#include "SystemVars.h"
using namespace std;
//=================================
// the actual class
class GameObject
{
public:
GameObject();
GameObject(CLASSTYPE mytype);
~GameObject(void);
virtual void sendMessage(string data);
virtual CLASSTYPE getMyClassType();
virtual void setMyClassType(CLASSTYPE newrecip);
void setId(int val);
int getId();
protected:
CLASSTYPE _MYCURRENTCLASSTYPE;
int myid;
};
#endif
Gameobject.cpp
#include "GameObject.h"
GameObject::GameObject() : _MYCURRENTCLASSTYPE(CLASSTYPE_NULL)
{
//do not register
}
GameObject::GameObject(CLASSTYPE mytype): _MYCURRENTCLASSTYPE(mytype)
{
//register this object into the gameobject list.
getGameRefrence()->registerGameObject(this);
}
GameObject::~GameObject(void)
{
getGameRefrence()->unregisterGameObject(this);
}
void GameObject::sendMessage(string data)
{
//logHelperMessage(INFO,1,"Recived te message in GameObject");
cout << "Recived te message in GameObject";
}
CLASSTYPE GameObject::getMyClassType()
{
return _MYCURRENTCLASSTYPE;
}
void GameObject::setMyClassType(CLASSTYPE newrecip)
{
}
void GameObject::setId(int val)
{
myid = val;
}
int GameObject::getId()
{
return myid;
}
Player.h:
pragma once
//=================================
// include guard
#ifndef _PLAYER_H_
#define _PLAYER_H_
//=================================
// forward declared dependencies
//=================================
// included dependencies
#include "SystemVars.h"
#include "GameObject.h"
//=================================
// the actual class
class Player : public GameObject
{
public:
Player();
void sendMessage(string data) override;
void test();
}
Player.cpp:
Player::Player() : GameObject(CLASSTYPE_PLAYER)
{
}
void Player::sendMessage(string data)
{
//logHelperMessage(INFO,1,"Recived the message in Player");
cout << "Recived the message in Player";
//logHelperMessage(INFO,1,data.c_str());
cout << data;
}
void Player::test()
{
cout << "Sent message";
getGameRefrence()->sendMessageToAllObjects("Test",CLASSTYPE_PLAYER);
}
main.cpp
#pragma once
#include "SystemVars.cpp"
#include "Player.h"
#include "Game.h"
Game mygame;
int main(int argc, char **argv)
{
setGameRefrence(&mygame);
Player newplayer = Player();
newplayer.test();
}
Now that that is all out of the way.
The expected output is:
Sent message
Sent message from game
Recived message in Player
but insted i get:
Sent message
Sent message from game
Recived message in Gameobject
Im pretty sure i have a sliceing problem, but im not sure what to do about it, or where it is.
So, any ideas gents?
ALso, i tried to cut back the classes a bit so im not posting 2000~ lines of code. So if anyhting missing let me know.
About a dozen classes inherit from gameobject. and i need them all to talk to eachother in one way or another.
Just by having CLASSTYPE_PLAYER, etc. makes me concerned that you're trying to subvert/reinvent C++ OOP. You already have the concept of 'class' in the language, why re-invent it? Forgive me if I'm wrong. Aside from this, I suggest you look up publisher-subscriber frameworks to do the kind of thing you're after, or implement your own. I'm sure somebody will answer your C++ issue more directly.
Edit: tone of first comment.

c++ error C2065 : undeclared identifier [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
C++ Undeclared Identifier (but it is declared?)
Im getting the error sprite.h(20): error C2065: 'Component' : undeclared identifier when I try to compile (I got a couple other files as well). Below is the sprite.h file. I cant for the life of me figure out what is causing this problem.
#ifndef SPRITE_H
#define SPRITE_H
#include "Image.h"
#include "Rectangle.h"
#include <string>
#include <SDL.h>
#include <vector>
#include "Component.h"
namespace GE2D {
class Sprite {
public:
Sprite();
Sprite(Image *i);
Sprite(Image *i, int x, int y);
Sprite(char *file, bool transparentBg, int x, int y, int w, int h);
virtual ~Sprite();
virtual void tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components);
virtual void handleEvent(SDL_Event eve);
virtual void draw(SDL_Surface *screen);
void setPosition(int x, int y);
const Rectangle& getRect() const;
const Image& getImage() const;
const Sprite& operator=(const Sprite& other);
Sprite(const Sprite& other);
protected:
private:
Image image;
Rectangle rect;
};
}
#endif
In the .cpp file tick() is defined like this:
void Sprite::tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components) {}
tick() is supposed to take two vectors like they do now, but maybe there's a better way to do that which might solve this problem?
EDIT
As requested, here is Component.h as well:
#ifndef COMPONENT_H
#define COMPONENT_H
#include "Rectangle.h"
#include "Component.h"
#include "Sprite.h"
#include <vector>
#include <SDL.h>
namespace GE2D {
class Component {
public:
Component();
virtual ~Component();
virtual void draw(SDL_Surface *screen) = 0;
virtual void tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components) = 0;
virtual void handleEvent(SDL_Event eve) = 0;
const Rectangle& getRect() const;
protected:
Component(int x, int y, int w, int h);
private:
Rectangle rect;
};
}
#endif
Sprite.h includes Component.h which includes Sprite.h, giving a circular dependency which can't be resolved.
Luckily, you don't need to include the headers at all. Each class only refers to a pointer to the other class, and for that a simple declaration is enough:
class Component;

Qt 'Rectangle' is not a type - when rectangle is declared as a class

Im having a problem of my Rectangle class not being seen as a type. I've included the proper header, and so I am confused.
shapes.h
#ifndef SHAPES_H
#define SHAPES_H
#include "Colors.h"
#include <QPoint>
#include "glwidget.h"
//class GLWidget;
class Shape
{
public:
virtual void draw();
};
class Rectangle : Shape
{
public:
Rectangle(GLWidget *w, QPoint tl, QPoint br){
glWidget = w;
topLeft = tl;
btmRight = br;
}
virtual void draw(){
// top horizontal
for(int i = topLeft.x(); i < btmRight.x(); i++){
glWidget->setPixel(i,topLeft.y(), color);
}
}
private:
QPoint topLeft,btmRight;
GLWidget *glWidget;
RGBColor color;
};
#endif // SHAPES_H
glwidget.cpp
#include <QtGui>
#include <QtOpenGL>
#include <math.h>
#include <stdio.h>
#include "glwidget.h"
#include "Shapes.h"
#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE 0x809D
#endif
// ... a bunch of code that doesn't need to be included
void GLWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
// do some drawing stuff
QPoint mPos = event->pos();
switch(drawmode)
{
case 1:
currentShape = new Rectangle(this,mPos, mPos); /*** This is the error ***/
}
}
}
glwidget.h
#ifndef AGLWIDGET_H
#define AGLWIDGET_H
#include <QGLWidget>
#include "Colors.h"
class Shape;
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
GLWidget(QWidget *parent = 0);
~GLWidget();
QSize minimumSizeHint() const;
QSize sizeHint() const;
void setPixel(int x, int y, RGBColor c);
public slots:
void setColor(RGBColor c);
void setDrawRectangle();
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private:
QPoint lastPos;
QVector<QPoint> drawPoints;
RGBColor paintColor;
int drawmode;
Shape *currentShape;
};
Sorry for the load of code... the exact error is
'Rectangle' is not a type glwidget.cpp line 85
Anybody have an idea why it wouldn't be seeing Rectangle as a type in glwidget.cpp despite my including "Shapes.h"?
Thanks in advance!
This is a bit of a longshot, but are you sure you're using moc correctly in regards to the GLWidget code? IE, have you added #include "glwidget.moc to the .cpp file or included it in your build system (qmake knows to do this for you), as well as running moc first. I only mention this because forgetting to do this many moons ago caused me to see a pile of inscrutable type-related warnings and errors.
Perhaps somewhere in the ancestry of GLWidget there is a method or member called Rectangle and there is a confusion. See the documentation for GLWidget and its ancestors
Looks like the compiler believes Rectangle is a template
Well I'm gonna go with it had something to do with the virtual function within Shape not being defined as in g++ undefined reference to typeinfo. The machine I had the strange error on is using an older version of Qt than I have on my personal machine, and my personal is having no issues with this code.
Thanks for the suggestions everyone, but I'm just gonna put this one to rest.