views:

35

answers:

2

QLineEdit has a textEdited signal which is emitted whenever the text is changed by user interaction, but not when the text is changed programatically. However, QDateTimeEdit has only a general dateTimeChanged signal that does not distinguish between these two types of changes. Since my app depends on knowing if the field was edited by the user or not, I'm looking for ways to implement it.

My (currently working) strategy was to create an eventFilter to the edit field, intercept key press and mouse wheel events and use them to determine if the field was modified by the user (storing this info in an object), and finally connecting the dateTimeChanged signal to a function that decides if the change was by the user or done programatically. Here are the relevant parts of the code (python):

class UserFilter(QObject):
    def __init__(self, parent):
        QObject.__init__(self, parent)
        self.parent = parent

    def eventFilter(self, object, event):
        if event.type() == QEvent.KeyPress or event.type() == QEvent.Wheel:
            self.parent.edited = True
        else:
            pass
        return False


class DockThumb(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self, parent)
        self.parent = parent

        self.edited = False

        self.dateedit = QDateTimeEdit(self)

        self.userfilter = UserFilter(self)
        self.dateedit.installEventFilter(self.userfilter)
        ...

    self.connect(self.dateedit,
            SIGNAL('dateTimeChanged(QDateTime)'),
            self.edited_or_not)

    def edited_or_not(self):
        if self.edited:
            # User interacted! Go for it.
            self.parent.runtimer()
            # self.edited returns to False once data is saved.
        else:
            # User did not edited. Wait.
            pass

Is there a more objective way of doing it? I tried subclasssing QDateTimeEdit, but failed to deal with events... Expected user interactions are direct typing, up/down arrow keys to spin through dates and copy/pasting the whole string.

A: 

The idiomatic Qt way of achieving this is indeed subclassing QDateTimeEdit and adding the functionality you require. I understand you tried it and "failed to deal with events", but that's a separate issue, and perhaps you should describe those problems - since they should be solvable.

Eli Bendersky
A: 

Since I'm not entirely sure about what you are trying to do, I would agree with Eli Bendersky. Short of that, if you know when you will be programatically changing the QDateTimeEdit, set some flag that you can check in the slot handler that will indicate a programatic change is occurring and clear it when you are done.

Arnold Spence