Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLineEdit validator memory management
QtWS25 Last Chance

QLineEdit validator memory management

Scheduled Pinned Locked Moved Solved General and Desktop
qvalidator
5 Posts 3 Posters 2.1k 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.
  • E Offline
    E Offline
    Eligijus
    wrote on last edited by
    #1

    Hi,
    I'd like to set QIntValidator for QLineEdit multiple times without having to store pointer of said validator. And that looks something like this:

    lineEdit->setValidator(new QIntValidator(1, 2));
    //some code
    lineEdit->setValidator(new QIntValidator(2, 3));
    

    So at this point I'm not sure if setting new validator deletes the old one.
    If not can i delete it by doing the following:

    delete lineEdit->validator();
    

    Or do I have to cast it?

    delete qobject_cast<const QIntValidator *>(lineEdit->validator());
    
    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      AFAIK, no it doesn't delete anything. The QLineEdit object doesn't become the owner of the validator. Also, if you take look at the examples, all create a QValidator object with the parent being the widget the validator is set on.

      E Offline
      E Offline
      Eligijus
      wrote on last edited by
      #4

      @SGaist In this example http://doc.qt.io/qt-5.8/qtwidgets-widgets-lineedits-example.html QIntValidator parent is set to QLineEdit and it appears that there is a memory leak.

      So the solution would be( thanks to @JohanSolo ) :

      delete lineEdit->validator();
      
      1 Reply Last reply
      0
      • JohanSoloJ Offline
        JohanSoloJ Offline
        JohanSolo
        wrote on last edited by JohanSolo
        #2

        No need to cast, thanks to polymorphism.

        Edit: the docs appear not so clear to me, but if setting a nullptr validator unsets the current one, I'd bet setting a new one would properly delete the old one.

        `They did not know it was impossible, so they did it.'
        -- Mark Twain

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

          Hi,

          AFAIK, no it doesn't delete anything. The QLineEdit object doesn't become the owner of the validator. Also, if you take look at the examples, all create a QValidator object with the parent being the widget the validator is set on.

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

          E 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            AFAIK, no it doesn't delete anything. The QLineEdit object doesn't become the owner of the validator. Also, if you take look at the examples, all create a QValidator object with the parent being the widget the validator is set on.

            E Offline
            E Offline
            Eligijus
            wrote on last edited by
            #4

            @SGaist In this example http://doc.qt.io/qt-5.8/qtwidgets-widgets-lineedits-example.html QIntValidator parent is set to QLineEdit and it appears that there is a memory leak.

            So the solution would be( thanks to @JohanSolo ) :

            delete lineEdit->validator();
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #5

              No there's not, the QIntValidator gets the QLineEdit as parent and thus it will get deleted at the same time as the widget.

              No leak per se but indeed, if you start to change the validator frequently you'll have a bunch of unused objects lying around that will get deleted when the widget is destroyed.

              I'd recommend calling deleteLater so you ensure that if there are events pending nothing goes wrong.

              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
              1

              • Login

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