How to prevent a user from entering anything other than a URL in QLineEdit ?

Situation: we need a form (not necessarily QLineEdit , but I have not found a special form for links), which will not allow the user to enter anything other than links. I saw forms that allow only the input of numbers, not letters. You need something like this, only for links. In this case, when checking is not necessary to connect to the Internet. I know about the QUrl class, but its isValid () method skips non-working URLs for some reason.

  • What do you mean by non-working URLs? - maestro
  • @maestro Workers - with the correct syntax. But they can lead to a non-existent page, for example - point
  • four
    The correct syntax is RFC 3986 compliant, and the isValid method checks the URL for compliance with this standard. Give examples of broken URLs that the isValid method skips. - maestro
  • that is, what would start as a link and have https: // at the beginning? - jNX
  • @jNX, except for https if a bunch of others: http, ftp, file, etc. :) - gil9red

1 answer 1

Checking in the course of input is done using QValidator subclasses, you can make your own on the basis of QUrl , or you can simply take QRegExpValidator out of the box and a regular from Google with en-so * :

 QLineEdit *leUrl= new QLineEdit(this); QRegExp urlRx{"https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[az]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)"}; leUrl->setValidator(QRegExpValidator(urlRx)); 


* Do not forget to double backslashes, so that the C ++ compiler itself does not try to interpret them.