Results 1 to 5 of 5

Thread: Changing QPushButton Text dynamically

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Changing QPushButton Text dynamically

    Hello there,
    I've been working on a GUI for a simple puzzle game where the program generates
    numbers for a 9x9 grid. I've represented that grid as 81 different, stylized QPushButtons
    and everything works so far. When the numbers are generated, each appear in its
    allocated slot. The problem occurs when the user clicks the "New Game" Menu action
    button (which is supposed to "reset" the grid and generate new random numbers); the
    QPushButtons retain their text and do not "reset".

    Here's the code I am using to populate the GUI portion of the code:
    I extract all the QPushButtons into a QList and iterate to access each one of
    them (and that works fine the first time), but not when DestroyWindow()
    within NewGame() is invoked....

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));
    7. connect(ui->actionNew_Game, SIGNAL(triggered()), this, SLOT(NewGame()));
    8. connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(About()));
    9.  
    10. InitiateWindow();
    11. }
    12.  
    13. // actionNew_Game Signal handler
    14. // Attempts to destroy the current puzzle and generate a new one
    15. // NOTE: TO BE DEBUGGED, NOT FULLY FUNCTIONAL AS OF YET
    16. void MainWindow::NewGame()
    17. {
    18. DestroyWindow();
    19. InitiateWindow();
    20. }
    21.  
    22.  
    23. // Initializes the puzzle by calling an instance of Board and its puzzle
    24. // generating functions. A list of all available QPushButtons is then
    25. // iterated though, where every single button gets a number from the
    26. // generated puzzle and background colour (either teal or white) depending
    27. // whether its location is odd or even.
    28. void MainWindow::InitiateWindow()
    29. {
    30. board = new Board();
    31. board->Initialize();
    32. board->GeneratePuzzle();
    33. // board->DisplayInConsole();
    34. std::vector<std::vector<int> > puzzle = board->getBoard();
    35. QList<QPushButton*> allbuttons = this->findChildren<QPushButton*>();
    36.  
    37. int counter = 0;
    38. int x =0; int y = 0;
    39. int colorcount = 0;
    40. foreach (QPushButton* button, allbuttons)
    41. {
    42. QString buttonname = "pushButton_" + QString::number(counter);
    43. button->setObjectName(buttonname);
    44. if (colorcount%2 == 0)
    45. button->setStyleSheet("background-color: teal");
    46. else
    47. button->setStyleSheet("background-color: white");
    48. if (puzzle[x][y] != -1)
    49. button->setText(QString::number(puzzle[x][y]));
    50. counter++,y++;colorcount++;
    51. if (counter == board->getBoardlength())
    52. {
    53. x++;
    54. y = 0;
    55. counter = 0;
    56. }
    57. }
    58. }
    59.  
    60. // Attempts to destory the current puzzle instance
    61. // NOTE: TO BE DEBUGGED, NOT FULLY FUNCTIONAL AS OF YET
    62. void MainWindow::DestroyWindow()
    63. {
    64. QString emptycell = " ";
    65. board->~Board();
    66. QList<QPushButton*> allbuttons = this->findChildren<QPushButton*>();
    67. foreach (QPushButton* button, allbuttons)
    68. button->setText(emptycell);
    69. }
    70.  
    71. // default destructor
    72. MainWindow::~MainWindow()
    73. {
    74. delete ui;
    75. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas? Any help would be appreciated!
    Thanks!
    Last edited by WanderingSoul; 18th March 2015 at 20:03.

Similar Threads

  1. Replies: 0
    Last Post: 6th May 2010, 12:13
  2. Dynamically changing QFrame color
    By Doug Broadwell in forum Newbie
    Replies: 6
    Last Post: 16th October 2008, 08:22
  3. Changing QPushButton text colour with mouseMoveEvent
    By Misenko in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2008, 16:53
  4. Dynamically changing QLabel background colour
    By T4ng10r in forum Qt Programming
    Replies: 19
    Last Post: 19th April 2007, 12:47
  5. Dynamically changing QGroupBox size
    By T4ng10r in forum Qt Programming
    Replies: 5
    Last Post: 30th March 2007, 15:02

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
  •  
Qt is a trademark of The Qt Company.