views:

358

answers:

3

OK, this is driving me nuts. I'm sure it is trivial, but I've been looking for the answer for a while and don't see it. I'm sure it will be a flat forehead.

I'm designing a Qt4 dialog in Python. I generated the code via QDesigner, and have 4 inputs on the system:

  • QLineEdit (can't be blank)
  • QPlainTextEdit
  • QLineEdit (can't be blank)
  • QComboBox (need to select one of the options)

Question: Is there a flag which makes a field "required"? Forces it to be non-blank?

I was trying to use a QRegExpValidator, but not sure this is right:

    regex = QRegExp(r"\\S+")        
    self.optionName.setValidator(QRegExpValidator(regex,self))

I know I'm missing something obvious (please, don't let it be a self.optionName.setRequired() function).

Update

I've now added this class:

from PyQt4 import QtGui

class ValidStringLength(QtGui.QValidator):
    def __init__(self, min, max, parent):
        QtGui.QValidator.__init__(self, parent)

        self.min = min
        self.max = max

    def validate(self, s, pos):
        if self.max > -1 and len(s) > self.max:
            return (QValidator.Invalid, pos)

        if self.min > -1 and len(s) < self.min:
            return (QValidator.Intermediate, pos)

        return (QValidator.Acceptable, pos)

    def fixup(self, s):
        pass

Call it like this:

    self.optionName.setValidator(ValidStringLength(2, 8, self.optionName))
    self.criteriaName.setValidator(ValidStringLength(2, 8, self.criteriaName))

and have a breakpoint set at the validate() function of the class, but that is never called.

Am I missing something basic?

TIA

Mike

A: 

I don't know much about validators, or why they may not be triggered. However, if i were trying to do something like this, I would make a validate slot in my dialog that gets called when any of the required items change. In that slot, I would check the state of the items, and enable/disable the accept button based on the validity of every item. I would also try to do something to indicate invalid items, such as outlining them in red.

An alternate way to do so would be to override the accept function and do your checking there, and only call the parent's accept function if everything is valid.

Caleb Huitt - cjhuitt
A: 

Perhaps set an input mask?

http://doc.trolltech.com/4.4/qlineedit.html#inputMask-prop

If the input in the line edit does not follow the input mask "hasAcceptableInput()" will return false.

http://doc.trolltech.com/4.4/qlineedit.html#acceptableInput-prop

You can use them in conjunction with a validator as well.

Chris Cameron
I tried "Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", but it makes the input work different (block cursor, seems like inserting didn't work)
Mike Crowe
Hmm, I was hoping that bizarre behaviour was a mistake on my part in my testing, but if it's the norm then I don't know why anyone would use input masks haha!
Chris Cameron
A: 

Not sure if you can do that, since the default value is blank. I.e. the lineedit would be invalid from the get-go, which it's never supposed to be. Just a hunch.

Anyway, since you want to coordinate between your buttons, isn't it easier to write something that checks global state when each data changes and enables/disables the ok button until the global state is acceptable?

Marcus Lindblom