Important: Please read the Qt Code of Conduct -

[SOLVED] QString.Contains Code works only with break point, does not work if no break point

  • I am a beginner on QSerialPort Terminal Example.
    Whenever my sensor initialization happens, SerialPort should receive data string that contains the menu selection string SenInitialMenuString as shown below.

    Here is my code:

    void MainWindow::readData()

    QString SenDataString;
    QString SenInitialMenuString = "------SparkFun SPI Shortcut------\n\r\n\rMAIN MENU:\n\r(1) Actions\n\r(2) Settings\n\r\n\r->";
    QByteArray data = serial->readAll();
    SenDataString = data;
        qDebug("\nThis is the %d time initialization", ++NumOfInitial);
        SenFakeKey = new QKeyEvent(QEvent::KeyPress,Qt::Key_1,Qt::NoModifier, "1",false,1);


    However, this code works very well only if I add a break point at the line of
    " if(SenDataString.contains(SenInitialMenuString))",
    whenever I run the code with no break point, it will pass it like it was never there.

    Could anyone help me find out what is going on ?


  • I added a delay function before reading the serial port,

    QByteArray data = serial->readAll();

    now everything is working.