Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is not that simple, however it is doable. The problem is that when you click on the button the press event is not propagated to the list widget. So you have to find a way to propagate the signal back to the list widget. </p> <p>When the button is pressed the <code>pressed</code> signal is emitted. When it is released the <code>released</code> signal is emitted. I will show you how to do it for one of them, you should do the same for the other.</p> <p>The most straight forward way to do what you want is to add a connection between the <code>pressed</code> signal of the button and the slot which will modify the background of your list. What you have is a <code>QListWidget</code> in which there are some <code>UserDetails</code> widgets each of which has a <code>QPushButton</code>. So we have to go all the way up from the <code>QPushButton</code> to the <code>QListWidget</code>. </p> <p>In your <code>UserDetails</code> class create a new signal, eg <code>buttonPressed()</code> and connect your button's <code>pressed()</code> signal with it. This way every time the button is clicked in your widget the widget itself will notify the world that its button has been clicked. </p> <pre><code>connect(ui-&gt;button, SIGNAL(pressed()), this, SIGNAL(buttonPressed()) </code></pre> <p>Now we want to notify the <code>QListWidget</code> that the button has been pressed. In order to achieve this we have to connect the <code>buttonPressed</code> signal from the <code>UserDetails</code> with a slot, let's call the slot <code>buttonPressedSlot()</code></p> <pre><code>connect(myItem, SIGNAL(pressed()), this, SLOT(buttonPressedSlot()) </code></pre> <p>Now the slot should detect which is the sender (since all <code>UserDetails</code> objects will get connected to the same slot, find the corresponding <code>QListWidgetItem</code> and call the slot that will update the background of this item.</p> <pre><code>void LiwstWidgetClick::buttonPressedSlot() { QObject* signalSender = QObject::sender(); UserDetails* p = qobject_cast&lt;UserDetails *&gt;(signalSender); if (p == NULL) return; // Get Position in list widget for (unsigned i=0; i&lt;ui-&gt;listWidget-&gt;count(); i++) { QListWidgetItem* item = ui-&gt;listWidget-&gt;item(i); if (ui-&gt;listWidget-&gt;itemWidget(item) == p) cellClicked(item); // THIS IS YOUR FUNCTION THAT CHANGES THE // BACKGROUND OF THE GIVEN ITEM } } </code></pre> <p>You should do the same for the <code>released()</code> signal. </p> <p><strong>EDIT</strong></p> <p>If the button was public you could avoid the extra signal, eg if you had a function (<code>getButton()</code>) in your <code>UserDetails</code> that returned the <code>ui-&gt;button</code> you could have just one connection</p> <pre><code>connect(myItem-&gt;button(), SIGNAL(pressed()), this, SLOT(buttonPressedSlot())); </code></pre> <p><strong>EDIT 2</strong></p> <p>If you just want to change the background of you button when it is pressed then you can achieve it using <a href="http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#qpushbutton-and-images" rel="nofollow">stylesheets</a>. You need to entries in your stylesheet one for the normal button state and one for the <code>pressed</code> state. For a list of available states have a look <a href="http://qt-project.org/doc/qt-4.8/stylesheet-reference.html#list-of-pseudo-states" rel="nofollow">here</a>. Sample code follows</p> <pre><code> QListView QPushButton { color: grey; border-image: url(/path/to/image1) 3 10 3 10; } QListView QPushButton:pressed { color: red; border-image: url(/path/to/image2) 3 10 3 10; } </code></pre> <p>Notice that I use the QListView <a href="http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html#selector-types" rel="nofollow">Descendant Selector</a> in order to get only these <code>QPushButtons</code> that are children of a <code>QListView</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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