Results 1 to 5 of 5

Thread: Changing QPushButton Text dynamically

  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 21:03.

  2. #2
    Join Date
    Nov 2011
    Location
    India
    Posts
    22
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Changing QPushButton Text dynamically

    try setText()
    Last edited by breakthecode; 18th March 2015 at 23:17.

  3. #3
    Join Date
    Mar 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Changing QPushButton Text dynamically

    Uhm, that's what I've been doing all along, have you seen my code?

    The problem is setText() doesn't override existing text.
    Look at this screenshot to what happens when DestoryWindow() is called (for the sake of
    demonstration, all buttons should be overridden with the same thing : "Test" ) ->

    Before:
    before.PNG

    After:

    after.PNG


    Added after 18 minutes:


    Ok Nvm, I think I found it after some extensive research.
    The button must be repainted after the update, which explains why the empty ones changed and the ones who had
    text didn't.
    Thanks anyways!
    Last edited by WanderingSoul; 18th March 2015 at 23:52.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Changing QPushButton Text dynamically

    setText() does override the previous text. If it doesn't work for you then you are either:
    a) calling the method on a different object to the one you think you are calling it on
    b) not calling the method at all (e.g. upon not entering a condition you think you are entering).

    Hard to say which is the case here. I would probably opt for the second one (you have a 'foreach' and a couple of 'ifs').

    As for repainting, setText() triggers an update() which in turn repaints the widget. So again calling setText() is enough. Then you just need to let Qt handle events like usual.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    WanderingSoul (19th March 2015)

  6. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Changing QPushButton Text dynamically

    Btw. you have a memory leak:
    Qt Code:
    1. board->~Board();
    To copy to clipboard, switch view to plain text mode 
    this does not release memory pointed to by "board" pointer, it only calls the destructor method.
    You need to use "operator delete"
    Qt Code:
    1. delete board;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 6th May 2010, 13:13
  2. Dynamically changing QFrame color
    By Doug Broadwell in forum Newbie
    Replies: 6
    Last Post: 16th October 2008, 09:22
  3. Changing QPushButton text colour with mouseMoveEvent
    By Misenko in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2008, 17:53
  4. Dynamically changing QLabel background colour
    By T4ng10r in forum Qt Programming
    Replies: 19
    Last Post: 19th April 2007, 13:47
  5. Dynamically changing QGroupBox size
    By T4ng10r in forum Qt Programming
    Replies: 5
    Last Post: 30th March 2007, 16: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.