PyQt - QDialogButtonBox signals and tool tip
<p>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</p>

<pre><code>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!"
</code></pre>

<p>My questions are as follows:</p>

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

<p><code>dialog = testDialog();dialog.show()</code></p></li>
<li><p>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  - <code>self.buttonBox.setToolTip("Buttons for life!")</code>, but this will results in all 3 buttons to have the same tool tip. Can I make it as individual?</p></li>
</ol>

<ol>
<li><p>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 <a href="http://pyqt.sourceforge.net/Docs/PyQt4/qdialogbuttonbox.html" rel="nofollow"><code>QDialogButtonBox</code></a>.  Different widgets and objects have different signals.  <code>QPushButton</code>'s and <code>QDialogButtonBox</code>'s have different signals.</p></li>
<li><p>Yes, <code>close()</code> will close the dialog.  The <code>QApplication</code> 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 <code>accept</code> or <code>reject</code> command.  This will alert the calling function as to whether the dialog was closed with the <code>Ok/Yes/Apply</code> button or closed with the `No/Cancel' button.</p></li>
<li><p>You can set different tooltips for different buttons in the <code>QDialogButtonBox</code>.  You just need to get a reference to the specific button you want to set the tooltip for.</p></li>
</ol>

<p>For example</p>

<pre><code>self.buttonBox.button(QDialogButtonBox.Help).setToolTip('Help Tooltip')
self.buttonBox.button(QDialogButtonBox.Ok).setToolTip('Apply Tooltip')
</code></pre>

<p>Or you could loop through all the buttons</p>

<pre><code>for button in self.buttonBox.buttons():
    if button.text() == 'Help':
        button.setToolTip('Help Tooltip')
    elif button.text() == 'Apply':
        button.setToolTip('Apply Tooltip')
</code></pre>

<p>Also, you could connect the <code>accepted</code> and <code>rejected</code> signals from the <code>QDialogButtonBox</code> to the <code>accept</code> and <code>reject</code> slots on the <code>QDialog</code></p>

<pre><code>self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
</code></pre>

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

python pyqt