You are not logged in.

radekas

Beginner

  • "radekas" is male
  • "radekas" started this thread

Posts: 28

Location: Czech rep.

  • Send private message

1

Tuesday, April 17th 2007, 10:37pm

help me please with QGridLayout

Can anybody help me?

I have set 64 of mywidget objects.
Now i need to get to some of them.
How can i get them?
for example: what is the name of mywidget in row2 column 4


QGridLayout *grid = new QGridLayout;
for (int row = 0; row < 8; ++row) {
for (int column = 0; column < 8; ++column) {
MyWidget *mywidget=new MyWidget();
connect(mywidget,SIGNAL(tah(int,int)),this,SLOT(setCard(int,int)));
grid->addWidget(mywidget,row,column);
grid->setMargin(0);
grid->setSpacing(0);
}
}

bolt18_80

Intermediate

  • "bolt18_80" is male

Posts: 186

Location: Ohio, US

Occupation: Programmer/Analyst

  • Send private message

2

Wednesday, April 18th 2007, 3:09am

RE: help me please with QGridLayout

Any time you need to get access to the sending object inside a slot you can use the sender() method of QObject to return the current sender. For example...

Source code

1
2
3
4
void MainWindowImpl::buttonclicked()
{
    //  Here sender() would return a pointer to the button that was clicked.
}


If you have questions about this you can also type "sender" into QAssistant and the documentation might be able to answer some questions for you.

Good Luck!

Chris
Christopher Springer
Programmer/Analyst
NBEC/NWOCA - Ohio SSDT
Website: http://www.moderncpp.org
cspringer@moderncpp.org

radekas

Beginner

  • "radekas" is male
  • "radekas" started this thread

Posts: 28

Location: Czech rep.

  • Send private message

3

Wednesday, April 18th 2007, 6:28pm

RE: help me please with QGridLayout

thanks a lot for answer
but now i am facing other problem

i have

class Card : public QWidget


but when i am compiling it writes me error

invalid conversion from QObject* to Card*

in slot
void MyMainImpl::setCard()
{
Card *card=new Card();
card=sender();
}

saubue

Trainee

Posts: 129

Location: Germany

  • Send private message

4

Wednesday, April 18th 2007, 9:20pm

You'll need to cast the sender to a Card:

Source code

1
2
Card *card = qobject_cast<Card *>(sender());
// You might want to check against NULL here

And why exactly do you allocate memory for a Card and throw away the pointer afterwards?

radekas

Beginner

  • "radekas" is male
  • "radekas" started this thread

Posts: 28

Location: Czech rep.

  • Send private message

5

Wednesday, April 18th 2007, 9:33pm

hi
i need to make a memory game
there is 64 couples-card where picture is hidden and you see the picture after you click on the button
when you choose one you have to finde another with the same picture

so i made class card
but now i have to remember the first choosen card then the second choosen card and
then i have to see if they are same.
and thats why i need to get pointer to the cards

i am new in Qt and programming too so maybe you know some better way how to do it?

saubue

Trainee

Posts: 129

Location: Germany

  • Send private message

6

Wednesday, April 18th 2007, 9:47pm

The method itself is ok (another possibility would be to use QSignalMapper, so you don't need to cast the sender (which doesn't look too beatiful)), it's just that allocating space and then reassign its pointer means a memory leak. The allocated memory can never be freed again without a pointer.
Maybe the following will work (with an array of cards):

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
...
    // Inside the constructor or some setup function
    QSignalMapper *mapper = new QSignalMapper(this);
    for (int i = 0; i < NUM_CARDS; i++)
    {
         card[i] = new Card(...);   // Init card
         connect(card[i], SIGNAL(clicked()), mapper, SLOT(map()));
         mapper->setMapping(card[i], i);
    }
    connect(mapper, SIGNAL(mapped(int)), this, SLOT(cardClicked(int)));
    firstCard = NULL;  // First card clicked (of a pair)
..


// The slot that should be called if a card has been choosen
void MyClass::cardClicked(int num)
{
    if (firstCard)
    {
        if (firstCard->val == card[i]->val)   // Some sort of comparison if the cards match
        {
            // match
        }
        else    // miss
        {
            firstCard == NULL
        }      
    }
    else
    {
        firstCard = card[num];
    }
}