Question

I am implementing a card game, with a GUI, in C++, using Qt.

I have a main.cpp, that create a window by calling the class Window in window.cpp. In window.cpp, we have a bunch of frames and other widgets that will later form the GUI.

Then there is the class Card in card.cpp, with infos about the cards. The class Deck, in deck.cpp, has the number of card of a certain type. Finally, gui_cards_kingdom.cpp constructs a widget from the image of the card (path in Card) and infos from card (also in Card). gui_cards_kingdom define a class that inherits from Widget, and contain an instance of Deck (to be able to pull the infos needed).

So far, so good. In window.cpp, I create an instance of Card, and put it in an instance of Deck, that I sent to Cards_kingdom (class in gui_cards_kingdom.cpp).

I get a segmentation fault when using QPixmap in gui_cards_kingdom.cpp at the line img->setPixmap(image)

The debugger tells me that the info is there, everything is in place, except when I use QPixmap to load the image of the card onto a label.

It is not the path, as I tested some simple code in main.cpp (see the commented out code in main.cpp). I went down to just img->setPixmap(image); just to be sure.

I am at the limit of my knowledge. I searched a lot, and did not find anything, therefore, I am asking you guys, what do you see in here, that I missed?

Thank you in advance, for your time, and your help,

-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_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);
}
Was it helpful?

Solution

My C++ sucks, but I know PyQt4, so here we go...

Are you sure you are initializing the QLabel pointer in your constructor?

Cards_kingdom :: Cards_kingdom(Deck_kingdom * input_deck) : QWidget(){
    ...
    img = new QLabel;
    QPixmap image  = QPixmap("Images/Cards/Base/village.png");
    img->setPixmap(image); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top