Those loops still bother me so here's an improvement idea:
// pick matching strings
// sort by version
qSort(gs.begin(), gs.end(), gsVersionGreaterThan);
// now the first item of gs is "Ghostscript M.N" with latest version (unless gs is empty, of course)
QStringList allsoftware = softs.childGroups();
// pick matching strings
QStringList gs = allsoftware.filter(QRegExp("Ghostscript \\d+\\.\\d+"));
// sort by version
qSort(gs.begin(), gs.end(), gsVersionGreaterThan);
// now the first item of gs is "Ghostscript M.N" with latest version (unless gs is empty, of course)
To copy to clipboard, switch view to plain text mode
The compare function would look more or less something like this:
bool gsVersionGreaterThan(const QString& s1, const QString& s2)
{
QRegExp regExp1
("Ghostscript (\\d+)\\.(\\d+)");
QRegExp regExp2
("Ghostscript (\\d+)\\.(\\d+)");
int pos1 = regExp1.indexIn(s1);
int pos2 = regExp2.indexIn(s2);
if (pos1 > -1 && pos2 > -1)
{
int maj1 = regExp1.cap(1).toInt();
int maj2 = regExp2.cap(1).toInt();
int min1 = regExp1.cap(2).toInt();
int min2 = regExp2.cap(2).toInt();
return maj1 > maj2 || (maj1 == maj2 && min1 > min2);
}
return false;
}
bool gsVersionGreaterThan(const QString& s1, const QString& s2)
{
QRegExp regExp1("Ghostscript (\\d+)\\.(\\d+)");
QRegExp regExp2("Ghostscript (\\d+)\\.(\\d+)");
int pos1 = regExp1.indexIn(s1);
int pos2 = regExp2.indexIn(s2);
if (pos1 > -1 && pos2 > -1)
{
int maj1 = regExp1.cap(1).toInt();
int maj2 = regExp2.cap(1).toInt();
int min1 = regExp1.cap(2).toInt();
int min2 = regExp2.cap(2).toInt();
return maj1 > maj2 || (maj1 == maj2 && min1 > min2);
}
return false;
}
To copy to clipboard, switch view to plain text mode
Bookmarks