2012-08-04 94 views
-1

我正在使用Qt在C++中使用GUI实现纸牌游戏。C++ Qt QPixmap分段错误

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

再有就是在card.cpp类卡,以及有关的相关信息卡。 deck.cpp中的甲板牌具有某种类型的牌的数量。最后,gui_cards_kingdom.cpp从卡片的图像(卡片中的路径)和卡片上的信息(也卡片中)构建一个小部件。 gui_cards_kingdom定义一个继承自Widget的类,并且包含一个Deck实例(以便能够获取所需的信息)。

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

我在该行中gui_cards_kingdom.cpp使用的QPixmap时img-> setPixmap(图)

调试器告诉我,该信息是存在的,一切都在发生,只是当我使用的QPixmap得到分割故障将卡的图像加载到标签上。

它不是路径,如我测试在main.cpp中一些简单的代码(参见main.cpp中的注释的代码)。我去了img-> setPixmap(image);只是要确定。

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

预先感谢您,您的时间,和你的帮助,

-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 

卡.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_kingdom.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_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); 

} 

在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); 
} 
+1

我一直在滚动,并想知道代码何时会结束:-) – jdi 2012-08-04 05:29:08

+0

哈哈哈,是的,我知道了!我很抱歉有这么多,但它跨越了3层,所以我只是想避免“哦,你没有给所有的代码”:p – 2012-08-04 05:48:29

回答

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); 
+0

嗯,它的工作原理!非常感谢您的帮助!它实际上是有道理的,而不是实例化指针= seg故障!我会记住这一点! 再次感谢您! – 2012-08-04 05:51:59

+0

我刚刚得到一个C++问题的权利热情和模糊。大声笑 – jdi 2012-08-04 06:02:41

+1

@jdi做得好:) – 2012-08-04 06:22:11