Note that there are some explanatory texts on larger screens.

plurals
  1. POQt: update pixmap grid layout with 2d array values
    primarykey
    data
    text
    <p>I am undertaking a game using a combination of c++ in visual studios 2010 and Qt 4.7 (both windows). The game is a clone of battleship and is console input based. I have created my gui how I want it to look, and on the Qt side in Qt designer, my gui consists of a grid layout 10x10, using labels to hold pixmaps of game cells:</p> <p><img src="https://i.stack.imgur.com/D3bAE.png" alt="current look of the game"></p> <p>I have painstakingly named each label to represent its position in the 2d array (ie. fleet map => <code>F_00</code> => <code>F[0,0]</code> => <code>F[i],[j]</code>). I can manually choose what pixmap I would like to display using the properties editor, but I would like something dynamic.</p> <p>I use an update mapboard class to redraw the game board after a player fires, which keeps storing over a char array. I would like to update my pixmaps for each, using a generic getupdatearray type function. As we traverse the array it will update the pixmap currently associated with individual labels to match their cousins from the array. (say <code>F[5][6] = 'X'</code> for hit, then when the loops got to that position in the array it would update the grid of pixmaps at F_56 to equal <em>hit.png</em>, replacing the <em>empty.png</em>.</p> <p>I have an idea how to make the loop that would accomplish this, but unsure how i would go about getting the pixmap for each label to be more along the lines of a runtime feature versus the now compile time (static) feature. I have read about QPainter and another Qt class that deals with images, but still having a hard go at it.</p> <p>Question to any of you, how do I update these pixmaps based on a 2d array?</p> <ol> <li>loop structure - i can figure out</li> <li>condition statements - i can figure out</li> <li>qt specific syntax dealing with labels- newbie so no idea atm.</li> </ol> <p>Here's some pseudocode of the kind of thing I am trying to do with map.h:</p> <pre><code>#include &lt;QtCore&gt; #include &lt;QtGui&gt; // WARNING: PSEUDOCODE, DOES NOT COMPILE // AT A LOSS ON HOW TO SELECT THE CORRECT LABEL // MAYBE A CHILD CLASS FOR THAT? class map { public: char updateboard(char mapname, char b[][10]){ for(int i=0;i&lt;10;i++){ for(int j=0;j&lt;10;j++){ char C = b[i][j]; if (C == 'M'){//miss Qlabel mapname_[i][j](&lt;img src='images/missspace.png'/&gt;); mapname_[i][j].show(); } else if(C == 'X'){//hit Qlabel mapname_[i][j](&lt;img src='images/hitspace.png'/&gt;); mapname_[i][j].show(); } else if(C == ' '){//undiscovered space Qlabel mapname_[i][j](&lt;img src='image/emptyspace.png'/&gt;); mapname_[i][j].show(); } } } } }; </code></pre> <p>Then in my mainwindow.cpp, I include map.h and say:</p> <pre><code>// calls class function update board // takes updated array values and replaces old pixmap with new map.updateboard(T,b[][10]); // target map update map.updateboard(F,v[][10]); // fleet map update </code></pre> <p>Thanks in Advance</p> <p><strong>UPDATE:</strong></p> <p>I've gotten to the point where I can swap pixmaps with buttons presses, but I would like to create something more dynamic. I wanted to use a Qstring in which I place the name of the label I want to change using by appending x y values using:</p> <pre><code>TR_position.append(QString::number(xvalue)); </code></pre> <p>When I try to call it using:</p> <pre><code>ui-&gt;TR_position-&gt;setPixmap(QPixmap(":/images/missspace.png")); </code></pre> <p>...it obviously doesnt work. Is there a way to type case it, or use the contents of the string as the Qlabel name?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload