c++qthashmd5

How to create MD5 hash in Qt?


I want to create a MD5 hash code in Qt.

My code :

QString queryStr;
queryStr = QString("%1")
.arg(QString(QCryptographicHash::hash(ui->txtPassword->text(),QCryptographicHash::Md5).toHex()));

but my code does not work! hash method does not work in Qt!

Any suggestion?


Solution

  • text() returns QString, QCryptographicHash::hash requires QByteArray and there is no implicit conversion, so you should do this by yourself. Use something like this:

    QString queryStr;
    
    ui->lineEdit_2->setText("hash");
    queryStr = QString("%1").arg(QString(QCryptographicHash::hash(ui->lineEdit_2->text().toUtf8(),QCryptographicHash::Md5).toHex()));
    qDebug()<< queryStr;
    

    In the documentation you can see another methods which return QByteArray. Choose the best for you.

    https://doc.qt.io/qt-5/qstring.html