1

I try to create a tiled base map by using Qt GUI: I have two classes: one to set my tile images that inherits from QGraphicPixmapItem, one to load my map from a text file that is a 2D array composed of the numbers 1, 0 and 2, and add it to a scene: But my program quits unexpectedly and I do not know where is the break:

my tile class:

class mapTile: public QGraphicsPixmapItem
{
public:
    enum Tile {DOOR, GRASS, FIRE};

    mapTile(): QGraphicsPixmapItem(),currentTile(GRASS){
        syncBitmap();
    }

    Tile getTile() const {return currentTile;}

    void setTile(Tile newTile){
        if(currentTile!= newTile){
            currentTile=newTile;
            syncBitmap();
        }
    }

private:
    void syncBitmap() {//Set my image to my tile
        switch(currentTile) {
        case DOOR:
            image->setPixmap(QPixmap(":/mpa/castledoors.png"));
        case GRASS:
            image->setPixmap(QPixmap(":/map/grass3_blur.jpg"));
        case FIRE:
            image->setPixmap(QPixmap(":/map/feu1/png"));
        }
    }

    Tile currentTile;
    QGraphicsPixmapItem *image;

};

my class map:

Map.h:

class Map
{

public:
    static const int TILE_SIZE=20; //value of the tile in pixels!!!->20X20=a tile
    void paintMap(QGraphicsScene *scene);
    Map();

 private:

    static const int WIDTH= 13;// width of the grid cell
    static const int HEIGHT= 9;//height  of the grid cell

    //my grid cell map !!!!
    int array[WIDTH][HEIGHT];
    mapTile *tile; //my tile

};

And Map.cpp

/*Create a default Map*/
Map::Map()
{
    QFile myfile("path to my file");
    if (!myfile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
         QMessageBox::information(0, "error", myfile.errorString());
    }

    QTextStream in(&myfile);
    QString line ;
    QStringList fields;
    int i=0;
    int j=0;

    while (!in.atEnd()) {
        //Fill my array with my list--> convert Qtring into Int
        for (i=0; i<HEIGHT; i++ ) {

           line = in.readLine();
           fields = line.split(" ");

           for(j=0;j<WIDTH;j++)
           {
               //cout<<fields[j].toInt();
                array[i][j] = fields[j].toInt();
           }
        }
    }
}



//Paint my map with my tile
void Map::paintMap(QGraphicsScene *scene){

    int i=0, j=0;
    tile= new mapTile();

    for (i=0; i<HEIGHT; i++){
        for(j=0; j<WIDTH; j++){

            switch(array[i][j]){

            case 0:

                tile->setTile(mapTile::GRASS);
                tile->setPos(i,j);
                scene->addItem(tile);
                j+=TILE_SIZE;

            case 1:

                tile->setTile(mapTile::FIRE);
                tile->setPos(i,j);
                scene->addItem(tile);
                j+=TILE_SIZE;
            case 2:

                tile->setTile(mapTile::DOOR);
                tile->setPos(i,j);
                scene->addItem(tile);
                j+=TILE_SIZE;
            }

            i+=TILE_SIZE;//
        }
    }

}

and finally my mainwindow (I directly give the file.cpp):

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    myMap= new Map;
    scene= new QGraphicsScene(this);

    myMap->paintMap(scene);
    ui->graphicsView->setScene(scene);

}

MainWindow::~MainWindow()
{
    delete ui;
}

Sorry guys for the long post but I am stuck! Any ideas on what I missed?

4

2 回答 2

1

好吧,您的代码中至少存在五个问题:

  • myTile将它们添加到场景时,您不能共享相同的实例。每次向场景中添加图块时,都需要创建一个新图块。目前,您只在Map::paintMap.

  • 您的myTile类继承自QGraphicsPixmapItem,但是它也有一个QGraphicsPixmapItem *image;未初始化的成员(因此它是一个漫游指针),但随后它使用它image->setPixmap(QPixmap(":/mpa/castledoors.png"));会崩溃。相反,您只想调用setPixmap(QPixmap(":/mpa/castledoors.png"))(调用超类的函数)。

  • 在上面的项目中,您可能已经注意到您将“map”拼错为“mpa”,但这不是导致崩溃的原因。

  • 和inmapTile::syncPixmap一样Map::paintMap,您忘记break;switch. 没有这些,所有的瓷砖都会看起来像火瓷砖。

  • 您将平铺大小添加到ij迭代器,就好像它们是像素坐标一样,但同时您将它们用作数组索引。您需要对像素坐标使用单独的变量,或者在调用ij乘以。TILE_SIZEsetPos

祝你好运!

于 2014-10-14T19:30:31.947 回答
0

我终于找到了解决问题的方法:我在我的函数 paintMap 中添加了一个场景,用于将每个项目添加到我的场景中,并创建了一个返回场景的 QGraphicScene* getScene(),然后我只需编写即可将其称为我的 mainWindow

ui->graphicsView->setScene(myMap->getScene())

此外,在我的mapTile课堂上,我不再使用函数 setTile()。我删除了它,然后QGraphicPixmapItem *image用 a改变QPixmap image了我的功能,syncbitmap()this->setPixmap(image.scaled(TILE_SIZE,TILE_SIZE));在开关结束时做了!为什么?因为我的图块已经是一个QGraphicsPixmapItem(继承),所以我只需要为它设置一个像素图!我把它放在开关的末尾,因为如果我之前设置了图像,图像将永远不会改变,因为它已经设置好了!最后在我删除的构造函数中currentTile(GRASS),在我的构造函数中添加了一个 Tile 变量作为参数,currentTile= name of your variable然后将其放入构造函数中,然后调用 syncBitmap 函数。所以在 paintMap() 你只需要做:

tile= new mapTile(mapTile::GRASS);
tile->setPos(j*TILE_SIZE,i*TILE_SIZE);
scene->addItem(tile);

在每种情况下!瞧!我希望这将有助于像我这样的新手,至少了解背后的逻辑!我不确定这是最好的方法,但这种方法对我有用!:)

于 2014-10-16T07:59:43.117 回答