Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QInputDialog with custom QValidator
Qt 6.11 is out! See what's new in the release blog

Using QInputDialog with custom QValidator

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 6.1k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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);@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        So I need to make a class from QDialog or from QInputDialog?

        Thanks for the reply!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Go from QDialog, it will be simpler, you just need a QLineEdit and QDialogButtonBox (plus the necessary eye candy like a label etc..)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              To add to the suggestion of Andre, you must first call setInputMode, otherwise you won't have the line edit available

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt
              • Unsolved