Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QByteArray function
Qt 6.11 is out! See what's new in the release blog

QByteArray function

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 3.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Ucn_
    wrote on last edited by Ucn_
    #1

    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;
    
    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

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

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      2
      • R Offline
        R Offline
        russjohn834
        wrote on last edited by
        #3

        @Ucn_
        Is your transportPic public?

        1 Reply Last reply
        0
        • U Offline
          U Offline
          Ucn_
          wrote on last edited by Ucn_
          #4

          @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();
          
                        ......
          
          1 Reply Last reply
          0
          • U Offline
            U Offline
            Ucn_
            wrote on last edited by
            #5

            @aha_1980 Could you help with this?

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • U Offline
                U Offline
                Ucn_
                wrote on last edited by Ucn_
                #7

                @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());
                
                
                }
                
                
                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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,

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  U 1 Reply Last reply
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    @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,

                    U Offline
                    U Offline
                    Ucn_
                    wrote on last edited by Ucn_
                    #9

                    @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

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      U 1 Reply Last reply
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        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.

                        U Offline
                        U Offline
                        Ucn_
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved