Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A way that I have done such a thing in the past, is to make a signal in the button class that signals all the other buttons to go unselected.</p> <p>So the widget that has a pointer to all the objects makes the connection between the signal of one to one of its signals, and its signal to deselect the object.</p> <p>Here are some skeleton classes of what you would need.</p> <pre><code>class Button { signals: void selected(Button *); slot: void deselect(Button *); } class ButtonContainer { public: void addButton(Button *) signals: void buttonSelected(Button *); } </code></pre> <p>When adding a button connect it to the container this way:</p> <pre><code>QObject::connect(button, SIGNAL(selected(Button *)), buttonContainer, SIGNAL(buttonSelected(Button *))); QObject::connect(buttonContainer, SIGNAL(buttonSelected(Button *)), button, SLOT(deselect(Button *))); </code></pre> <p>When the mouse press event happens, emit this signal after applying your "selected" stylesheet:</p> <pre><code>emit selected(this); </code></pre> <p>In the deselect slot put this:</p> <pre><code>void Button::deselect(Button * selectedButton) { if(this != selectedButton) { // apply stylesheet for deselecting if it was previously selected } } </code></pre> <p>Hope that helps.</p> <p>By the way, be sure to make sure that you aren't setting the stylesheet too often. I haven't done extensive testing, but in at least some versions of Qt, it could go past the limit of QList. They have dynamic properties available to do frequent style changes:</p> <p><a href="https://stackoverflow.com/questions/13564823/qlist-out-of-memory/13568733#13568733">QList: Out of memory</a></p>
 

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