Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QByteArray function



  • Hello, I am trying to pass QByteArray data from one class to another class in a different file. I am able to send and receive the data because when I print with qDebug it shows. But I'm not able to use it out of the function, I have tried to append, but when I print or try to use "useData" it shows nothing, it only shows if I print inside transportPic(). How can I get the data out of the function? Thanks
    code:

    public:
         QByteArray transportPic(QByteArray name){
    
              useData = name;
              qDebug() << "ByteArray " << name.toHex();
              return name;
          }
    
    private:
        QByteArray useData;
    


  • Show us how you are trying to use it. Maybe show the rest of the class that useData is defined in too.



  • @Ucn_
    Is your transportPic public?



  • @russjohn834 yes it's public.
    @fcarney I try to insert into my database, I have tested using the code bellow, It worked then I moved to another class because of some conditions. I pass the parameters the QByteArray comes till the "transport()" and it prints the data, but it doesn't show in the desired function.

    //I moved this code to another class and pass the parameter through "transport()" in the "transport()" I can print and the data is there.
    //    QPixmap inPixmap;
    //    inPixmap.load(":/file/mypic");
    //    QByteArray inByteArray;
    //    QBuffer inBuffer( &inByteArray );
    //    inBuffer.open( QIODevice::WriteOnly );
    //    inPixmap.save( &inBuffer, "JPG" );
    
           QSqlQuery query;
           query.setForwardOnly(true);
           query.prepare("{CALL InsertTestingImg(? , ?, ?, ?)}");
                  query.bindValue(0, createdB);
                  query.bindValue(1, imgtext);
                  query.bindValue(2, imgtexts);
                  query.bindValue(3, useData); //I use it here
    
                  	//And test here
                  qDebug() << "Send Array from DbPic" << useData.toHex();
    
                  ......
    


  • @aha_1980 Could you help with this?


  • Lifetime Qt Champion

    @Ucn_ said in QByteArray function:

    Could you help with this?

    Please show us code where you call your class function and where you execute your function with the sql stuff.



  • @Christian-Ehrlicher :

    sqlServerconnection.h

    public:
    	......
         QByteArray transportPic(QByteArray name){
    
              useData = name;
              qDebug() << "ByteArray " << name.toHex();
              return name;
          }
    
    void setStudentData(QString fname, QString sname,QString lname,
                            QString gender, QString birthday,
                            QString country, QString city,
                            QString telephonN, QString stdId,
                            QString eduLevel, QString status,
                            QString bookLevel, QString classR,
                            QString classT){
    
            insertStudentData(fname, sname, lname, gender, birthday, country, city,
                              telephonN, stdId, eduLevel, status,
                              bookLevel, classR, classT);
    
        }
    
    private:
        QByteArray useData;
        .....
    

    sqlServerconnection.cpp

    void sqlServerConnection::insertStudentData(QString fname, QString sname,QString lname,
                                                QString gender, QString birthday,
                                                QString country, QString city,
                                                QString telephonN, QString stdId,
                                                QString eduLevel, QString status,
                                                QString bookLevel, QString classR,
                                                QString classT)
    
    {
    //    QPixmap inPixmap;
    //    inPixmap.load(":/file/mypics");
    //    QByteArray inByteArray;
    //    QBuffer inBuffer( &inByteArray );
    //    inBuffer.open( QIODevice::WriteOnly );
    //    inPixmap.save( &inBuffer, "JPG" );
    
        QString name = "nameTest";
        QString lname = "lnameTest";
        QString imgname = "pic.jpg";
        QDateTime currentDT = QDateTime::currentDateTime();
    
           QSqlQuery query;
           query.setForwardOnly(true);
           query.prepare("{CALL InsertTestingImg(? , ?, ?, ?)}");
                  query.bindValue(0, name);
                  query.bindValue(1, lname);
                  query.bindValue(2, imgname);
                  query.bindValue(3, useData);
    
    
           if( !query.exec() ){
    
                qDebug() << "Error inserting into table:\n" << query.lastError();
    
           } 
    
            qDebug() << "useData Test" << useData.toHex();
    
    
    }
    

    Secondui.cpp

    SecondUi::loadAndSetPic(QStringList fileNames, QByteArray outByteArray, qint32 funcSender)
    {
    
        switch (funcSender) {
        case 1:
            {
    
                QPixmap inPixmap;
                inPixmap.load(":/file/mypics");
    
                QByteArray inByteArray;
                QBuffer inBuffer( &inByteArray );
                inBuffer.open( QIODevice::WriteOnly );
                inPixmap.save( &inBuffer, "JPG" );
    
                QPixmap outPixmap = QPixmap();
                outPixmap.loadFromData( inByteArray );
    
                sqlServerConnection *sendPicArray;
                sendPicArray = new sqlServerConnection();
    
                sendPicArray->transportPic(inByteArray);
                studentViewPic->setPixmap(outPixmap.scaled(100,100,Qt::KeepAspectRatio));
    
            }
            break;
        case 2:
            break;
    
        }
    }
    
    
    void SecondUi::on_pushButton_3_clicked()
    
    {
        sqlServerConnection *setStudentInfo;
        setStudentInfo = new sqlServerConnection();
    
    
            setStudentInfo->setStudentData(ui2.nameLineEdit->text(), ui2.middleNameLineEdit->text(),
                                           ui2.surnameLineEdit->text(), ui2.genderComboBox->currentText(),
                                           ui2.birthDateDateEdit->text(), ui2.countryLineEdit->text(),
                                           ui2.cityLineEdit->text(), ui2.telephoneLineEdit->text(),
                                           ui2.studentIdLineEdit->text(), ui2.educationLevelComboBox->currentText(),
                                           ui2.statusComboBox->currentText(), ui2.bookLevelComboBox->currentText(),
                                           ui2.classroomComboBox->currentText(), ui2.classTimeComboBox->currentText());
    
    
    }
    
    

  • Lifetime Qt Champion

    @Ucn_ said in QByteArray function:

    And where do you call insertStudentData? You're creating a new, local instance inside the switch statement but don't do anything with it apart from calling transportPic() function and leaking this object afterwards,



  • @Christian-Ehrlicher I updated the code to show where I call insertStudentData, where do I create the instance? Actually apart from transportPic() I use it in other functions, but when I create where my class initialize and try to use in the functions it says is not declared. I delete the pointer after


  • Lifetime Qt Champion

    Now you're creating two instances of sqlServerConnection - one in on_pushButton_3_clicked() and one in loadAndSetPic(). If you want to have one instance you should e.g. make them a member pointer - C++ basics.



  • @Christian-Ehrlicher I see, thanks. Is that the reason for the QByteArray not displaying when trying to use?


Log in to reply