Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. Why doesn't QInputDialog::getText() allow passing a custom QValidator?

Why doesn't QInputDialog::getText() allow passing a custom QValidator?

Scheduled Pinned Locked Moved Unsolved The Lounge
validatordialogs
7 Posts 3 Posters 83 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    L0uisc
    wrote 14 days ago last edited by
    #1

    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 the QInputDialog is not inputting text and the validator() call can return nullptr 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 a QValidator * 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's QLineEdit widget. Let me know if you agree or if there is a good reason I'm missing why this can not be done.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      CassD
      wrote 14 days ago last edited by
      #2

      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.

      J L 2 Replies Last reply 14 days ago
      0
      • C CassD
        14 days ago

        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.

        J Offline
        J Offline
        JonB
        wrote 14 days ago last edited by
        #3

        @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 the QLineEdit to place your validator. Although it does not look like you are intended to do this, try creating a QInputDialog instance (not one the static get...() methods), set its inputMode and use QLineEdit *lineEdit = dialog->findChild<QLineEdit *>(). That may give you the line edit, unless they create it dynamically, I don't know.

        1 Reply Last reply
        1
        • C CassD
          14 days ago

          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.

          L Offline
          L Offline
          L0uisc
          wrote 14 days ago last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            CassD
            wrote 14 days ago last edited by
            #5

            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.

            L 1 Reply Last reply 14 days ago
            0
            • C CassD
              14 days ago

              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.

              L Offline
              L Offline
              L0uisc
              wrote 14 days ago last edited by
              #6

              @CassD I know there are no validator() and setValidator() methods on QInputDialog. 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.

              J 1 Reply Last reply 14 days ago
              0
              • L L0uisc
                14 days ago

                @CassD I know there are no validator() and setValidator() methods on QInputDialog. 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.

                J Offline
                J Offline
                JonB
                wrote 14 days ago last edited by JonB 5 Jan 2025, 18:10
                #7

                @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 of QDialog. 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 Reply Last reply
                1

                1/7

                1 May 2025, 15:44

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved