QUrl url("http://example.com"); if(url.isValid()) { // Do something with the URL } else { // Handle invalid URL case }
QString url_str = "invalidurl"; QUrl url = QUrl::fromUserInput(url_str); if(url.isValid()) { // Do something with the URL } else { // Handle invalid URL case }In this example, a QString object is created with an invalid URL string. The fromUserInput static method of the QUrl class is used to create a QUrl object from the string. The isValid method is called on the object to determine if it is valid. If the URL is valid, the code inside the if statement is executed. If the URL is invalid, the code inside the else statement is executed. The QUrl class is part of the Qt Core library.