C++Qt QPixmap分割错误



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

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

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

到目前为止,一切都很好。在window.cpp中,我创建了一个Card的实例,并将其放在Deck的实例中,将其发送给Cards_kingdom(gui_Cards_kingdy.cpp中的类)

在ui_cards_kingdom.cpp中img->setPixmap(image)行使用QPixmap时,我遇到了分割错误

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

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

我的知识已经到了极限。我找了很多,没有找到任何东西,所以,我问你们,你们在这里看到了什么,我错过了?

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

-Béatrice

main.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();
}

card.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

card.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;
}

deck_kind.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

deck_kingdom.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_kind.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);
}

window.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

window.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);
}

我的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); 

相关内容

  • 没有找到相关文章

最新更新