Home > Software engineering >  Getting an integer from a vector
Getting an integer from a vector

Time:12-16

    Customer c = data.getCustomer(index.row());
    QString assetId = QString::fromStdString(c.getAssetId());
    QString name = QString::fromStdString(c.getName());
    QString price = QString::fromStdString(c.getPrice());
    QString quantity = QString::fromStdString(c.getQuantity());
    QString category = QString::fromStdString(c.getCategory());
    QString roomNumber = QString::fromStdString(c.getRoomNumber());

I am trying to get an integer from assetId, price, quantity, roomNumber.

I'm unsure what syntax to use to get the int from Qstring.

CodePudding user response:

You can use toInt member function of QString class

QString str = "10";
int i = str.toInt(); // now i is 10.

If you need to check if conversion was successful, you can also use additional bool to indicate success.

QString str = "not a number";
bool success;
int i = str.toInt(&success); // success is false, because conversion failed, i holds value 0. 
  • Related