You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, January 28th 2012, 2:45pm

show picture element on QWindow

Hello all!

Im trying to create a simple memory Game on Qt and 'I think my basic background code for the logical handling has been nearly finished.
However I have a problem with the GUI related to the ui data.

1. I try to make a new QPushButton(a Card Object I created which is derivated from PushButton) and whenever I create it in the constructor of my QMainWindow object
and set Stylesheet it is shown in the window, BUT when I do the same in every other function of the QMAinWindow it doesnt show my pictureButton. I thought maybe
it has something to do with the paint function??? But I dont know much about when to use that...
2. When debugging it says error when disassembling: "cannot access memory at adress ....", but I dont know how to fix problems with this debugger..

Oh right, a new programming wonder which also appeared right now is: even when i can display now the button in the constructor the border image cant be seen on the window, just the grey button..

Does anyone has a clue how to fix this? I really dont understand the graphic system in the qtcreator programm,
#include "karte.h"
#include "spielbrett.h"
using std::cout;
using std::endl;

spielbrett::spielbrett(int length1, int height1): length(length1), height(height1), size(length1*height1){

Karte* card1 = new Karte(this);
card1->setStyleSheet("border-image: url(:/new/prefix1/beer.jpg);");

2

Saturday, January 28th 2012, 3:27pm

cannot access memory at adress

this probably means you have done something wrong with pointers. You should show moree of you code - especially mentioning the line that causes the error.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Saturday, January 28th 2012, 5:25pm

/*
* File: spielbrett.cpp
* Author: Reini
*
* Created on 07. Dezember 2011, 17:17
*/

//#include <algorithm
#include <iostream>
#include "karte.h"
#include "spielbrett.h"
using std::cout;
using std::endl;

spielbrett::spielbrett(int length1, int height1): length(length1), height(height1), size(length1*height1){

Karte* card1 = new Karte(this);
card1->setStyleSheet("border-image: url(:/new/prefix1/beer.jpg);"); // QPushbutton on mainwindow instanciated, displays only when created in constructor why???

}

spielbrett::spielbrett(const spielbrett& orig) {

}

spielbrett::~spielbrett() {
}

void spielbrett::fillSpielbrett()
{



int idSize = length*height;
//I. Karten erstellen + cardOnBoard true setzen
for(int i = 1; i<=idSize; i++)
{
cards.push_back(new Karte(this));
cards.at(i-1)->setCardOnBoard(true);
}



//II. karten Ids füllen + StringNamen
srand( (unsigned)time( NULL ) );
int picIDSize = (length*height)/2;
int count= 1;
for(int i = 0; i < idSize; i++){
if(count > picIDSize)
count = 1;
cards.at(i)->setPicID(count);
cards.at(i)->setID(i+1);
switch(count){
case 1: cards.at(i)->setCardName("beer.jpg)");
break;
case 2: cards.at(i)->setCardName("patrick.jpg)");
break;
case 3: cards.at(i)->setCardName("pikachu.jpg)");
break;
case 4: cards.at(i)->setCardName("sonne.jpg)");
break;
case 5: cards.at(i)->setCardName("spongebob.jpg)");
break;
case 6: cards.at(i)->setCardName("thaddäus.jpg)");
break;
case 7: cards.at(i)->setCardName("thaddäus2.jpg)");
break;
case 8: cards.at(i)->setCardName("Basketball.png)");
break;

}


count++;
}
//karten ids müssen jetzt durchgemischt werden
//jede karte wird mit anderer karte vertauscht
for(int i = 0; i < idSize; i++){
int number = createRandomValues(i,idSize-1);
cout <<"nummer zufällig: " << number<<endl;

swapCards(cards.at(i),cards.at(number));
count++;
}


//III: fill coordsAndCards Vector
//größe = länge * breite
//for in for loop
int brettSize = -1;
for(int i = 1; i <= length; i++)
{
//i = xcoord, y = ycoord
//brettSize geht von 0 bis lenght*height

for(int j = 1; j<= height; j++)
{
brettSize++;
//vector.at(x) = 1.pair stelle
//vector.at(x).first.first = xCoord
//vector.at(x).first.second = yCoord
//vector.at(x).second = Karte*
allCoordsAndCards.push_back(std::make_pair(std::make_pair(i,j), cards.at(brettSize)) );




}
}

//IV: fill CardsBrettCoords
int x = 30;
int y = -10;
brettSize = -1;
for(int i = 1; i <= length; i++)
{
//i = xcoord, y = ycoord
//brettSize geht von 0 bis lenght*height
y+=40;


for(int j = 1; j<= height; j++)
{
if(x>300)
x= 30;
brettSize++;
//vector.at(x) = 1.pair stelle
//vector.at(x).first.first = xCoord
//vector.at(x).first.second = yCoord
//vector.at(x).second = Karte*
CardsBrettCoords.push_back(std::make_pair(x,y));
x+= 90;




}

cout <<"blub";
}





}

bool spielbrett::checkSlotEmpty(int slotPlace)
{
if(allCoordsAndCards.at(slotPlace).second->getCardOnBoard() == false)

return true;
else
return false;
}
vector<pair<pair<int,int>,Karte*> > spielbrett::getAllCoordsAndCards()
{
return allCoordsAndCards;
}

int spielbrett::getSize()
{
return size;
}

vector<Karte*> spielbrett::getCards()
{
return cards;
}

int spielbrett::createRandomValues(int min, int max){
//srand( (unsigned)time( NULL ) );


repaint();
int blubb = time(NULL) * 0.77;
if(min>=max)
return max;
return ( blubb % (max-min)) + min;
}

int spielbrett::getLength(){
return length;
}

int spielbrett::getFoundPlace1()
{
return foundPlace1;
}
int spielbrett::getFoundPlace2()
{
return foundPlace2;
}

void spielbrett::setFoundPlace1(int place){
foundPlace1 = place;
}

void spielbrett::setFoundPlace2(int place){
foundPlace2 = place;
}
bool spielbrett::checkCardsLeft(){

for(int i = 0; i < allCoordsAndCards.size(); i++)
{
if(checkSlotEmpty(i) ==false)
return true;

}
return false;
}

//alle Karten mit gespeichertem String werden auf ihrem
//Platz angezeigt von allCoordsAndCardsVector
//setStylesheet von allen Karten

void spielbrett::showAllCardsOnBrett()
{

cout <<"function: showAllCards";
// QString borderTag = "border-image: url(:/";


for(int i = 0; i < allCoordsAndCards.size(); i++)
{
//jedes Kartenbild anzeigen lassen
// QString cardName = cards.at(i)->getCardName();
cards.at(i)->setStyleSheet("border-image: url(:/beer.jpg)");


cards.at(i)->move(CardsBrettCoords.at(i).first, CardsBrettCoords.at(i).second);
//position:1,1 1,2 1,3 etc...
//Karte an richtiger Stelle anzeigen lassen
}

}


so this is my full spielbrett.cpp class :D
its just the funny thing, that my Karte* object isnt on the window when i put it in any other function than the constructor
I dont get errors while compiling but my pushbutton doesnt appear when creating it

this is the log of debugger when i stopp it:
0 ntdll!LdrFindResource_U C:\Windows\system32\ntdll.dll 0 0x77b2000d
1 ntdll!RtlQueryTimeZoneInformation C:\Windows\system32\ntdll.dll 0 0x77baf896
2 ?? 0 0x737a7715
3 ?? 0

4

Saturday, January 28th 2012, 6:02pm

firstly, please modify your post to use code tags [ code] [/code] (remove spaces), and format it so that it is readable.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 1 times, last edit by "Amleto" (Jan 28th 2012, 6:15pm)


5

Saturday, January 28th 2012, 6:08pm

which line causes the crash?

you dont show what setCardOnBoard does.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 1 times, last edit by "Amleto" (Jan 28th 2012, 6:15pm)


6

Saturday, January 28th 2012, 6:17pm

setCardOnBoard just tells the Karte class that the "boolean" CardOnBoard shall be true

7

Saturday, January 28th 2012, 6:39pm

your code is too hard to read. I dont see anywhere where you set the positions of the coords. CODE TAGS please

edit:
found something, but tlooks like you're doing it twice for different containers allCoordsAndCards and cardsbrettcoords, which is just plain asking for trouble.

You should use your debugger to look at the x/y values that get into the move(...) method.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 2 times, last edit by "Amleto" (Jan 28th 2012, 6:47pm)


8

Saturday, January 28th 2012, 7:39pm

ok, i know what you mean with the 2 containers, they both have pointers which directs to the same class but isnt that allowed?
but the move method which is called in the function showAllCardsOnBrett()

"cards.at(i)->move(CardsBrettCoords.at(i).first, CardsBrettCoords.at(i).second);"

isnt called in the main cpp or somewhere else during the executation.. or could this still causes problems due to my button problem?
Basically my whole Karte class isnt important for what I like to do. I just need to create a QPushButton and set its stylesheet on my window,
like I did in the constructor...

9

Saturday, January 28th 2012, 7:58pm

I'm not concerned about the 'shared' pointers, I'm concerned about the duplication of coordinates.

you said your cards arent shown - if you never move() them, they will all be in exactly the same place.

You shoul dbe solving your issues in this order - crash, buttons not showing, stylesheet.


It's very frustrating to be only shown partial code - only now you tell me that showAllCardsOnBrett isnt called at all. and you still HAVENT ADDED CODE TAGS. This is why I suggest showing compilable example - people invariable hide 'unimportant' details, which turn out to either waste time or not be quite so unimportant.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 2 times, last edit by "Amleto" (Jan 28th 2012, 8:07pm)


10

Sunday, January 29th 2012, 1:50pm

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * File:   spielbrett.cpp
 * Author: Reini
 *
 * Created on 07. Dezember 2011, 17:17
 */

//#include <algorithm
#include <iostream>
#include "karte.h"
#include "spielbrett.h"
using std::cout;
using std::endl;

spielbrett::spielbrett(int length1, int height1): length(length1), height(height1), size(length1*height1){
    Karte* card1 = new Karte(this);
    card1->setStyleSheet("border-image: url(:/new/prefix1/beer.jpg);");
    card1->move(length,height);

}

spielbrett::spielbrett(const spielbrett& orig) {

}

spielbrett::~spielbrett() {
}

void spielbrett::fillSpielbrett()
{

    Karte* card1 = new Karte(this);
    card1->setStyleSheet("border-image: url(:/new/prefix1/beer.jpg);");
    card1->move(50,50);
   // delete[] card1;


      int idSize = length*height;
     //I. Karten erstellen + cardOnBoard true setzen
      for(int i = 1; i<=idSize; i++)
      {
          cards.push_back(new Karte(this));
          cards.at(i-1)->setCardOnBoard(true);
      }



     //II. karten Ids füllen + StringNamen
     srand( (unsigned)time( NULL ) );
     int picIDSize = (length*height)/2;
     int count= 1;
     for(int i = 0; i < idSize; i++){
         if(count > picIDSize)
             count = 1;
         cards.at(i)->setPicID(count);
         cards.at(i)->setID(i+1);
         switch(count){
         case 1: cards.at(i)->setCardName("beer.jpg)");
             break;
         case 2: cards.at(i)->setCardName("patrick.jpg)");
             break;
         case 3: cards.at(i)->setCardName("pikachu.jpg)");
             break;
         case 4: cards.at(i)->setCardName("sonne.jpg)");
             break;
         case 5: cards.at(i)->setCardName("spongebob.jpg)");
             break;
         case 6: cards.at(i)->setCardName("thaddäus.jpg)");
             break;
         case 7: cards.at(i)->setCardName("thaddäus2.jpg)");
             break;
         case 8: cards.at(i)->setCardName("Basketball.png)");
             break;

         }


         count++;
     }
     //karten ids müssen jetzt durchgemischt werden
     //jede karte wird mit anderer karte vertauscht
      for(int i = 0; i < idSize; i++){
        int number = createRandomValues(i,idSize-1);
        cout <<"nummer zufällig: " << number<<endl;

        swapCards(cards.at(i),cards.at(number));
         count++;
     }


    //III: fill coordsAndCards Vector
    //größe = länge * breite
    //for in for loop
    int brettSize = -1;
    for(int i = 1; i <= length; i++)
    {
        //i = xcoord, y = ycoord
        //brettSize geht von 0 bis lenght*height

        for(int j = 1; j<= height; j++)
        {
             brettSize++;
            //vector.at(x) = 1.pair stelle
            //vector.at(x).first.first =  xCoord
            //vector.at(x).first.second = yCoord
            //vector.at(x).second = Karte*
            allCoordsAndCards.push_back(std::make_pair(std::make_pair(i,j), cards.at(brettSize)) );




        }
    }

    //IV: fill CardsBrettCoords
    int x = 30;
    int y = -10;
    brettSize  = -1;
    for(int i = 1; i <= length; i++)
    {
        //i = xcoord, y = ycoord
        //brettSize geht von 0 bis lenght*height
        y+=40;


        for(int j = 1; j<= height; j++)
        {
            if(x>300)
                x= 30;
             brettSize++;
            //vector.at(x) = 1.pair stelle
            //vector.at(x).first.first =  xCoord
            //vector.at(x).first.second = yCoord
            //vector.at(x).second = Karte*
            CardsBrettCoords.push_back(std::make_pair(x,y));
            x+= 90;




        }

        cout <<"blub";
    }





}

bool spielbrett::checkSlotEmpty(int slotPlace)
{
    if(allCoordsAndCards.at(slotPlace).second->getCardOnBoard() == false)

        return true;
    else
        return false;
}
vector<pair<pair<int,int>,Karte*> > spielbrett::getAllCoordsAndCards()
{
    return allCoordsAndCards;
}

int spielbrett::getSize()
{
    return size;
}

vector<Karte*> spielbrett::getCards()
{
    return cards;
}

int spielbrett::createRandomValues(int min, int max){
    //srand( (unsigned)time( NULL ) );


    repaint();
    int blubb = time(NULL) * 0.77;
    if(min>=max)
        return max;
    return ( blubb % (max-min)) + min;
}

int spielbrett::getLength(){
    return length;
}

int spielbrett::getFoundPlace1()
{
    return foundPlace1;
}
int spielbrett::getFoundPlace2()
{
    return foundPlace2;
}

void spielbrett::setFoundPlace1(int place){
    foundPlace1 = place;
}

void spielbrett::setFoundPlace2(int place){
    foundPlace2 = place;
}
bool spielbrett::checkCardsLeft(){

    for(int i = 0; i < allCoordsAndCards.size(); i++)
    {
        if(checkSlotEmpty(i) ==false)
            return true;

    }
    return false;
}

//alle Karten mit gespeichertem String werden auf ihrem
//Platz angezeigt von allCoordsAndCardsVector
//setStylesheet von allen Karten

void spielbrett::showAllCardsOnBrett()
{

    cout <<"function: showAllCards";
   // QString borderTag = "border-image: url(:/";


   for(int i = 0; i < allCoordsAndCards.size(); i++)
   {
      //jedes Kartenbild anzeigen lassen
     //  QString cardName = cards.at(i)->getCardName();
//       cards.at(i)->setStyleSheet("border-image: url(:/beer.jpg)");


//       cards.at(i)->move(CardsBrettCoords.at(i).first, CardsBrettCoords.at(i).second);
      //position:1,1  1,2   1,3 etc...
      //Karte an richtiger Stelle anzeigen lassen
   }

}



Spielbrett.h :

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * File:   spielbrett.h
 * Author: Reini
 *
 * Created on 07. Dezember 2011, 17:17
 */

#ifndef SPIELBRETT_H
#define	SPIELBRETT_H
#include <time.h>
#include "Karte.h"
#include "mainwindow.h"
#include <string>

#include "qwidget.h"
#include <vector>
using std::vector;
using std::pair;
using std::string;

class spielbrett:public MainWindow{
public:
    spielbrett(int lenght1, int height1);
    spielbrett(const spielbrett& orig);
    virtual ~spielbrett();
    int getCurrentPos(int x, int y);
    bool checkSlotEmpty(int slotPlace);
    void fillSpielbrett(); // mit x*y karten
    bool removeCardPair(pair<Karte*,Karte*> p);
    bool checkCardsLeft();//falls alle cards vom brett sind
    vector<pair<pair<int,int>,Karte*> > getAllCoordsAndCards();
    int getSize();
    vector<Karte*> getCards();

    void showAllCardsOnBrett();
    void swapCards(Karte* card1, Karte* card2)
    {
        int picIdTemp = card1->getPicID();
        int IDTemp    = card1->getID();
        card1->setPicID(card2->getPicID());
        card1->setID(card2->getID());
        card2->setPicID(picIdTemp);
        card2->setID(IDTemp);
    }
    int createRandomValues(int min, int max);
    int getLength();
    void setFoundPlace1(int place);
    void setFoundPlace2(int place);
    int getFoundPlace1();
    int getFoundPlace2();

private:
    int length;
    int height;
    int size;
    int foundPlace1;
    int foundPlace2;
    vector<Karte*> cards;
    vector<pair<pair<int,int>,Karte*> > allCoordsAndCards;
    vector<pair<int,int> > CardsBrettCoords;



};

#endif	/* SPIELBRETT_H */


with code tags... and this is my main:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <QtGui/QApplication>
//#include "mainwindow.h"
#include "spielbrett.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    spielbrett* w = new spielbrett(4,4);
    w->show();
    w->fillSpielbrett();





    return a.exec();
}


Karte.cpp

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
34
35
36
37
38
39
40
41
42
43
/*
 * File:   Karte.h
 * Author: Reini
 *
 * Created on 23. November 2011, 01:10
 */

#ifndef KARTE_H
#define	KARTE_H
#include "qpushbutton.h"
#include "mainwindow.h"
using std::string;

class Karte: public QPushButton{
public:

    Karte(MainWindow* w);
    Karte(const Karte& orig);
    virtual ~Karte();
    void setPicID(int id);
    int getPicID();
    void setID(int id);
    int getID();
    void setCardOnBoard(bool b);
    bool getCardOnBoard();
    void setCardName(QString name);
    QString getCardName();

private:

    int picId;
    int ID;
    bool cardOnBoard;
    QString cardName; //example "beer.jpg)"






};

#endif	/* KARTE_H */


I am sorry but with the debugger of the QTCreator programm i dont know how to use
thats all I can show

11

Sunday, January 29th 2012, 2:51pm

Learning how to use a debugger is not optional for programmer. You cannot come to a forum for every small problem that would take 30s to solve using the debugger.

As you have said - the move(x,y) is never called - so of course you will not see multiple cards in different positions.

In fillSpielbrett, you only ever setstylesheet on 'card1', and you dont save that in 'cards'! Obviously no image will be on any Karte where you do not set one.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.