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. Appending an image to itemList of QFormLayout widgets
Forum Updated to NodeBB v4.3 + New Features

Appending an image to itemList of QFormLayout widgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
51 Posts 4 Posters 6.0k Views 3 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.
  • C CEO.
    14 Sept 2021, 12:35

    @SGaist I said I stored it in the database with varbinary data type.

    G Offline
    G Offline
    Gojir4
    wrote on 14 Sept 2021, 12:58 last edited by
    #4

    @CEO said in Appending an image to itemList of QFormLayout widgets:

    @SGaist I said I stored it in the database with varbinary data type.

    And that's exactly what @SGaist's answer is about. He doesn't ask "What type of data is" but "What format are these images" means what format of image do you store ? PNG ? JPG ? ... so QImage's constructor know how to interpret binary data...

    1 Reply Last reply
    0
    • C Offline
      C Offline
      CEO.
      wrote on 14 Sept 2021, 13:16 last edited by
      #5

      jpg and png.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CEO.
        wrote on 14 Sept 2021, 13:16 last edited by
        #6

        @mrjj attention please

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Sept 2021, 18:30 last edited by
          #7

          Since you have standard image formats stored then again:

          @SGaist said in Appending an image to itemList of QFormLayout widgets:

          Depending on that, you can create a QImage from the binary data to later convert as QPixmap for QLabel.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply 14 Sept 2021, 18:53
          1
          • S SGaist
            14 Sept 2021, 18:30

            Since you have standard image formats stored then again:

            @SGaist said in Appending an image to itemList of QFormLayout widgets:

            Depending on that, you can create a QImage from the binary data to later convert as QPixmap for QLabel.

            C Offline
            C Offline
            CEO.
            wrote on 14 Sept 2021, 18:53 last edited by
            #8

            @SGaist hello,
            Would you mind to write some lines of code? I will appreciate it.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 14 Sept 2021, 19:10 last edited by SGaist
              #9
              QLabel label;
              label.setPixmap(QPixmap::fromImage(QImage(databaseImageData)));
              label.show();
              

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              C 1 Reply Last reply 15 Sept 2021, 00:46
              1
              • S SGaist
                14 Sept 2021, 19:10
                QLabel label;
                label.setPixmap(QPixmap::fromImage(QImage(databaseImageData)));
                label.show();
                
                C Offline
                C Offline
                CEO.
                wrote on 15 Sept 2021, 00:46 last edited by
                #10

                @SGaist thanks for taking your time to help me. I decided to do a little sketch of what I want in a simpler project. I will drop the code here.

                See, in this program, data is retrieved from the database and the data is appended in their corresponding qLineEdits. The data are: photo stored in varbinary(max) in the database, dateRecorded and photopath. Mind you, this is just an example to pass my message better, my original project is very long, so I choose to use this sketchy one.

                Now, the retrieved data will be displayed as texts in the qLineEdits (self.imgbyt, self.dattetx2, self.pixtx2)

                the self.imgbyt QLineEdit will hold the image data which displays as a text in the QLineEdit ( in bytes form).

                I want the text in the self.imgbyt QLineEdit to be converted to jpg or jpeg image so the image can be displayed on the qpixmap

                imgg.JPG

                Here's the code for the openFile function. This is to select images to upload when saving record.

                   def openfile(self):
                    try:
                        fdate = datetime.datetime.now()
                        strfd = str(fdate)
                        self.datte.setText(strfd)
                        fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)")
                        self.img = open(fname, 'rb')
                        self.pixL.setPixmap(QPixmap(fname).scaled(250, 250))
                        self.pixTx.setText(fname)
                        self.pixL.setText(self.img)
                        #print(self.pixL.setText(self.img))
                
                    except Exception as err:
                     print(err)
                

                This is the code for the retrieval function.

                  def retrieval(self):
                          self.db_connection()
                          sch = self.srch.text()
                          try:
                              self.cursor.execute("SELECT * FROM pixxdate WHERE photopath LIKE ? OR imagedata LIKE ? OR dateRecorded LIKE ?",(sch,sch,sch) )
                              row = self.cursor.fetchone()
                              print(row)
                              if row:
                                  self.imgbyt.setText(str(row[2]))
                                  self.pixtx2.setText((row[1]))
                                  self.dattex2.setText(str(row[3]))
                  
                              imgg = self.imgbyt.text()
                              imgfileobj = open(imgg)
                              imgbinary = imgfileobj.read()
                              imgstrm = io.BytesIO(imgbinary)
                              imgfile = Image.open(imgstrm)
                              print("imgfile.size = %s" %imgfile.size)
                              self.pixL2.setText(imgfileobj)
                  
                  
                          except Exception as err:
                              print(err)
                
                C 1 Reply Last reply 15 Sept 2021, 09:17
                0
                • C CEO.
                  15 Sept 2021, 00:46

                  @SGaist thanks for taking your time to help me. I decided to do a little sketch of what I want in a simpler project. I will drop the code here.

                  See, in this program, data is retrieved from the database and the data is appended in their corresponding qLineEdits. The data are: photo stored in varbinary(max) in the database, dateRecorded and photopath. Mind you, this is just an example to pass my message better, my original project is very long, so I choose to use this sketchy one.

                  Now, the retrieved data will be displayed as texts in the qLineEdits (self.imgbyt, self.dattetx2, self.pixtx2)

                  the self.imgbyt QLineEdit will hold the image data which displays as a text in the QLineEdit ( in bytes form).

                  I want the text in the self.imgbyt QLineEdit to be converted to jpg or jpeg image so the image can be displayed on the qpixmap

                  imgg.JPG

                  Here's the code for the openFile function. This is to select images to upload when saving record.

                     def openfile(self):
                      try:
                          fdate = datetime.datetime.now()
                          strfd = str(fdate)
                          self.datte.setText(strfd)
                          fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)")
                          self.img = open(fname, 'rb')
                          self.pixL.setPixmap(QPixmap(fname).scaled(250, 250))
                          self.pixTx.setText(fname)
                          self.pixL.setText(self.img)
                          #print(self.pixL.setText(self.img))
                  
                      except Exception as err:
                       print(err)
                  

                  This is the code for the retrieval function.

                    def retrieval(self):
                            self.db_connection()
                            sch = self.srch.text()
                            try:
                                self.cursor.execute("SELECT * FROM pixxdate WHERE photopath LIKE ? OR imagedata LIKE ? OR dateRecorded LIKE ?",(sch,sch,sch) )
                                row = self.cursor.fetchone()
                                print(row)
                                if row:
                                    self.imgbyt.setText(str(row[2]))
                                    self.pixtx2.setText((row[1]))
                                    self.dattex2.setText(str(row[3]))
                    
                                imgg = self.imgbyt.text()
                                imgfileobj = open(imgg)
                                imgbinary = imgfileobj.read()
                                imgstrm = io.BytesIO(imgbinary)
                                imgfile = Image.open(imgstrm)
                                print("imgfile.size = %s" %imgfile.size)
                                self.pixL2.setText(imgfileobj)
                    
                    
                            except Exception as err:
                                print(err)
                  
                  C Offline
                  C Offline
                  CEO.
                  wrote on 15 Sept 2021, 09:17 last edited by CEO.
                  #11

                  @SGaist I have resolved it. I used:

                     def retrieval(self):
                    
                            self.db_connection()
                    
                            sch = self.srch.text()
                    
                            try:
                    
                                self.cursor.execute("SELECT * FROM pixxdate WHERE photopath LIKE ? OR imagedata LIKE ? OR dateRecorded LIKE ?",(sch,sch,sch) )
                    
                                row = self.cursor.fetchone()
                    
                                print(row)
                    
                                if row:
                    
                                    self.imgbyt.setText(str(row[2]))
                    
                                    self.pixtx2.setText((row[1]))
                    
                                    self.dattex2.setText(str(row[3]))
                    
                    
                    
                                imgstrm = io.BytesIO(row[2])
                    
                                imgfile = Image.open(imgstrm)
                    
                                print("imgfile.size = %s" % str(imgfile.size))
                    
                                self.pixL2.setPixmap(ImageQt.toqpixmap(imgfile))
                    
                    
                    
                    
                    
                            except Exception as err:
                    
                                print(err)
                  

                  I am now trying to apply same logic in my main project with the qtablewidget and qformlayout widgets

                  1 Reply Last reply
                  1
                  • C Offline
                    C Offline
                    CEO.
                    wrote on 15 Sept 2021, 14:12 last edited by CEO.
                    #12

                    @mrjj please I need your help here. I know you can help me make the photo display in an arrayList

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 15 Sept 2021, 14:39 last edited by
                      #13

                      Why are you going through a conversion rather than using the content of row[2] directly to build your QImage ?

                      @CEO said in Appending an image to itemList of QFormLayout widgets:

                      @mrjj please I need your help here. I know you can help me make the photo display in an arrayList

                      Using a QListView and using the decoration would likely be the simplest way to show your images.

                      In any case, what did you try until now ?
                      What is your exact issue ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      C 1 Reply Last reply 15 Sept 2021, 15:41
                      1
                      • S SGaist
                        15 Sept 2021, 14:39

                        Why are you going through a conversion rather than using the content of row[2] directly to build your QImage ?

                        @CEO said in Appending an image to itemList of QFormLayout widgets:

                        @mrjj please I need your help here. I know you can help me make the photo display in an arrayList

                        Using a QListView and using the decoration would likely be the simplest way to show your images.

                        In any case, what did you try until now ?
                        What is your exact issue ?

                        C Offline
                        C Offline
                        CEO.
                        wrote on 15 Sept 2021, 15:41 last edited by
                        #14

                        @SGaist hello, I've achieved my aim with that. You didn't tell me explanatorily when I needed a straight sample code from you to ease my stress.

                        That conversion was to convert the row 2 in the database to jpg image. It's not making use of the texts in the qLineEdits (self.imgbyt). I tried that but encountered error.

                        You don't give someone straight code even when one is already too stressed.

                        Now, I am trying to make a retrieved varbinary data display in the qpixmap in my main project.

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          CEO.
                          wrote on 15 Sept 2021, 16:32 last edited by
                          #15

                          part of the sql data retrieval code:

                                                 result = self.cursor.fetchall()             
                                               for row_number, row_data in enumerate(result):             
                                                   self.tab.insertRow(row_number)             
                                                   for column_number, data in enumerate(row_data):             
                                                       self.tab.setItem(row_number, column_number, QTableWidgetItem(str(data)))
                          

                          part of the itemsArray code:

                                                        for row in rows:                      
                                                            for col in range(self.tab.columnCount()):                      
                                                                itemArray = self.tab.item(row, col)
                                          
                                                              
                          
                                               
                                             text = "" if itemArray is None else itemArray.text()                 
                                          
                                               if (col == 14):
                                              itemList[12].setText(text)
                                              imgcon = self.passlbbtx.text()
                                              imgstrm = io.BytesIO(imgcon)
                                              imgfile = Image.open(imgstrm)
                                              self.passlb.setPixmap(ImageQt.toqpixmap(imgfile).scaled(250, 250))
                          

                          it runs but with an error message: "a bytes-like object is required, not 'str' "

                          C 1 Reply Last reply 16 Sept 2021, 05:55
                          0
                          • M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 15 Sept 2021, 16:53 last edited by
                            #16

                            Hi

                            • it runs but with an error message: "a bytes-like object is required, not 'str' "

                            for which line ?

                            imgstrm = io.BytesIO(imgcon) ?

                            in the original / first sample you get it from
                            imgstrm = io.BytesIO(row[2])

                            so is that the same data in your real app you give it ?

                            C 2 Replies Last reply 15 Sept 2021, 17:35
                            0
                            • M mrjj
                              15 Sept 2021, 16:53

                              Hi

                              • it runs but with an error message: "a bytes-like object is required, not 'str' "

                              for which line ?

                              imgstrm = io.BytesIO(imgcon) ?

                              in the original / first sample you get it from
                              imgstrm = io.BytesIO(row[2])

                              so is that the same data in your real app you give it ?

                              C Offline
                              C Offline
                              CEO.
                              wrote on 15 Sept 2021, 17:35 last edited by
                              #17

                              @mrjj heeeeyyyyy you're here. From these lines:

                                             imgcon = bytes(self.passlbbtx.text(), 'utf-8')
                                              imgstrm = io.BytesIO(imgcon)
                                              imgfile = Image.open(imgstrm)
                              
                              C 1 Reply Last reply 15 Sept 2021, 17:51
                              0
                              • M mrjj
                                15 Sept 2021, 16:53

                                Hi

                                • it runs but with an error message: "a bytes-like object is required, not 'str' "

                                for which line ?

                                imgstrm = io.BytesIO(imgcon) ?

                                in the original / first sample you get it from
                                imgstrm = io.BytesIO(row[2])

                                so is that the same data in your real app you give it ?

                                C Offline
                                C Offline
                                CEO.
                                wrote on 15 Sept 2021, 17:36 last edited by
                                #18

                                @mrjj said in Appending an image to itemList of QFormLayout widgets:

                                Hi

                                • it runs but with an error message: "a bytes-like object is required, not 'str' "

                                for which line ?

                                imgstrm = io.BytesIO(imgcon) ?

                                in the original / first sample you get it from
                                imgstrm = io.BytesIO(row[2])

                                so is that the same data in your real app you give it ?

                                No, that's not actually the original. That's just like a rough work which I was using to test. That one was without a Qtablewidget for display of the retrieved data.

                                1 Reply Last reply
                                0
                                • C CEO.
                                  15 Sept 2021, 17:35

                                  @mrjj heeeeyyyyy you're here. From these lines:

                                                 imgcon = bytes(self.passlbbtx.text(), 'utf-8')
                                                  imgstrm = io.BytesIO(imgcon)
                                                  imgfile = Image.open(imgstrm)
                                  
                                  C Offline
                                  C Offline
                                  CEO.
                                  wrote on 15 Sept 2021, 17:51 last edited by CEO.
                                  #19

                                  @CEO said in Appending an image to itemList of QFormLayout widgets:

                                  @mrjj heeeeyyyyy you're here. From these lines:

                                                 imgcon = bytes(self.passlbbtx.text(), 'utf-8')
                                                  imgstrm = io.BytesIO(imgcon)
                                                  imgfile = Image.open(imgstrm)
                                  

                                  You would notice I tried to do a conversion from string to byte here. This is because the data is automatically appended in the QLineEdits of the qFormLayout as a string after it was retrieved and displayed in the cells of the qtablewidget as a byte.

                                  So I was trying to convert that string to byte and then convert the byte to jpg/jpeg image format.

                                  M 1 Reply Last reply 16 Sept 2021, 05:30
                                  0
                                  • C CEO.
                                    15 Sept 2021, 17:51

                                    @CEO said in Appending an image to itemList of QFormLayout widgets:

                                    @mrjj heeeeyyyyy you're here. From these lines:

                                                   imgcon = bytes(self.passlbbtx.text(), 'utf-8')
                                                    imgstrm = io.BytesIO(imgcon)
                                                    imgfile = Image.open(imgstrm)
                                    

                                    You would notice I tried to do a conversion from string to byte here. This is because the data is automatically appended in the QLineEdits of the qFormLayout as a string after it was retrieved and displayed in the cells of the qtablewidget as a byte.

                                    So I was trying to convert that string to byte and then convert the byte to jpg/jpeg image format.

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 16 Sept 2021, 05:30 last edited by
                                    #20

                                    @CEO
                                    Hi
                                    In the orignal code, you convert directly from io.BytesIO(row[2]) is is directly from self.cursor.execute
                                    so I guess it contains QBytearray in a Qvariant

                                    However, in real app it seems you are trying to take it from a Widget self.passlbbtx.text(),
                                    and I'm not sure that can work as its a QString.

                                    what is passlbbtx ?

                                    1 Reply Last reply
                                    0
                                    • C CEO.
                                      15 Sept 2021, 16:32

                                      part of the sql data retrieval code:

                                                             result = self.cursor.fetchall()             
                                                           for row_number, row_data in enumerate(result):             
                                                               self.tab.insertRow(row_number)             
                                                               for column_number, data in enumerate(row_data):             
                                                                   self.tab.setItem(row_number, column_number, QTableWidgetItem(str(data)))
                                      

                                      part of the itemsArray code:

                                                                    for row in rows:                      
                                                                        for col in range(self.tab.columnCount()):                      
                                                                            itemArray = self.tab.item(row, col)
                                                      
                                                                          
                                      
                                                           
                                                         text = "" if itemArray is None else itemArray.text()                 
                                                      
                                                           if (col == 14):
                                                          itemList[12].setText(text)
                                                          imgcon = self.passlbbtx.text()
                                                          imgstrm = io.BytesIO(imgcon)
                                                          imgfile = Image.open(imgstrm)
                                                          self.passlb.setPixmap(ImageQt.toqpixmap(imgfile).scaled(250, 250))
                                      

                                      it runs but with an error message: "a bytes-like object is required, not 'str' "

                                      C Offline
                                      C Offline
                                      CEO.
                                      wrote on 16 Sept 2021, 05:55 last edited by CEO.
                                      #21

                                      @CEO said in Appending an image to itemList of QFormLayout widgets:

                                      part of the sql data retrieval code:

                                                             result = self.cursor.fetchall()             
                                                           for row_number, row_data in enumerate(result):             
                                                               self.tab.insertRow(row_number)             
                                                               for column_number, data in enumerate(row_data):             
                                                                   self.tab.setItem(row_number, column_number, QTableWidgetItem(str(data)))
                                      

                                      it runs but with an error message: "a bytes-like object is required, not 'str' "

                                      Could the self.tab.setItem(row_number, column_number, QtablewidgetItem(str(data))) be where the issue is coming from?

                                      self.passlbtx is a QLineEdit for appending the varbinary data retrieved and displayed in a column of the qtablewidget.

                                      @mrjj

                                      M 1 Reply Last reply 16 Sept 2021, 06:10
                                      0
                                      • C CEO.
                                        16 Sept 2021, 05:55

                                        @CEO said in Appending an image to itemList of QFormLayout widgets:

                                        part of the sql data retrieval code:

                                                               result = self.cursor.fetchall()             
                                                             for row_number, row_data in enumerate(result):             
                                                                 self.tab.insertRow(row_number)             
                                                                 for column_number, data in enumerate(row_data):             
                                                                     self.tab.setItem(row_number, column_number, QTableWidgetItem(str(data)))
                                        

                                        it runs but with an error message: "a bytes-like object is required, not 'str' "

                                        Could the self.tab.setItem(row_number, column_number, QtablewidgetItem(str(data))) be where the issue is coming from?

                                        self.passlbtx is a QLineEdit for appending the varbinary data retrieved and displayed in a column of the qtablewidget.

                                        @mrjj

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 16 Sept 2021, 06:10 last edited by
                                        #22

                                        Hi

                                        Well the
                                        QTableWidgetItem(str(data))
                                        part tried to make str out of data but it complains about the reverse
                                        "a bytes-like object is required, not 'str' "
                                        so I think it must be related to when you do the other way.

                                        I think maybe from here
                                        imgcon = bytes(self.passlbbtx.text(), 'utf-8')

                                        as QLineEdit only contains QString not Qbytearry or the binary stream you read from database.

                                        Would it not be possible to directly read it into the image/pixmap and not via a QLineEdit ?

                                        C 4 Replies Last reply 16 Sept 2021, 07:42
                                        0
                                        • M mrjj
                                          16 Sept 2021, 06:10

                                          Hi

                                          Well the
                                          QTableWidgetItem(str(data))
                                          part tried to make str out of data but it complains about the reverse
                                          "a bytes-like object is required, not 'str' "
                                          so I think it must be related to when you do the other way.

                                          I think maybe from here
                                          imgcon = bytes(self.passlbbtx.text(), 'utf-8')

                                          as QLineEdit only contains QString not Qbytearry or the binary stream you read from database.

                                          Would it not be possible to directly read it into the image/pixmap and not via a QLineEdit ?

                                          C Offline
                                          C Offline
                                          CEO.
                                          wrote on 16 Sept 2021, 07:42 last edited by
                                          #23

                                          @mrjj please kindly see this thread:

                                          https://github.com/python-pillow/Pillow/issues/5712

                                          1 Reply Last reply
                                          0

                                          13/51

                                          15 Sept 2021, 14:39

                                          38 unread
                                          • Login

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