Convert Qvector<Qstring> to int in C++

Asked

Viewed 155 times

3

I have a Qvector and I want to convert it so I can make comparisons to find out which one is bigger.

I tried to do this but it doesn’t work:

QVector<QString> vector;
std::atoi(vector.value (i).toStdString ());

2 answers

4


You can convert a Qstring to int using Qt itself:

vector.at( i ).toInt();

Or, if you only need the amount:

QVector<QString> vector;
vector << "1278" << "7" << "9";

int max = 0;
foreach (QString s, vector) {
   max = qMax( max, s.toInt() );
}

qDebug() << max;

http://qt-project.org/doc/qt-4.8/qstring.html#toInt

  • I didn’t even think to see if there was a method in Qstring itself. Thank you very much

  • 1

    @Giovaniracipaganini @Wakim’s reply was already good, but I chose to add an example with foreach, which may be useful as well, depending on your practical case.

  • Yes, I think it’s worth setting the example, I didn’t think of it at the time.

3

Incredible as it may seem (so big that is the QT) class QString has a method that makes the Parsing of its contents to whole, gives a look at QString.toInt.

  • Thanks for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.