Need advice to control button color on checked state transitions



  • My goal is to graphically render the state of a peg solitaire game board. With very limited Qt experience, my first approach has been to dynamically create checkable pushbuttons within the mainwindow ctor, and add each of them to a specific position of a grid layout. I would like to control the color of each grid button such that there is a clear visible representation of the presence/absence of a game piece when it is clicked, as it transitions between the checked/unchecked state, respectively.

    The default behavior on my platform (Mac OS X Yosemite, with Qt 5.5) is that the buttons in my grid toggle between a darkish gray and a light gray.

    I would like to assign those two colors myself, but haven't been able to discover the most direct way to do that. Is it possible to set the properties of the buttons when they are first created, such that the checked/unchecked colors are of my own design, and everything will simply work? Or must I subclass the pushbutton, override the paintEvent method, and query the checked state, explicitly setting the color during each event?

    Also, if anyone might suggest a method better than my QGridLayout/QPushbutton approach, I would be grateful for such advice. I certainly don't need the mechanism to be a button - it could simply be a rectangle or any other construct, so long as it can be clicked, and I can cause it to change color on each click event; it just seemed that since the pushbutton already has that behavior, it was the most obvious approach. I would actually prefer the game pieces to be circles, and not knowing how to achieve that with a pushbutton, I have settled for a square shape instead.

    My thanks in advance for any advice on these topics!


  • Lifetime Qt Champion

    Hi,

    Depending on how your button should look like, Qt's stylesheet system might be the most simple solution

    Hope it helps


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.