How to transfer QString to QByteArray?



  • Hi All:
    I'm using qt5.5 on my computer and Linux+QT for my OS system.
    I wanna compare the Array that I get from my MCU via RS23 with my QString.
    Here is my program.

     bytesWritten = comport.write(BIOS_name,sizeof(BIOS_name));
    
        if (bytesWritten == -1)
        {
            qDebug("Failed to write the data to port");
        }
        else if (!comport.waitForBytesWritten(80))
        {
            qDebug("Operation timed out or an error occurred for port");
        }
        readData=comport.readAll();
        while(comport.waitForReadyRead(80))
            readData.append(comport.readAll());
        ui->textEditPT_Ignitionversionshow->append(readData.constData());
        if (comport.error() == QSerialPort::ReadError)
        {
            ui->lineEditPT_IgnitionResult->setText("Failed");
            ui->pushButtonPT_Ignition_ResultLight->setStyleSheet("QPushButton{background-color:red;}");
            test_result[7]=false;
    
        }
        else if (comport.error() == QSerialPort::TimeoutError && readData.isEmpty())
        {
            ui->lineEditPT_IgnitionResult->setText("Failed");
            ui->pushButtonPT_Ignition_ResultLight->setStyleSheet("QPushButton{background-color:red;}");
            test_result[7]=false;
        }
        //else if(readData.contains("FA3A120-000000000-000-AG121-WN"))
        else if(readData.contains(ignition_firmware))
        {
            if(!ignition_bios_name_error)
            {
                ui->textEditPT_Ignitionversionshow->append(readData.constData());
                ui->lineEditPT_IgnitionResult->setText("Pass");
                ui->pushButtonPT_Ignition_ResultLight->setStyleSheet("QPushButton{background-color:green;}");
                test_result[7]=true;
            }
            else
            {
                ui->textEditPT_Ignitionversionshow->append("BIOS_name is wrong Please check the firmware of EC");
                ui->textEditPT_Ignitionversionshow->append(readData.constData());
                ui->lineEditPT_IgnitionResult->setText("Failed");
                ui->pushButtonPT_Ignition_ResultLight->setStyleSheet("QPushButton{background-color:red;}");
                test_result[7]=false;
            }
        }
    

    But I will get the error like below.

    mainwindow.cpp: In member function 'void MainWindow::on_pushButtonPT_Ignitionversion_clicked()':
         ^
    mainwindow.cpp:2649:48: error: no matching function for call to 'QByteArray::contains(QString&)'
         else if(readData.contains(ignition_firmware))
                                                    ^
    In file included from ../../sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/usr/include/qt5/QtCore/qstring.h:42:0,
                     from ../../sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/usr/include/qt5/QtCore/qobject.h:41,
                     from ../../sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/usr/include/qt5/QtWidgets/qwidget.h:38,
                     from ../../sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/usr/include/qt5/QtWidgets/qmainwindow.h:37,
                     from ../../sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/usr/include/qt5/QtWidgets/QMainWindow:1,
                     from mainwindow.h:4,
                     from mainwindow.cpp:1:
    ../../sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/usr/include/qt5/QtCore/qbytearray.h:585:13: note: candidate: bool QByteArray::contains(char) const
     inline bool QByteArray::contains(char c) const
                 ^
    ../../sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/usr/include/qt5/QtCore/qbytearray.h:585:13: note:   no known conversion for argument 1 from 'QString' to 'char'
    ../../sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/usr/include/qt5/QtCore/qbytearray.h:635:13: note: candidate: bool QByteArray::contains(const char*) const
     inline bool QByteArray::contains(const char *c) const
                 ^
    ../../sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/usr/include/qt5/QtCore/qbytearray.h:635:13: note:   no known conversion for argument 1 from 'QString' to 'const char*'
    ../../sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/usr/include/qt5/QtCore/qbytearray.h:583:13: note: candidate: bool QByteArray::contains(const QByteArray&) const
     inline bool QByteArray::contains(const QByteArray &a) const
    

    How can I compare QString and QBytearray?
    Thanks in Advanced!


  • Lifetime Qt Champion

    Hi,

    The most simple would be to make ignition_firmware a QByteArray also.



  • @victor-wang
    toUtf8() return a QByteArray.

    For Example:

    QString str("abcd");
    QByteArray br = str.toUtf8();
    

  • Lifetime Qt Champion

    @Taz742 while technically correct, it's a bit overkill in this case.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.