PDA

View Full Version : Does QToolButton have a RELEASED Signal ?



Nfrancisj
27th June 2019, 06:37
Does QToolButton have a 'RELEASED' signal?

I see these two, but no release.

self.snapshotBtn.pressed.connect(self.printMe)
self.snapshotBtn.triggered.connect(self.printMe)

What I am trying to do is fire a signal when the user releases the QToolButton.

Ideas?

Please and Thanks,

Nick

My Test Code

import sys
try:
from PySide import QtCore, QtGui, QtGui as QtWidgets
except:
from PySide2 import QtCore, QtGui, QtWidgets


class Example(QtWidgets.QWidget):
def __init__(self):
super(Example, self).__init__()

self.initUI()

def printMe(self):
print("thisWorks")

def initUI(self):

self.snapshotBtn = QtWidgets.QToolButton(self)
self.snapshotMenu = QtWidgets.QMenu()
self.captureAction = QtWidgets.QAction("")
#self.viewAction = QtWidgets.QAction("")

self.snapshotBtn.setObjectName("SnapShotButton")
#self.snapshotBtn.setIcon(QtGui.QIcon("C:/Users/nfran/.nuke/icons/cameraIcon.png"))
#self.viewAction.setIcon(QtGui.QIcon("C:/Users/nfran/.nuke/icons/cameraIcon.png"))
self.captureAction.setIcon(QtGui.QIcon("C:/Users/nfran/.nuke/icons/cameraIcon.png"))


self.snapshotMenu.addAction(self.captureAction)
#self.snapshotMenu.addAction(self.viewAction)

self.snapshotBtn.setMenu(self.snapshotMenu)
self.snapshotBtn.move(50, 50)
self.snapshotMenu.setStyleSheet("""QMenu{ width: 30px; }""")
self.snapshotBtn.setStyleSheet("""#SnapShotButton {image: url(C:/Users/nfran/.nuke/icons/cameraIcon.png);} #SnapShotButton:hover {image: url(C:/Users/nfran/.nuke/icons/cameraIcon_Hover.png);}""")

self.snapshotBtn.pressed.connect(self.printMe)
self.snapshotBtn.triggered.connect(self.printMe)

self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Tooltips')
self.show()



def main():
app = QtWidgets.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())


if __name__ == '__main__':
main()

ChristianEhrlicher
27th June 2019, 19:57
See the documentation: https://doc.qt.io/qt-5/qabstractbutton.html#released

Nfrancisj
27th June 2019, 22:12
hmm... I did see the docs.
So, I guess I'm using the wrong Signal. The behavior I'm trying to get is more of a pressed and hold down kinda thing. I'm making a before and after dialog window. Where I have 2 images, and when I push the button and hold it down, it shows the image before it was color-corrected, and when I let the button go, it returns to the color-corrected image. Pressed does switch to the unmodified image, but the Release doesn't switch back. Though, when I quickly click and release, it fires both.

How would I go about implementing a Press and Hold, and Release? Is the QToolButton the right widget for this?

Thanks,

Nick

d_stranz
28th June 2019, 18:56
Pressed does switch to the unmodified image, but the Release doesn't switch back.

That's two unrelated things. If your slots for handling pressed() and released() are being called at the right times, then the problem is in your code for restoring the image in the slot that handles released(). If you are connecting both signals to the same slot, then you probably have an error in your logic for keeping track of which image should be displayed.

Note that if you move the cursor off the tool button before you release the mouse, you will never get a released() signal for that button. So it would then be possible for your image to get stuck in the unmodified state. This is why the clicked() signal works as it does - you will get a clicked() signal only if the mouse is pressed and then released while on the button. You won't get a clicked() if either the press or release occur when the cursor is not on the button. It's this behavior that allows a user to "cancel" a click by simply moving off the button before releasing.

You might get more predictable behavior if you use a checkable button. (QAbstractButton::setCheckable()) This would give you a two state button - click once and it goes to the checked state, click again and it goes back to unchecked. You would connect to the QAbstractButton::toggled() signal and examine the state of the "checked" parameter of the signal to determine which image to display.

Using a checkable button would also give you the flexibility to implement other features - like being able to zoom in on the image or pan it around. You can't do that it you are requiring the user to keep the mouse pressed and on the button all the time.

Nfrancisj
29th June 2019, 21:07
I was able to get the behavior I was looking for by using the QPushButton instead of the QToolButton.
Thank you very much for all the information. :)

d_stranz
30th June 2019, 17:07
I was able to get the behavior I was looking for by using the QPushButton instead of the QToolButton.

That's very hard to believe. The clicked(), released(), pressed(), and toggled() signals for QPushButton and QToolButton are all implemented by the base class QAbstractButton, so there is no difference in behavior between the two derived classes when it comes to those signals. QToolButton adds one more signal, triggered(), which is emitted when the QAction associated with the button is executed (i.e. the tool button is clicked), but you aren't using that feature in your code.