2

I use validators to filter user input. Normally I have my validators working like this:

my_reg_ex = QRegExp("[1-9]\d{0,5}")
my_validator = QRegExpValidator(my_reg_ex, self.ui.lineEdit_test)
self.ui.lineEdit_test.setValidator(my_validator)

I wrote this after looking at some examples online. But I just noticed that if I remove the last part on the second line:

, self.ui.lineEdit_test

The code works exactly the same. I have a couple of these validators all around. I was wondering if it's okay to just use it without the part I mentioned. For example:

my_reg_ex = QRegExp("[1-9]\d{0,5}")
my_validator = QRegExpValidator(my_reg_ex)
self.ui.lineEdit_test.setValidator(my_validator)

Is there any difference between these? If there is please explain and tell me which one is the better way to go.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
shafuq
  • 465
  • 6
  • 20
  • both conceptually are the same. – eyllanesc Dec 30 '17 at 15:10
  • @eyllanesc The reason I asked was because most if not all the answers online show the usage as the first way I provided. Yet as I mentioned, if I take that part out the result doesn't seem to change. In that case isn't it just extra coding for no good reason? And btw I came upon this while I was trying to use the same validator for two different lineEdits. I had forgotten to write the my_validator = QRegExpValidator(my_reg_ex, self.ui.lineEdit_test) line for the second lineEdit. But had the self.ui.lineEdit_test.setValidator(my_validator) part coded. That made me see that it still works.. – shafuq Dec 30 '17 at 15:14
  • in the case of `my_validator = QRegExpValidator(my_reg_ex, self.ui.lineEdit_test)` you are just passing him a parent. – eyllanesc Dec 30 '17 at 15:16

1 Answers1

2

The QRegExpValidator class inherits QObject, so its constructor has an argument that takes a parent QObject. One general reason for setting a parent, is to ensure the object doesn't get garbage-collected when it goes out of scope. This can easily happen if you don't keep any other reference to the object, and is a very common cause of many of the problems seen in newbie questions on SO.

However, this is actually not a problem in your specific example. This is because the line-edit takes ownership of the validator when you pass it to setValidator(), so you don't need to explicitly keep a reference to it yourself. As far as the code in your question is concerned, it really makes no difference whether you set a parent or not.

Having said that, there is at least one scenario where not setting a parent may be advantageous. This could arise if you needed to regularly reset the validator at runtime. If you gave each new validator a parent, the old one would not be deleted when setValidator() is called (because the parent would still hold a reference to it). So for the purposes of simplifying object-cleanup, not setting a parent in this situation might be preferrable.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thank you very much for the deep explanation. Since I want to use the same validator for more than one lineedit and since (as you greatly explained) it doesn't change anything (for this case), I'll go ahead and not set its parent. less lines of code for the same price :) @ekhumoro Thanks again! – shafuq Dec 30 '17 at 21:37