C++ replying to a QFrame signal origin

Apr 27, 2018 at 8:57pm
Hi, so I'm dealing with a slight wall I met in my coding project.

I'm making a card game from a pre-written code using QFrame, and at this point I'm trying to implement the logic to send all cards from one deck widget to another deck widget after I send a signal for it.

Deck "deck_" sends signal to "pickedCards_" using signal, but "pickedCards_" has to send the cards to "deck_" without using signal.

What I initially tried was

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    Deck deck;
    for (;;){
        if (layout_->currentWidget() != nullptr)
        {
            layout_->currentWidget();
            Card* card = static_cast<Card*>(layout_->currentWidget());
            deck.receiveCard(card);
            layout_->removeWidget(layout_->currentWidget());
            layout_->setCurrentIndex(layout_->count()-1);
            card->setAttribute(Qt::WA_DeleteOnClose);
            card->close();
        }else{
            return;
        }
    }


where "Deck" is the class that deck_ is an object of. The problem I have in this code, is that I'm creating a new Deck object and as it has not been initialized in my mainWindow, it's not being drawn at all. At least, that's what I Think is the problem. The cards are properly being removed from the pickedCards_, and they do end up somewhere, but they're not drawn which leads me to believe that they're simply in a new Deck object that is not being drawn.

I read the documentation a bit and found out that

QObject::sender(); returns a pointer to the sender of the signal, but trying to use this with

1
2
3
QObject* deck;
deck = QObject::sender();
deck->receiveCard(card);


does not work with the error code "request for member 'receiveCard' in 'deck', which is a pointer type 'QObject*' ".

So I'm basically asking how I could securely send the card widgets back to the original deck_ object.

I also tried adding a function to my pickedCards class definition with

 
receiveDeck(Deck* deck);


but it would not compile and instead complained about Deck not being defined (even though it's included in the .hh).
Apr 27, 2018 at 10:26pm
does not work with the error code "request for member 'receiveCard' in 'deck', which is a pointer type 'QObject*' ".

If Deck inherits from QObject, then that QObject* can be dynamic_cast into Deck*.
(Qt might have some cast of its own for the same purpose too.)
Apr 28, 2018 at 8:45am
This works, awesome!

For some reason, I'm now getting a SIGSEV, segmentation fault in the class I'm sending the object to. I've been trying to debug it for a while, but from what I can tell the object type I'm trying to append into the data structure in the class is of the correct type.

The class initially creates cards with

1
2
3
4
5
6
7
8
9
10
11
    for (int suit = SPADE; suit != SUIT_COUNT; ++suit){

        for (unsigned value = 1; value <= 13; ++value){
            auto card = new Card(static_cast<CardSuit>(suit), value, this);
            int xOffset = (this->width() - card->width()) / 2;
            int yOffset = (this->height() - card->height()) / 2;
            card->move(xOffset, yOffset);
            cards_.push_back(card);
        }
        random_shuffle(std::begin(cards_), std::end(cards_));
    }


and I'm trying to repopulate cards_ in a function with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Deck::receiveCard(Card* cardObj)
{
    /*
    std::cout << "I'm here now " << std::endl;
    card->turn();
    int xOffset = (this->width() - card->width()) / 2;
    int yOffset = (this->height() - card->height()) / 2;
    card->move(xOffset, yOffset);
    card->show();
    cards_.push_back(card);
   /*

    Card* card = static_cast<Card*>(cardObj);
    cards_.push_back(card);

} */


The commented lines didn't work out, and I tried to pinpoint the problem, but I've no clue why I can't even push_back() the card into my cards_ struct.
Topic archived. No new replies allowed.