C++ 指针 GUI QT

标签 c++ qt user-interface pointers

<分区>

我可能完全找错了树,但它确实编译了,只是崩溃了。我最近 2 周才在做 C++,主要来自 Java 背景,这不是很广泛!!

任何人,我有一个房间类。我想设置4个导出,北、南、东、西。我有创建一个 setExits() 方法的想法,该方法采用 4 个 Room 指针。每个 Room 对象都有一个调用该指针的 goNorth() 等方法。

Room::Room(QString name, QString shortD, QString longD, QImage roomImage)
{
    this->name = name;
    this->shortD = shortD;
    this->longD = longD;
    this->roomImage = roomImage;
}

QString Room::shortDescription()
{
    return shortD;
}

QString Room::longDescription()
{
    return longD;
}

QImage Room::showImage()
{
    return roomImage;
}

void Room::setExits(Room *north, Room *south, Room *east, Room *west)
{
    this->north = north;
    this->south = south;
    this->east = east;
    this->west = west;
}

Room* Room::goNorth()
{
    return north;
}

我是这样调用和设置的。

void TheIslandMain::createRooms()
{
    //Room *forest, *lake; - declare as global in theislandmain.h?
    forest = new Room(readRooms("../Island/Rooms/forest.name"), readRooms("../Island/Rooms/forest.short"), readRooms("../Island/Rooms/forest.long"), getImage("../Island/Rooms/forest.jpg"));
    //forest->setExits(lake, currentRoom, currentRoom, currentRoom);

    lake = new Room(readRooms("../Island/Rooms/lake.name"), readRooms("../Island/Rooms/lake.short"), readRooms("../Island/Rooms/lake.long"), getImage("../Island/Rooms/lake.jpg"));
    currentRoom = forest;
}

当单击 gui 中的按钮时,我使用以下代码。

void TheIslandMain::on_northButton_clicked()
{
    ui->roomDesc->clear();
    currentRoom = lake;
    //currentRoom = currentRoom->goNorth();
    ui->actionLabel->setText(currentRoom->shortDescription());
    ui->imageArea->setPixmap(QPixmap::fromImage(currentRoom->showImage()));
}

我已经注释掉了我想使用的代码,这给出了错误。现在的代码可以正常工作。当我再次取消注释代码时,它会编译文件,但一旦我单击 North 按钮并崩溃,我就会收到 255 错误。

我什至不确定这样做是否合法,这只是我在考虑的事情。我完全是在吠叫错误的树吗?就像我说的,我只是在学习 C++,我们实际上还没有在类里面对指针做任何事情。

我对注释代码的理解是,我正在将 currentRoom 变量的内存引用更改为 Room 对象中的引用 north,它本身取自主类中定义的引用。

对不起,如果我是愚蠢的!

最佳答案

//forest->setExits(lake, currentRoom, currentRoom, currentRoom); 如果取消注释,显然会导致问题,因为 lake 未初始化。

关于C++ 指针 GUI QT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14851523/

相关文章:

c++ - 数值参数化集的模板与构造函数参数

python - 一起调试boost暴露的Python和C++

linux - 当使用 -platform eglfs 运行 Qt5 应用程序时,i.MX6 上的 "EGL Error : could not create the EGL surface: error 0x300b"

c++ - 如何对齐小部件?

language-agnostic - 关于如何构建出色命令行界面的灵感

Java GUI JList 应用程序

c++ - std::unique 与谓词比较 std::string 不删除重复项

c++ - 如何限制C++中rand函数生成的数字

python-2.7 - PyQt 4 : Get Position of Toolbar

html - html页面中的无框文本字段