-1

我正在使用 Qt 在 C++ 中实现一个带有 GUI 的纸牌游戏。

我有一个main.cpp,它通过调用window.cpp 中的Window 类来创建一个窗口。在 window.cpp 中,我们有一堆框架和其他小部件,它们稍后将形成 GUI。

然后是 card.cpp 中的 Card 类,其中包含有关卡片的信息。Deck.cpp 中的 Deck 类具有特定类型的卡片数量。最后,gui_cards_kingdom.cpp 从卡片的图像(卡片中的路径)和卡片中的信息(也在卡片中)构造一个小部件。gui_cards_kingdom 定义了一个继承自 Widget 的类,并包含一个 Deck 实例(以便能够提取所需的信息)。

到现在为止还挺好。在window.cpp 中,我创建了一个Card 实例,并将其放入Deck 的一个实例中,我将其发送到Cards_kingdom(gui_cards_kingdom.cpp 中的类)。

在 gui_cards_kingdom.cpp 中的 img->setPixmap(image) 行中使用 QPixmap 时出现分段错误

调试器告诉我信息在那里,一切都准备就绪,除了我使用 QPixmap 将卡的图像加载到标签上时。

这不是路径,因为我在 main.cpp 中测试了一些简单的代码(请参阅 main.cpp 中注释掉的代码)。我只使用了 img->setPixmap(image); 只是要确定。

我在我的知识的极限。我搜索了很多,没有找到任何东西,因此,我问你们,你们在这里看到了什么,我错过了?

提前感谢您的时间和帮助,

-比阿特丽斯

主.cpp:

#include <QtGui>
#include <window.h>

int main(int argc, char *argv[]){
QApplication app(argc, argv);

//QWidget fenetre;
//QLabel *label = new QLabel(&fenetre);
//label->setPixmap(QPixmap("Images/Cards/Base/village.jpg"));
//label->move(30, 20);
//    fenetre.show();

Window window;
window.show();
return app.exec();

}

卡.h:

#ifndef CARD_H
#define CARD_H

#include <string>

class Card {
public:
    Card(int price, std::string info, std::string path, std::string id);
    int get_price();
    std::string get_info();
    std::string get_path();
    std::string get_id();
private:
    int price;
    std::string info;   /* Text on card     */
    std::string path;   /* Path to image    */
    std::string id;     /* Name/ID of card  */
};
#endif // CARD_H

卡.cpp:

#include <card.h>

Card::Card(int a, std::string b, std::string c, std::string d){
    price = a;
    info  = b;
    path  = c;
    id    = d;
}

int Card::get_price(){
    return price;
}

std::string Card::get_info(){
    return info;
}

std::string Card::get_path(){
    return path;
}

std::string Card::get_id(){
    return id;
}

甲板王国.h:

#ifndef DECK_KINGDOM_H
#define DECK_KINGDOM_H

#include <card.h>

class Deck_kingdom {
public:
    Deck_kingdom(int size_deck, Card* card_type);
    int get_count();
    Card* get_card();
private:
    Card* card;
    int count;
};
#endif // DECK_KINGDOM_H

甲板王国.cpp:

#include <deck_kingdom.h>

Deck_kingdom::Deck_kingdom(int size_deck, Card *new_card){
    count = size_deck;
    card = new_card;
}

int Deck_kingdom::get_count(){
    return count;
}

Card* Deck_kingdom::get_card(){
    return card;
}

gui_cards_kingdom.h:

#ifndef GUI_CARDS_KINGDOM_H
#define GUI_CARDS_KINGDOM_H

#include <QtGui>
#include <QApplication>
#include <QFrame>
#include <QWidget>
#include <QLabel>
#include <QVBoxLayout>
#include <QString>
#include <sstream>
#include <string>
#include <QPixmap>

#include <deck_kingdom.h>

#define CARD_HEIGHT 135
#define CARD_WIDTH  85

class Cards_kingdom : public QWidget {

public:
    Cards_kingdom(Deck_kingdom * input_deck);    /* Constructor  */

/*
 * Price and Counter are display in the same label, then are wrapped with Icon in a vertical layout, inside a frame.
 */
private:
    QLabel       *img;       /* Icon         */
    QLabel       *info;     /* Nb Cards left*/
    QVBoxLayout  *layout;    /* Layout       */
    QFrame       *pack;      /* Frame        */
    Deck_kingdom *deck;      /* Deck         */
};

#endif // GUI_CARDS_KINGDOM_H

gui_cards_kingdom.cpp:

#include <gui_cards_kingdom.h>
#include <iostream>

Cards_kingdom :: Cards_kingdom(Deck_kingdom * input_deck) : QWidget(){

    deck = input_deck;

    //DEBUG
    std:: cout << deck->get_card()->get_price() << std::endl;

    /* Convert "price" and "count" into string */
    std::ostringstream price, count;
    price << deck->get_card()->get_price();
    count << deck->get_count();
    std::string text_label ("$"+price.str()+" ("+count.str()+")");

    //QString test = QString::fromStdString(deck->get_card()->get_path());

    //img->setPixmap(QPixmap(test).scaled(CARD_WIDTH,CARD_HEIGHT,Qt::IgnoreAspectRatio, Qt::FastTransformation ));
    QPixmap image  = QPixmap("Images/Cards/Base/village.png");
    img->setPixmap(image); // THE PROBLEM IS HERE

    img->setToolTip(QString::fromStdString(deck->get_card()->get_info()));
    info->setText(QString::fromStdString(text_label));
    layout = new QVBoxLayout();
    layout->addWidget(img);
    layout->addWidget(info);
    pack->setLayout(layout);
    pack->setParent(this);

}

窗口.h:

#ifndef WINDOW_H
#define WINDOW_H

#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QFrame>
#include <gui_cards_kingdom.h>

class Window : public QWidget {
    public:
    Window();

    protected:
    QVBoxLayout *layout_1;
    QHBoxLayout *layout_2;
    QGridLayout *layout_3;
    QFrame *box_1;
    QFrame *box_2;
    QFrame *box_layout_2;
    QFrame *box_3;
    QFrame *box_4;
};
#endif // WINDOW_H

窗口.cpp:

#include <window.h>
#include <iostream>

Window::Window(){
    /* Window       */

    setFixedSize(900,700);
    move(10,10);


    layout_1 = new QVBoxLayout(this);       /* Global Vertical Column      */
    layout_2 = new QHBoxLayout;                /* Include Kingdom and Log     */
    layout_3 = new QGridLayout;                /* Matrix display Kingdom cards*/


    /* Kingdom cards box       */
    box_1 = new QFrame(this);
    box_1->setFrameShape(QFrame::StyledPanel);
    box_1->setGeometry(30, 20, 300, 300);
    box_1->setToolTip("box_1");
    box_1->setLayout(layout_3);

    Card * test_card = new Card(2,"Village: +2 Action, +1 Buy", "Images/Cards/Base/village.jpg", "Village");
    Deck_kingdom * test_deck = new Deck_kingdom(10, test_card);

    std::cout << "Here 2a" << std::endl;
    Cards_kingdom * test_gui = new Cards_kingdom(test_deck);
    std::cout << "Here 2b" << std::endl;

    layout_3->addWidget(test_gui,0,0);

    std::cout << "Here 2" << std::endl;
    /* Log */
    box_2 = new QFrame(this);
    box_2->setFrameShape(QFrame::StyledPanel);
    box_2->setGeometry(340, 20, 300, 300);
    box_2->setToolTip("box 2");

    std::cout << "Here 2" << std::endl;

    layout_2->addWidget(box_1);
    layout_2->addWidget(box_2);

    box_layout_2 = new QFrame(this);
    box_layout_2->setLayout(layout_2);

    std::cout << "Here 2" << std::endl;
    /* Player Board*/
    box_3 = new QFrame(this);
    box_3->setFrameShape(QFrame::StyledPanel);
    box_3->setGeometry(30, 350, 650, 100);

    /* Chat room*/
    box_4 = new QFrame(this);
    box_4->setFrameShape(QFrame::StyledPanel);
    box_4->setGeometry(30, 460, 650, 50);


    layout_1->addWidget(box_layout_2);
    layout_1->addWidget(box_3);
    layout_1->addWidget(box_4);

    setLayout(layout_1);
}
4

1 回答 1

2

我的 C++ 很烂,但我知道 PyQt4,所以我们开始...

您确定要在构造函数中初始化 QLabel 指针吗?

Cards_kingdom :: Cards_kingdom(Deck_kingdom * input_deck) : QWidget(){
    ...
    img = new QLabel;
    QPixmap image  = QPixmap("Images/Cards/Base/village.png");
    img->setPixmap(image); 
于 2012-08-04T05:36:14.800 回答