Results 1 to 15 of 15

Thread: update grid of pixmap labels based on 2d char arrray value

  1. #1
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question update grid of pixmap labels based on 2d char arrray value

    Hello all, I am undertaking a game using a combination of c++ in visual studios 2010 and Qt 4.7 (both windows). I have created my gui how i want it to look, but now i would like to integrate it into the game files. The game is a clone of battleship and is console input based. I will keep with this, but I used an update mapboard class to redraw the game board after a player fires, which keeps storing over the char array.

    On the Qt side in Qt designer, my gui consists of a grid layout 10x10, using labels to hold pixmaps of game cells. I can manually choose what pixmap I would like to display using the properties editor, but I would something dynamic. I have painstakingly named each label to represent its position in the 2d array (ie. fleet map => F_00 => F[0,0] => F[i],[j])

    using this, 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 F[5][6] = 'X' 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 a hit.png, replacing the empty.png.

    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 Qtpainter and another Qt class that deals with images, but still having a hard go at it.

    Question to any of you, how do I update these pixmaps based on a 2d array?
    1.) loop structure - i can figure out
    2.) condition statements - i can figure out
    3.) qt specific syntax dealing with labels- newbie so no idea atm.

    Thanks in Advance

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,590
    Thanks
    13
    Thanked 1,586 Times in 1,515 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: update grid of pixmap labels based on 2d char arrray value

    One approach would to use the grid layout that contains the (anonymous) widgets to allow access by row and column. This makes mapping from underlying 2D array to widget trivial. For example:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class MainWindow: public QMainWindow {
    5. Q_OBJECT
    6. public:
    7. MainWindow(QWidget *p = 0):
    8. m_blankPixmap("blank.png"),
    9. m_otherPixmap("other.png")
    10. {
    11. QWidget *central = new QWidget(this);
    12.  
    13. m_layout = new QGridLayout(central);
    14. for(int r = 0; r < 10; ++r) {
    15. for(int c = 0; c < 10; ++c) {
    16. QLabel *label = new QLabel(central);
    17. label->setPixmap(m_blankPixmap);
    18. m_layout->addWidget(label, r, c, Qt::AlignCenter);
    19. }
    20. }
    21.  
    22. central->setLayout(m_layout);
    23. setCentralWidget(central);
    24.  
    25. setCellPixmap(3, 3, m_otherPixmap);
    26. }
    27.  
    28. void setCellPixmap(int row, int col, const QPixmap &pixmap) {
    29. QLabel *label = qobject_cast<QLabel*>( m_layout->itemAtPosition(row, col)->widget() );
    30. if (label) {
    31. label->setPixmap(pixmap);
    32. }
    33. }
    34. private:
    35. QGridLayout *m_layout;
    36. const QPixmap m_blankPixmap;
    37. const QPixmap m_otherPixmap;
    38. };
    39.  
    40. int main(int argc, char *argv[])
    41. {
    42. QApplication app(argc, argv);
    43.  
    44. MainWindow m;
    45. m.show();
    46. return app.exec();
    47. }
    48. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    wuts.the.martyr (6th December 2011)

  4. #3
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: update grid of pixmap labels based on 2d char arrray value

    If i understand this correctly you are doing something like this?

    Note that just as it's possible to use loops to set the images on the controls by name, it's possible to use loops to create the widgets in the first place. You basically would leave the area for the game board blank in the design tool, and then dynamically create the controls with code...attach them to the layout with code...and save pointers to them in 2D array. (You wouldn't access them by label name at that point, you'd index them just as you are indexing your board.)

    You could create your own class derived from QLabel (such as a GameCell class) which contained the information for your board cell and methods related to it. Then you wouldn't need an array of label widgets in parallel to an array representing your board. You'd simply have one array of objects that took care of both aspects of the implementation.
    this is from a user on stackoverflow, I like the idea, but I am totally lost with what you have given me. Would it be possible to add comments to some of the lines of code so i can follow it?

    This is what the gameloop window looks like now:



    I had created some code using a class that would update the pixmaps. I would like to make something like you suggested or what the other user mentioned. If i did use this method, would I need to use a Qframe as a container to hold all the widgets in a grid arrangment? Also if I change widget focus ( I have two main windows: game view, splash menu view), will I lose the Qframe under another widget?

    Thanks A bunch

    Wuts

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,590
    Thanks
    13
    Thanked 1,586 Times in 1,515 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: update grid of pixmap labels based on 2d char arrray value

    What I am doing in the example is cruder than the StackOverflow suggestions and closer to what you are already doing. I am creating 100 widgets, and the StackOverflow suggestion of using the Qt graphics view classes and QGraphicsGridLayout is another way to go about it.

    Lines 10 through 24 creates a 10x10 grid of QLabels using nested for() loops. Each label is initially set to a blank pixmap. This is a quick way of doing what your Designer UI has done with 100 individually created labels (have a look in your ui_*.h file). The layout is put into a QMainWindow for example purposes only, you could put it in a QFrame or QWidget. You will create two objects of this class to place in your main UI.

    setCellPixmap() shows how to obtain the address of the QLabel in a given cell in order to change its pixmap. Once you have this address you can do anything to that label. Rather than keep a pointer to each label in my own data structure I just fetch it from the layout when required.

    If you have an array of data then you could set the labels with a quick loop or two. Try this version of main() in the code above:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. MainWindow m;
    6. m.show();
    7.  
    8. QPixmap empty("empty.png");
    9. QPixmap hit("hit.png");
    10. char data[10][10] = {
    11. {'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
    12. {'X', 'X', 'X', 'X', 'X', 'X', 'X', ' ', 'X', 'X'},
    13. {'X', 'X', ' ', ' ', 'X', 'X', 'X', 'X', 'X', 'X'},
    14. {'X', 'X', 'X', 'X', 'X', 'X', ' ', ' ', 'X', 'X'},
    15. {'X', 'X', 'X', 'X', 'X', 'X', ' ', 'X', 'X', 'X'},
    16. {'X', 'X', 'X', ' ', 'X', 'X', ' ', 'X', 'X', 'X'},
    17. {'X', 'X', 'X', 'X', 'X', 'X', ' ', 'X', ' ', 'X'},
    18. {'X', 'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X'},
    19. {'X', ' ', ' ', ' ', 'X', 'X', 'X', 'X', 'X', 'X'},
    20. {'X', 'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X'},
    21. };
    22. for (int row = 0; row < 10; ++row) {
    23. for (int col = 0; col < 10; ++col) {
    24. if (data[row][col] == ' ')
    25. m.setCellPixmap(row, col, empty);
    26. else
    27. m.setCellPixmap(row, col, hit);
    28. }
    29. }
    30.  
    31. return app.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

    Also if I change widget focus ( I have two main windows: game view, splash menu view), will I lose the Qframe under another widget?
    Sorry, I have no idea what the question means.
    Last edited by ChrisW67; 1st December 2011 at 07:13. Reason: updated contents

  6. The following user says thank you to ChrisW67 for this useful post:

    wuts.the.martyr (6th December 2011)

  7. #5
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: update grid of pixmap labels based on 2d char arrray value

    Wow, this is exactly what I was looking for earlier. I really am starting to like Qt, seems pretty straight forward after you play with it for awhile. Anyways like you mentioned I would like to place both arrays of pixmaps into container widgets, either Qframe or Qwidget. So could I create the container in the designer, and just place the arrays inside by calling the code you show with a function call from the main program loop, or would it be easier to just do both at the same time by code alone? If so How would I get the code to go inside the container? Lastly, with my ui as of now, I have the pixmaps scaled to fit inside 20x20 labels that are arranged in a grid layout 250x250 (H/V centered 5x5 spacing 2x2 margins) am I still going to be able to have it like it appears in the image i attached? Thank you so much, you have shown me what I have been googling for who knows how long, I kept finding hints but no definitive ways of doing this. If I had known about the graphicsclass /grid layout method I would have been doing that instead.

    I tried out your code, cleaned up a few items to make it work. It will compile/run but gives me a window that is blank, I have tried adding a background image, widget, frame. nothing shows up on a brand new Qt gui application template. I basically disabled the premade mainwindow.cpp since all the implementation goes on in the header using your codes prototypes/functions. I will keep playing with it.

    oh and on the part you didn't understand from my last post...
    I am not sure how I should be doing this, but I have two main widgets as containers. one holds my pixmap grids that you see in the picture, the other one has the splash screen / start menu where you can select type of networked game you want. I thought you could use focus to bring each widget to the top most layer like you can in many other software programs. I may be wrong about this, since this stuff is still pretty confusing to me. Is this the correct way to do things, or should I be using another mainwindow ui and just call the game stuff after the user is done with the networked game type selection. I keep hearing "scenes", maybe this is what i need to be using instead, dunno. I will keep looking around in regards to this one for sure, since I want my game to look clean and run smoothly.

    again many thanks for all your help

    Wuts


    mainwindow.h code:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include <QtCore>
    4. #include <QtGui>
    5. #include <QDebug>
    6. #include "ui_mainwindow.h"
    7.  
    8. class MainWindow: public QMainWindow, private Ui::MainWindow {
    9. Q_OBJECT
    10. public:
    11. MainWindow(QWidget *p = 0):QMainWindow(p) ,m_missPixmap("images/missspace.png"),m_hitPixmap("images/hitspace.png"), m_emptyPixmap("images/emptyspace.png"){
    12. QWidget *central = new QWidget(this);
    13.  
    14. m_layout = new QGridLayout(central);//gridlayout centered in mainwindow
    15.  
    16. for(int r = 0; r < 10; ++r) {//creates a 2d array of emptyspaces
    17. for(int c = 0; c < 10; ++c) {
    18. QLabel *label = new QLabel(central);
    19. label->setPixmap(m_emptyPixmap);
    20. m_layout->addWidget(label, r, c, Qt::AlignCenter);
    21. }
    22. }
    23.  
    24. central->setLayout(m_layout);//updates layout on central widget with new gridlayout in the center
    25. setCentralWidget(central);//ditto
    26. }
    27.  
    28. void setCellPixmap(int row, int col, const QPixmap &pixmap) {//in main.cpp fuction to label controls of widgets with matching [i][j] info
    29. QLabel *label = qobject_cast<QLabel*>( m_layout->itemAtPosition(row, col)->widget() );
    30. if (label) {
    31. label->setPixmap(pixmap);
    32. }
    33. }
    34.  
    35. private:
    36. QGridLayout *m_layout;
    37. const QPixmap m_emptyPixmap;
    38. const QPixmap m_hitPixmap;
    39. const QPixmap m_missPixmap;
    40. };
    41.  
    42. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    main.cpp code:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QLabel>
    3. #include <QtCore>
    4. #include <QSound>
    5. #include <QGraphicsWidget>
    6. #include "mainwindow.h"
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11.  
    12. MainWindow m;
    13. m.show();
    14.  
    15. QPixmap empty("images/emptyspace.png");
    16. QPixmap hit("images/hitspace.png");
    17. QPixmap miss("images/missspace.png");
    18. char data[10][10] = {
    19. {'X', ' ', 'X', 'X', 'S', 'X', 'X', 'X', 'X', 'X'},
    20. {'X', 'X', 'X', 'X', 'X', 'X', 'X', ' ', 'X', 'X'},
    21. {'X', 'X', ' ', ' ', 'X', 'X', 'S', 'X', 'X', 'X'},
    22. {'S', 'X', 'X', 'X', 'X', 'X', ' ', ' ', 'S', 'X'},
    23. {'S', 'X', 'X', 'X', 'S', 'X', ' ', 'X', 'S', 'X'},
    24. {'S', 'S', 'X', ' ', 'S', 'X', ' ', 'X', 'S', 'X'},
    25. {'S', 'X', 'X', 'X', 'S', 'X', ' ', 'X', ' ', 'X'},
    26. {'S', 'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X'},
    27. {'S', ' ', ' ', ' ', 'X', 'X', 'X', 'X', 'X', 'X'},
    28. {'X', 'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X'},
    29. };
    30. for (int row = 0; row < 10; ++row) {
    31. for (int col = 0; col < 10; ++col) {
    32. if (data[row][col] == ' '){
    33. m.setCellPixmap(row, col, empty);
    34. }
    35. else if (data[row][col]== 'X'){
    36. m.setCellPixmap(row, col, hit);
    37. }
    38. else m.setCellPixmap(row,col, miss);
    39. }
    40. }
    41.  
    42. return app.exec();
    43. }
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50. /*QApplication a(argc, argv);
    51.   MainWindow w;
    52.   w.show();
    53.  
    54.   QImage myImage;
    55.   myImage.load("images/hitspace.png");
    56.  
    57.   QLabel myLabel;
    58.   myLabel.setPixmap(QPixmap::fromImage(myImage));
    59.  
    60.   myLabel.show();*/
    To copy to clipboard, switch view to plain text mode 
    Last edited by wuts.the.martyr; 1st December 2011 at 10:27. Reason: tested code provided, giving feedback

  8. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,590
    Thanks
    13
    Thanked 1,586 Times in 1,515 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: update grid of pixmap labels based on 2d char arrray value

    The example is dependent on a few PNG files "blank.png" etc. and will fail silently if they are not present (it is an example not a finished product). My example is a single file for convenience. In a real application you would separate the class into a header and implementation.

    You should look at the "Promotion" feature in Designer: place a Qwidget in your main UI layout and promote it to a MyGameGrid class that you hand write along the lines of the example.
    You should look at QStackedWidget to hold the start widget and the game widget and allow switching from one to another.

  9. The following user says thank you to ChrisW67 for this useful post:

    wuts.the.martyr (6th December 2011)

  10. #7
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: update grid of pixmap labels based on 2d char arrray value

    okay, so i now have a qstackedwidget running with 3 pages, that are selectable on button/menu presses. I have created a function to automatically set the page 0 to start every time. a few qmessage/dialog boxes have been implemented as well. I have created some hardcoded arrays to test your script with and two buttons to swap them back and forth to make sure they update fine. I have tried to incorporate what you have shown, but failing horribly lol. All i get is a white screen, and if i change layers to another widget i crash. so my new question is, given my hierarchy of mainwindow.cpp, mainwindow.h, main.cpp, and mainwindow.ui... where should i be creating this loop to generate the array, i thought it should be on the main.cpp. Also what will i need to do to create these pixmap arrays on the correct page/with offset, so that when i switch views they dont still show up-anchoring maybe? they need to lie on my second pages widget called Game_Loop. Thanks again for all the help. below is what i have done to get me a white window of death.

    I placed the:
    Qt Code:
    1. void setCellPixmap(int row, int col, const QPixmap &pixmap) //whole thing is there i just trimmed for post
    2. QGridLayout *m_layout;
    3. const QPixmap m_emptyPixmap;
    4. const QPixmap m_hitPixmap;
    5. const QPixmap m_missPixmap;
    To copy to clipboard, switch view to plain text mode 
    inside my mainwindow.h

    then i placed:
    Qt Code:
    1. QWidget *central = new QWidget(this);
    2. m_layout = new QGridLayout(central);
    3. for(int r = 0; r < 10; ++r) {
    4. for(int c = 0; c < 10; ++c) {
    5. QLabel *label = new QLabel(central);
    6. label->setPixmap(m_emptyPixmap);
    7. m_layout->addWidget(label, r, c, Qt::AlignCenter);
    8. }
    9. }
    10. central->setLayout(m_layout);
    11. setCentralWidget(central);
    12. setCellPixmap(3, 3, m_hitPixmap);
    To copy to clipboard, switch view to plain text mode 
    inside the mainwindow.cpp with the proper urinary scope variables initialized

    then on the main.cpp i had this:
    Qt Code:
    1. ...
    2. QPixmap empty("images/emptyspace.png");
    3. QPixmap hit("images/hitspace.png");
    4. QPixmap miss("images/missspace.png");
    5. char data[10][10] = {
    6. {'X',' ',' ','X',' ',' ',' ','X',' ',' '},
    7. {' ','X',' ',' ','X',' ','X','S','X',' '},
    8. {' ','X','S','X',' ',' ','X',' ','X',' '},
    9. {'X','X','X',' ',' ',' ','X',' ',' ','X'},
    10. {' ',' ','X','X',' ',' ','X',' ',' ','X'},
    11. {' ',' ','X','S','X','X','X','X','X',' '},
    12. {' ','S','X','X','X','X','X','S',' ',' '},
    13. {' ','X','X',' ','X','X','X','X',' ','X'},
    14. {' ',' ','S',' ',' ',' ',' ','X',' ',' '},
    15. {' ',' ',' ',' ',' ',' ',' ','X',' ',' '}
    16. };
    17. for (int row = 0; row < 10; ++row) {
    18. for (int col = 0; col < 10; ++col) {
    19. if (data[row][col] == ' '){
    20. w.setCellPixmap(row, col, empty);
    21. }
    22. else if(data[row][col] == 'X'){
    23. w.setCellPixmap(row,col,hit);
    24. }
    25. else
    26. w.setCellPixmap(row, col, hit);
    27. }
    28. }
    29. w.setstackwidget();
    30. return a.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wuts.the.martyr; 5th December 2011 at 16:42. Reason: reformatted to look better

  11. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,590
    Thanks
    13
    Thanked 1,586 Times in 1,515 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: update grid of pixmap labels based on 2d char arrray value

    If all you get is a blank grid then you should check your pixmaps: QPixmap::isNull(). Then ask yourself, why would the constructor give me a null pixmap? Where is the program looking for the the image files and are they present?

  12. The following user says thank you to ChrisW67 for this useful post:

    wuts.the.martyr (6th December 2011)

  13. #9
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: update grid of pixmap labels based on 2d char arrray value

    QPixmap::QPixmap ( const QString & fileName, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )

    Constructs a pixmap from the file with the given fileName. If the file does not exist or is of an unknown format, the pixmap becomes a null pixmap.

    The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format.

    The file name can either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.

    If the image needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the flags to control the conversion.

    The fileName, format and flags parameters are passed on to load(). This means that the data in fileName is not compiled into the binary. If fileName contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
    I evidently didn't place it in the runtime folder? I was attempting to use the directory i use for my image resource file, is it possible to use that? or do I have to copy the folder over at runtime to test? Also is it possible to replace pixmaps placed using QtCreator by label name, such as my fire button switching from place during different modes of the game? could i use a boolean check slot /signal connection like this (after the boolean condition check) ui->F_43->setCellPixmap(4,3,fire);

  14. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,590
    Thanks
    13
    Thanked 1,586 Times in 1,515 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: update grid of pixmap labels based on 2d char arrray value

    You can use any method you like to find your external resources; having the files on the file system relative to the application executable is one method (see QCoreApplication::applicationDirPath()). For small icons and the like it is often desirable to build them into your program so they cannot get lost: Qt Resource System.

    Once your program is running you can change any UI widget property you like, you are not stuck with what you have set in the Qt Designer .ui file.

  15. The following user says thank you to ChrisW67 for this useful post:

    wuts.the.martyr (6th December 2011)

  16. #11
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: update grid of pixmap labels based on 2d char arrray value

    well i got that part working now by using:
    Qt Code:
    1. ui->label->setPixmap(QPixmap(":/images/missspace.png"));
    To copy to clipboard, switch view to plain text mode 


    Now I am trying to make a for loop situation where i make a QString equal the name of a label I would like to change, but am having a problem trying to typecaste it to be that label I want. Maybe you can figure out an alternative from this:

    Qt Code:
    1. xvalue = ui->X_coord->value();//value of x taken from a spin box
    2. yvalue = ui->Y_coord->value();//value of y taken from a spin box
    3. TR_position.clear();//initialize the QString TR_position
    4. TR_position = ("TR_");//place the first part of the label name on there
    5. TR_position.append(QString::number(xvalue));//append the x value to the end of the string
    6. TR_position.append(QString::number(yvalue));//append the y value to the end of the string
    7. ui->(QLabel)TR_position->setPixmap(QPixmap(":/images/missspace.png"));//fails, but trying to call the label that is represented by that string
    To copy to clipboard, switch view to plain text mode 

    was thinking that maybe I can just put the string characters themselves there, but i doubt that it would run.

    thank you for all your tips again

  17. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,590
    Thanks
    13
    Thanked 1,586 Times in 1,515 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: update grid of pixmap labels based on 2d char arrray value

    Quote Originally Posted by wuts.the.martyr View Post
    Now I am trying to make a for loop situation where i make a QString equal the name of a label I would like to change, but am having a problem trying to typecaste it to be that label I want.
    You cannot rewrite C++ code at run time, which seems to be what you are expecting to happen. You need to abandon this line of thinking. You don't have 100 labels with individual names any more and you already have a way to change the pixmap on the anonymous label at a given row and column in the layout (setCellPixmap() in my example). What is the new problem you are trying to solve?

  18. #13
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: update grid of pixmap labels based on 2d char arrray value

    so i can't use the contents of the QString to reference a QLabel by name? that's all I am trying to do. I understand you can't rewrite. I would just like to bend the rules a bit, and use the string that i conjure up each run through to grab the appropriate label and swap the image. I will attempt to go back to what you had, now that I know how to get the png files I require, if that's what it takes, I just wanted to do something simple albeit time consuming to code since I am running out of time. I am not trying to be hard headed here either, I just am having a hard time getting what I ultimately want, that I currently have minus the updating of game tiles. With what you have given me, I will need to figure out how to place it in my existing widget container. That is why I havent gone that route. I know you have already done so much, but i can only do fairly simple things using the Designer and slots/signals. Would you still be willing to at the very least help me get the game board we made placed inside my page widget. as it lies right now it breaks my game.
    Last edited by wuts.the.martyr; 6th December 2011 at 02:57.

  19. #14
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: update grid of pixmap labels based on 2d char arrray value

    Sorry again, its just hard to give up on something I spent a lot of time on, I've gotten to work finally even, but honestly its a terrible method and I have to write a bunch of code to differentiate between the QWidget class and QMainwindow back and forth since the mainwindow takes on the characteristics of the QWidget class. Whilst I have managed to get your code to resize a bit, I would still rather have it take on the properties of my current QGridLayout I created using Designer, or even just place the pixmaps in there, but i see the later being a problem possibly? any ideas?

  20. #15
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,590
    Thanks
    13
    Thanked 1,586 Times in 1,515 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: update grid of pixmap labels based on 2d char arrray value

    You can build the game panel with Designer and still use the general method for finding the label at [r,c] in a grid layout. See the attached complete Designer built example (3x3 but you get the idea). I have put a timed, random event in the main window so you can see stuff changing on the two game panels. It doesn't matter what you call the labels, and you don't have to try to find them by name.
    Attached Files Attached Files

Similar Threads

  1. Replies: 1
    Last Post: 19th April 2011, 12:17
  2. Replies: 0
    Last Post: 14th October 2010, 19:45
  3. Replies: 7
    Last Post: 4th September 2010, 06:10
  4. Creating a pixmap from unsigned char*
    By winder in forum Qt Programming
    Replies: 8
    Last Post: 27th July 2010, 03:54
  5. Rapid Update of Pixmap
    By dbrmik in forum Qt Programming
    Replies: 5
    Last Post: 23rd April 2009, 11:49

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.