C++ QPixmap图像未显示
我是Qt的新手,我正在做一个游戏。我想显示图标,但放了一些代码后,我在任何地方都找不到它。我很困惑,因为我没有得到任何错误,所以我很确定我只是没有添加一些东西。救命啊 战斗.h文件C++ QPixmap图像未显示,c++,qt,C++,Qt,我是Qt的新手,我正在做一个游戏。我想显示图标,但放了一些代码后,我在任何地方都找不到它。我很困惑,因为我没有得到任何错误,所以我很确定我只是没有添加一些东西。救命啊 战斗.h文件 #ifndef COMBAT #define COMBAT #include <QImage> #include <QGraphicsPixmapItem> class Combat: public QGraphicsPixmapItem{ public: // constructors
#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;
};
#ifndef战斗
#定义战斗
#包括
#包括
职业格斗:公共QGraphicsPixmapItem{
公众:
//建设者
//战斗(QPixmap*父项=NULL);
战斗(const QString x);
//二传手
void getOwner(QString x);
私人:
QString所有者;
};
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
#包括“batch.h”
#包括
战斗::战斗(const QString x){
//画图
setPixmap(QPixmap(x));
}
无效战斗::getOwner(QString x){
所有者=x;
}
#endif//战斗
接口.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
#ifndef接口
#定义接口
#包括
#包括“十六进制h”
#包括“Combat.h”
类接口{
公众:
//建设者
接口();
//能手/二传手
QList getHexes();
void getOwner(int x);
//公共方法
空置六角体();
无效战斗();
私人:
void createHexColumn(int x、int y、int numorrows);
QList-hexes;
void createCombatIcon(int x,int y,QString z);
业主;
};
#endif//接口
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);
}
#包括“Interface.h”
#包括“Game.h”
外人游戏*游戏;
接口::接口(){
}
QList接口::getHexes(){
返回六角;
}
void接口::placeHexes(){
createHexColumn(100100,6);
}
void接口::placeCombat()
{
createCombatIcon(100100),:/grafika/atak_magiczny.png);
}
void接口::createCombatIcon(int x,int y,QString z)
{
战斗*图标=新战斗(z);
图标->获取所有者(“玩家1”);
图标->设置位置(x,y);
游戏->场景->附加项(图标);
}
我不确定缺少了什么东西。我通过将图形添加到Qt Creator中的资源文件解决了问题。我通过将图形添加到Qt Creator中的资源文件解决了问题。如何将项目添加到场景中?调用父构造函数:
Combat::Combat(const QString x):QGraphicsPixmapItem(){
@eyllansc,父类的默认构造函数被隐式调用。我不确定您的建议是否会更改任何内容。您确定您创建的QPixmap
有效吗?您应该选中“使用”。@eyllansc我正在使用Qt Creator,因此不应该自动添加它。如何将项目添加到场景中?调用父构造函数tor:Combat::Combat(const QString x):QGraphicsPixmapItem(){
@eyllansc,父类的默认构造函数被隐式调用。我不确定您的建议是否会更改任何内容。您确定您创建的QPixmap
有效吗?您应该选中“使用”。@eyllansc正在使用Qt Creator,因此不应该自动添加它?