Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt - QDialogButtonBox signals and tool tip

Tags:

python

pyqt

I got a couple of questions regarding qDialogButtonBox. While my code still works, I believed that there are a few parts that can be better refined/ I am not finding much info online

class testDialog(QtGui.QDialog):
    def __init_(self, parent=None):
        ...
        self.init_ui()
        self.signals_connection()

    def init_ui(self):
        ...
        self.buttonBox = QtGui.QDialogButtonBox()
        self.buttonBox.addButton("Help", QtGui.QDialogButtonBox.HelpRole)
        self.buttonBox.addButton("Apply", QtGui.QDialogButtonBox.AcceptRole)
        self.buttonBox.addButton("Cancel", QtGui.QDialogButtonBox.RejectRole)
        #

    def signals_connection(self):
        self.test_random.clicked.connect(self.test_rand)

        # Is this the latest/correct way to write it?
        self.buttonBox.accepted.connect(self.test_apply)
        self.buttonBox.rejected.connect(self.test_cancel)
        self.buttonBox.helpRequested.connect(self.test_help)

    def test_apply(self):
        print "I am clicking on Apply"

    def test_cancel(self):
        print "I am clicking on Cancel"
        self.close()

    def test_help(self):
        print "I am clicking for Help!"

My questions are as follows:

  1. Under my function - signals_connection(), the lines that I wrote for the buttonBox (though the code still works) are quite different for the signal I have wrote for the self.test_random and I am unable to find any similar online for the qdialogbuttonbox.. There is another style that I have found - self.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), self, QtCore.SLOT("accept()")) but I think that is the old style?? Otherwise what should be the right way to write it?
  2. In my test_cancel() function, is writing self.close() the best way to close the application? The way that I run my program is as follows:

    dialog = testDialog();dialog.show()

  3. Lastly, is it possible to add 3 different tool tips to the 3 buttons I have created? I saw that there is a command for it - self.buttonBox.setToolTip("Buttons for life!"), but this will results in all 3 buttons to have the same tool tip. Can I make it as individual?

like image 280
dissidia Avatar asked Sep 29 '16 00:09

dissidia


1 Answers

  1. Yes, that is the correct way to write signal connections (the other syntax you found is indeed the old way of doing it). You can find all the signals in the pyqt documentation for QDialogButtonBox. Different widgets and objects have different signals. QPushButton's and QDialogButtonBox's have different signals.

  2. Yes, close() will close the dialog. The QApplication will exit by default if there are no other windows open. However, if this is a modal dialog, you typically want to close a dialog with either the accept or reject command. This will alert the calling function as to whether the dialog was closed with the Ok/Yes/Apply button or closed with the No/Cancel button.

  3. You can set different tooltips for different buttons in the QDialogButtonBox. You just need to get a reference to the specific button you want to set the tooltip for.

For example

self.buttonBox.button(QDialogButtonBox.Help).setToolTip('Help Tooltip')
self.buttonBox.button(QDialogButtonBox.Ok).setToolTip('Apply Tooltip')

Or you could loop through all the buttons

for button in self.buttonBox.buttons():
    if button.text() == 'Help':
        button.setToolTip('Help Tooltip')
    elif button.text() == 'Apply':
        button.setToolTip('Apply Tooltip')

Also, you could connect the accepted and rejected signals from the QDialogButtonBox to the accept and reject slots on the QDialog

self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)

That way, you won't have to manually connect the Ok and Cancel buttons to your callbacks for closing the dialog.

like image 136
Brendan Abel Avatar answered Nov 19 '22 18:11

Brendan Abel