Using QInputDialog with custom QValidator
-
Hi,
I'm trying to use QInputDialog with Validator. The custom Validator I'm using is from "here":http://www.qtforum.org/article/17075/qlineedit-enable-only-number.html?s=2fa8a4c736074599317c1a85d8971710c4a1c479#post67682 . In the docs I saw that one of the arguments of the function QInputDialog::getText can be QLineEdit::setValidator. The problem is how to get the pointer to the lineEdit to pass to the QValidator? The code below is not working:
@QString text = QInputDialog::getText(NULL, tr("Define price"), tr("Price:"), QLineEdit::setValidator(new MyDoubleValidator( 0, 10, 2, text)), defaultPrice, &ok);@
-
Hi,
You can't set any validator when using getText, it's a static function.
Calling setValidator like that won't compile. You are trying to call a non static function like it was one.
The simple way is to implement your own dialog to do what you want to achieve
-
So I need to make a class from QDialog or from QInputDialog?
Thanks for the reply!
-
Go from QDialog, it will be simpler, you just need a QLineEdit and QDialogButtonBox (plus the necessary eye candy like a label etc..)
-
Or, you simply use QInputDialog itself, but use the normal, non static API for it. It's not that hard to get a reference to the line edit. findChild<QLineEdit*>() on it should do it.
-
To add to the suggestion of Andre, you must first call setInputMode, otherwise you won't have the line edit available