Important: Please read the Qt Code of Conduct -

Problem with finding pattern using QFile[SOLVED]

  • Hello everyone!

    I have faced with some trouble while using next program:

    QFile file1("/Path/To/File/input.txt");;
    QString s = file1.readAll();
    if (!file1.isOpen()) {
        qDebug() << "Not";
        return -1;
    QRegExp reg(QString("%1").arg("Слово"));
    QLabel l;

    I am trying to find word in my file input.txt. There is only one word "Слово". But every time I getting result -1.

    But in next program:

    QString s = QByteArray("Слово");
    QRegExp reg(QString("%1").arg("Слово"));
    QLabel l;

    The result is 0.

    Why the results are different? And what I need to do to get correct answer in first program?
    Thank in advance.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why not just use QString's contains method ?

  • @SGaist Hello and thank you for answer.
    Thank for this method, but it doesn't help me with my problem.

  • Lifetime Qt Champion

    What's the encoding of input.txt ?

    By the way QRegExp rx("Слово"); is enough to create your QRegExp.

    On a side note, if you are using Qt 5, QRegularExpression is recommended.

  • @SGaist The Unicode encoding.
    So I need to use QTextStream to solve my problem.
    Thank for your help!

  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

Log in to reply