[PyQt] PyQt4 : connecting QTimeEdit to QScrollBar ?

Cruella Deville cgi3d at yahoo.com
Sat Nov 17 23:27:52 GMT 2012


Hi can somebody please help me?
I am trying to control the hours, minutes and seconds of a QTimeEdit independently by using 3 QScrollBars.
Here's what I've got so far. It works but the setTime command always resets the elements that I don't want to affect. Is there something like setMinutes, setHours or setSeconds ?
I've also thought about using a QLCDNumber widget instead of the QTimeEdit but I'm not sure if that would work.

Thanks for any help.

from PyQt4 import QtCore, QtGui

class Help(QtGui.QMainWindow):
    def __init__(self):
        super(Help, self).__init__()

        #QTimeEdit
        self.TimeEdit = QtGui.QTimeEdit()

        #Hours ScrollBar
        self.HourScrollBar = QtGui.QScrollBar(QtCore.Qt.Horizontal)
        self.HourScrollBar.setMinimum(0)
        self.HourScrollBar.setMaximum(23)
        self.HourScrollBar.setFocusPolicy(QtCore.Qt.StrongFocus)

        #Minutes ScrollBar
        self.MinScrollBar = QtGui.QScrollBar(QtCore.Qt.Horizontal)
        self.MinScrollBar.setMinimum(0)
        self.MinScrollBar.setMaximum(59)
        self.MinScrollBar.setFocusPolicy(QtCore.Qt.StrongFocus)

        #Seconds ScrollBar
        self.SecScrollBar = QtGui.QScrollBar(QtCore.Qt.Horizontal)
        self.SecScrollBar.setMinimum(0)
        self.SecScrollBar.setMaximum(59)
        self.SecScrollBar.setFocusPolicy(QtCore.Qt.StrongFocus)

        #ScrollBar connections
        self.HourScrollBar.valueChanged.connect(self.HourChanged)
        self.MinScrollBar.valueChanged.connect(self.MinChanged)
        self.SecScrollBar.valueChanged.connect(self.SecChanged)

        #Main layout and widget
        MainWidget = QtGui.QWidget()
        MainLayout = QtGui.QVBoxLayout()
        MainLayout.addWidget(self.TimeEdit)
        MainLayout.addWidget(self.HourScrollBar)
        MainLayout.addWidget(self.MinScrollBar)
        MainLayout.addWidget(self.SecScrollBar)
        MainWidget.setLayout(MainLayout)
        self.setCentralWidget(MainWidget)

    def HourChanged(self):
        self.TimeEdit.setTime(QtCore.QTime.fromString(str(self.HourScrollBar.value()), "h"))

    def MinChanged(self):
        self.TimeEdit.setTime(QtCore.QTime.fromString(str(self.MinScrollBar.value()), "m"))

    def SecChanged(self):
        self.TimeEdit.setTime(QtCore.QTime.fromString(str(self.SecScrollBar.value()), "s"))

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Help()
    w.show()
    sys.exit(app.exec_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20121117/62d067f1/attachment.html>


More information about the PyQt mailing list