Compare two numbers in a QString

There are two values:

QString str1 = "3.5.8", str2 = "20.3.6";

Let’s imagine that these two numbers represent the software version, as it were.

It is known that QString compares character-by-character.

What if we approach this decision in this way:

str1.replace(".","");
str2.replace(".","");
int n = str1.toInt();
int m = str2.toInt();

if (n >= m)
{
    qDebug() << "YES";
} else if (n <= m) {
    qDebug() << "NO";
}

Maybe there is a more optimal and correct way to do this.

Could you tell me please how I can translate these values into numbers so that they can be compared in their entirety. Thanks.

Answer

The QVersionNumber class was designed to solve this problem (requires Qt 5.6+):

QVersionNumber version1 = QVersionNumber::fromString(str1);
QVersionNumber version2 = QVersionNumber::fromString(str2);

if (version1 > version2)
{
    qDebug() << "YES";
}
else
{
    qDebug() << "NO";
}