Qt World Summit: Submit your Presentation

[SOLVED}Making noises when buttons get pushed.

  • I want to get a button to make a noise when it gets pushed so I know it's sent an email, someone previously told me I need Phonon, but I can't find it anywhere, is there any other way of doing it that doesn't require additional modules?
    This is the script I've got so far

    from PyQt4 import QtGui, QtCore

    import smtplib
    import wave

    from email.mime.text import MIMEText

    class Window(QtGui.QWidget):
    def init(self):
    self.button = QtGui.QPushButton('', self)
    layout = QtGui.QVBoxLayout(self)

    def handleButton(self):
        fp = open('Firetruck.txt', 'r')
        msg = MIMEText(fp.read())
        msg['Subject'] = 'Subject'
        msg['From'] = 'ajames@brecon-hs.powys.sch.uk'
        msg['To'] = 'swilliams@brecon-hs.powys.sch.uk'
        s = smtplib.SMTP('BHS-MAIL')

    if name == 'main':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()


    I'm assuming the code for the sound goes under
    [code] def handleButton(self): [/code]

  • QApplication::beep()
    That's in basic sounds the PC buzzer or other system used device. If you want specific sounds to be played a bit more work is needed.

  • Yeah that'd be fine :D
    What do I need to import to get that to work?

  • Because I'm just getting invalid syntax and it's pointing to the first ":"

  • hmm,
    (not tested! But something like this)
    // source file:
    @#include <QApplication>

    void YourObject::beeping(void)

  • Because it's a static function of QApplication you don't need an object to be created or to use the qApp pointer. Just don't forget to include the QApplication module.
    Think that this should work.

  • Ah I just found this - http://docs.python.org/2/library/winsound.html

    import winsound

    winsound.Beep(3500, 1000)
    Seems to work c:

  • Hmm, tried the beep() function here, but no sound on my laptop, so there might be an issue with it. (using Qt5.0.1 btw). It is only stated that it's not available on Embedded Linux.

  • If fall you want is a beep then just use:


    [quote author="swilliams2" date="1369304282"]Because I'm just getting invalid syntax and it's pointing to the first ":"[/quote]

    This is because the example (QApplication::beep()) is for c++ and you're using Python.

Log in to reply