Why doesn't QInputDialog::getText() allow passing a custom QValidator?
-
wrote 14 days ago last edited by
I'm working with validators in Qt at the moment. I wanted to give my own custom validator to
QInputDialog
. However, it seems like that is not possible (except via some cursed runtime reflection tricks to access a private member): https://forum.qt.io/post/37206.I'm wondering if it would be possible to expose the validator of the input in the
QInputDialog
via a pair of accessor methods:void QInputDialog::setValidator(QValidator *validator); QValidator *QInputDialog::validator() const;
The
setValidator()
call can be a no-op in the case where theQInputDialog
is not inputting text and thevalidator()
call can returnnullptr
if the validator is not set or if the dialog is not a text input dialog.It will also be useful to change the static
QInputDialog::getText()
convenience method to include aQValidator *
parameter. Although it's not as much of an issue for me. I can understand that the convenience method doesn't expose every knob to tune the behaviour in favour of being simpler in the usual case. If it is to be supported, I'd suggest the following method signature:static QString QInputValidator::getText(QObject *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode = QLineEdit::Normal, const QString &text = QString(), bool *ok = nullptr, Qt::WindowFlags = Qt::WindowFlags, Qt::InputMethodHints inputMethodHints = Qt::ImhNone, QValidator *validator = nullptr);
The only thing I am not feeling quite sure about is what will happen if the validator is already the child of another QObject. I can't find anything in the docs regarding the memory safety of calling
setParent()
on an object which already has a parent. Will the old parent first be updated to remove the child from its children list? If not, this will cause a double free.Anyway, would like to hear your thoughts on this. I think it will be quite useful to have the option to set a custom validator on the
QInputDialog
'sQLineEdit
widget. Let me know if you agree or if there is a good reason I'm missing why this can not be done. -
wrote 14 days ago last edited by
Hi
for this you should pass your validator as argument to the internal QLineEdit, not to the QInputDialog itself (unless the containing class has a dedicated accessor to transmit the dependancy to it's component, but here it's not the case).
However, there is no accessor to that QLineEdit (just a few accessors to some of it's properties). So it's not possible.You probably should create your own equivalent widget. In a general matter, you shouldn't expect too much from those so called "convenience classes", they usually don't allow for much else than very basic customization possibilities.
-
Hi
for this you should pass your validator as argument to the internal QLineEdit, not to the QInputDialog itself (unless the containing class has a dedicated accessor to transmit the dependancy to it's component, but here it's not the case).
However, there is no accessor to that QLineEdit (just a few accessors to some of it's properties). So it's not possible.You probably should create your own equivalent widget. In a general matter, you shouldn't expect too much from those so called "convenience classes", they usually don't allow for much else than very basic customization possibilities.
wrote 14 days ago last edited by@CassD said in Why doesn't QInputDialog::getText() allow passing a custom QValidator?:
However, there is no accessor to that QLineEdit (just a few accessors to some of it's properties). So it's not possible.
@L0uisc
If you want to get theQLineEdit
to place your validator. Although it does not look like you are intended to do this, try creating aQInputDialog
instance (not one the staticget...()
methods), set itsinputMode
and useQLineEdit *lineEdit = dialog->findChild<QLineEdit *>()
. That may give you the line edit, unless they create it dynamically, I don't know. -
Hi
for this you should pass your validator as argument to the internal QLineEdit, not to the QInputDialog itself (unless the containing class has a dedicated accessor to transmit the dependancy to it's component, but here it's not the case).
However, there is no accessor to that QLineEdit (just a few accessors to some of it's properties). So it's not possible.You probably should create your own equivalent widget. In a general matter, you shouldn't expect too much from those so called "convenience classes", they usually don't allow for much else than very basic customization possibilities.
wrote 14 days ago last edited by@CassD I understand that the convenience widgets aren't as flexible as writing your own widget. I just thought adding a validator is simple enough and can be quite useful. I was pondering the possibility and wondered if there is a specific reason why this was not done.
If the Qt company doesn't ever implement it, I will not mind. If they do, it's nice, but it's in no way a dealbreaker or requirement.
-
wrote 14 days ago last edited by
adding a validator to the appropriate widget is simple an easy, the problem here is that you are trying to pass it to a wrong widget (but that actually contains the appropriate widget on which you would like to add it but doesn't give you a way to access it).
JonB's workaround might worth a try. I had completely forgotten that possibility.
-
adding a validator to the appropriate widget is simple an easy, the problem here is that you are trying to pass it to a wrong widget (but that actually contains the appropriate widget on which you would like to add it but doesn't give you a way to access it).
JonB's workaround might worth a try. I had completely forgotten that possibility.
wrote 14 days ago last edited by@CassD I know there are no
validator()
andsetValidator()
methods onQInputDialog
. I'm wondering why and if it might be a useful addition to the API. I'm currently implementing my own custom dialog.I'm writing Qt as part of a CS course at university, so I don't even have a real issue with it taking longer to write. I'm just asking the question a) if it is something that more people will want and b) why isn't it there currently.
a) seems to be most people will rather write a custom widget since the
QInputDialog
is just a convenience class which hides too much of the features people need.
b) seems to be that it's not worth the extra complexity for just a convenience class when most people will write a custom dialog anyway. -
@CassD I know there are no
validator()
andsetValidator()
methods onQInputDialog
. I'm wondering why and if it might be a useful addition to the API. I'm currently implementing my own custom dialog.I'm writing Qt as part of a CS course at university, so I don't even have a real issue with it taking longer to write. I'm just asking the question a) if it is something that more people will want and b) why isn't it there currently.
a) seems to be most people will rather write a custom widget since the
QInputDialog
is just a convenience class which hides too much of the features people need.
b) seems to be that it's not worth the extra complexity for just a convenience class when most people will write a custom dialog anyway.wrote 14 days ago last edited by JonB 5 Jan 2025, 18:10@L0uisc
The simple answer is that Qt must have have hundreds of classes and thousands of methods. You could add useful/utility features forever. There is no deep reason why one is offered but not another, other than popular usage.QInputDialog
--- which by the way is not used that often --- is only really a convenience class on top ofQDialog
. Nothing wrong with your suggestion --- and you can always make those at https://bugreports.qt.io/, or offer to implement it yourself --- just TQtC probably has a million other things to do :)
1/7