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 5.9k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #33

    Hi

    so in your opinion the line
    self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
    should make sure the binary stream of the image, is kept safe as we don't call str on it ?

    In c++ this would not work, but I don't know if python can do something magic.

    If you can't get it to function, then you could try storing the image stream in a user role. its kept as QVariant so might work.

    item = QTableWidgetItem("the text it should show")
    MyRole = QtCore.Qt.UserRole + 2
    item.setData(MyRole, data ) // data should be the real stream.

    then when you click on a row and want the image
    grab the item via one of the functions that directly gives it
    https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidget.itemChanged

    and get the data back as
    data = item.data(MyRole)
    imgstrm = io.BytesIO(data)
    imgfile = Image.open(imgstrm)

    C 4 Replies Last reply
    1
    • mrjjM mrjj

      Hi

      so in your opinion the line
      self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
      should make sure the binary stream of the image, is kept safe as we don't call str on it ?

      In c++ this would not work, but I don't know if python can do something magic.

      If you can't get it to function, then you could try storing the image stream in a user role. its kept as QVariant so might work.

      item = QTableWidgetItem("the text it should show")
      MyRole = QtCore.Qt.UserRole + 2
      item.setData(MyRole, data ) // data should be the real stream.

      then when you click on a row and want the image
      grab the item via one of the functions that directly gives it
      https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidget.itemChanged

      and get the data back as
      data = item.data(MyRole)
      imgstrm = io.BytesIO(data)
      imgfile = Image.open(imgstrm)

      C Offline
      C Offline
      CEO.
      wrote on last edited by
      #34

      @mrjj thanks for your follow-up, I discovered it made no difference. I'm talking about this line:

            self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
      

      The QTableWidgetItem is already in string, so whatever I print out from there would be in string. I tried it before.

      1 Reply Last reply
      0
      • mrjjM mrjj

        Hi

        so in your opinion the line
        self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
        should make sure the binary stream of the image, is kept safe as we don't call str on it ?

        In c++ this would not work, but I don't know if python can do something magic.

        If you can't get it to function, then you could try storing the image stream in a user role. its kept as QVariant so might work.

        item = QTableWidgetItem("the text it should show")
        MyRole = QtCore.Qt.UserRole + 2
        item.setData(MyRole, data ) // data should be the real stream.

        then when you click on a row and want the image
        grab the item via one of the functions that directly gives it
        https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidget.itemChanged

        and get the data back as
        data = item.data(MyRole)
        imgstrm = io.BytesIO(data)
        imgfile = Image.open(imgstrm)

        C Offline
        C Offline
        CEO.
        wrote on last edited by
        #35

        @mrjj please have you done something like this before? If yes, I wouldn't mind having a study of it, I might get one or two ideas on how to manipulate it in Python.

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi

          so in your opinion the line
          self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
          should make sure the binary stream of the image, is kept safe as we don't call str on it ?

          In c++ this would not work, but I don't know if python can do something magic.

          If you can't get it to function, then you could try storing the image stream in a user role. its kept as QVariant so might work.

          item = QTableWidgetItem("the text it should show")
          MyRole = QtCore.Qt.UserRole + 2
          item.setData(MyRole, data ) // data should be the real stream.

          then when you click on a row and want the image
          grab the item via one of the functions that directly gives it
          https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidget.itemChanged

          and get the data back as
          data = item.data(MyRole)
          imgstrm = io.BytesIO(data)
          imgfile = Image.open(imgstrm)

          C Offline
          C Offline
          CEO.
          wrote on last edited by
          #36

          @mrjj can you think over this line:

                       self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 12 else str(data)))
          

          ...and how I can convert the data there to bytes right there?

          1 Reply Last reply
          0
          • mrjjM mrjj

            Hi

            so in your opinion the line
            self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
            should make sure the binary stream of the image, is kept safe as we don't call str on it ?

            In c++ this would not work, but I don't know if python can do something magic.

            If you can't get it to function, then you could try storing the image stream in a user role. its kept as QVariant so might work.

            item = QTableWidgetItem("the text it should show")
            MyRole = QtCore.Qt.UserRole + 2
            item.setData(MyRole, data ) // data should be the real stream.

            then when you click on a row and want the image
            grab the item via one of the functions that directly gives it
            https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidget.itemChanged

            and get the data back as
            data = item.data(MyRole)
            imgstrm = io.BytesIO(data)
            imgfile = Image.open(imgstrm)

            C Offline
            C Offline
            CEO.
            wrote on last edited by
            #37

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

            item = QTableWidgetItem("the text it should show")
            MyRole = QtCore.Qt.UserRole + 2
            item.setData(MyRole, data )

            Error message: 'QTableWidget' object is not callable

            1 Reply Last reply
            0
            • C Offline
              C Offline
              CEO.
              wrote on last edited by
              #38

              I have finally resolved the issue:

              I decided to make the retrieved data display in the qtablewidget with the image displaying inside the qtablewidget (as a png, or jpg file) as it is retrieved from the database, so the varbinary texts no longer displays in the qtablewidget.

              Then I use the .loadData() method in displaying it there. I was able to resolve the appendSelection function and get the displayed image of the qtablewidget to display also in the qlabel qpixmap reserved for it in the qformlayout. This resolved the appending issue for me:

                                                            if (col == 2) and itemArray:
                                            
                                                                img = itemArray.data(QTableWidgetItem.UserType)
                                            
                                                                pixmap = QPixmap()
                                            
                                                                pixmap.loadFromData(img, 'jpg')
                                            
                                                                itemList[2].setPixmap(pixmap)
              
              mrjjM 1 Reply Last reply
              1
              • C CEO.

                I have finally resolved the issue:

                I decided to make the retrieved data display in the qtablewidget with the image displaying inside the qtablewidget (as a png, or jpg file) as it is retrieved from the database, so the varbinary texts no longer displays in the qtablewidget.

                Then I use the .loadData() method in displaying it there. I was able to resolve the appendSelection function and get the displayed image of the qtablewidget to display also in the qlabel qpixmap reserved for it in the qformlayout. This resolved the appending issue for me:

                                                              if (col == 2) and itemArray:
                                              
                                                                  img = itemArray.data(QTableWidgetItem.UserType)
                                              
                                                                  pixmap = QPixmap()
                                              
                                                                  pixmap.loadFromData(img, 'jpg')
                                              
                                                                  itemList[2].setPixmap(pixmap)
                
                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #39

                @CEO

                Super.
                So it helped not to store it in Linedit first but in a user role and
                hence it was not broken and could convert to image ?

                C 1 Reply Last reply
                0
                • mrjjM mrjj

                  @CEO

                  Super.
                  So it helped not to store it in Linedit first but in a user role and
                  hence it was not broken and could convert to image ?

                  C Offline
                  C Offline
                  CEO.
                  wrote on last edited by
                  #40

                  @mrjj yes and I was so happy and exclaimed EUREKA! immediately it worked fine. I now understand the joy Archimedes felt when the idea of upthrust struck him while bathing to the extent he had to run out of his bathroom naked. lol

                  mrjjM 1 Reply Last reply
                  1
                  • C CEO.

                    @mrjj yes and I was so happy and exclaimed EUREKA! immediately it worked fine. I now understand the joy Archimedes felt when the idea of upthrust struck him while bathing to the extent he had to run out of his bathroom naked. lol

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #41

                    @CEO

                    hehe well good :)
                    thx for the feedback. I learn by knowing what was wrong so it was as i
                    guessed at. the binary stream didn't like to be in lineedit first.

                    C 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @CEO

                      hehe well good :)
                      thx for the feedback. I learn by knowing what was wrong so it was as i
                      guessed at. the binary stream didn't like to be in lineedit first.

                      C Offline
                      C Offline
                      CEO.
                      wrote on last edited by CEO.
                      #42

                      @mrjj

                      .

                      I wanna update the retrieved image which is displayed in jpg format. How do I go about it plz?

                      The image label (i.e the qpixmap label) is self.passlb

                       if (col == 2) and itemArray:
                                          img = itemArray.data(QTableWidgetItem.UserType)
                                          pixmap = QPixmap()
                                          pixmap.loadFromData(img, 'jpg')
                                          itemList[2].setPixmap((pixmap).scaled(250,250))
                      

                      So in my sql update statement, I tried:

                                   values(self.fullname.text(),
                                   self.passlb.pixmap()
                      ...)```
                      
                      It didn't work.
                      
                      Do you think it should be:
                      
                             img.pixmap() or img.text() or img.read()
                      Any clue about the method I should use in updating a qpixmap label?
                      mrjjM 1 Reply Last reply
                      0
                      • C CEO.

                        @mrjj

                        .

                        I wanna update the retrieved image which is displayed in jpg format. How do I go about it plz?

                        The image label (i.e the qpixmap label) is self.passlb

                         if (col == 2) and itemArray:
                                            img = itemArray.data(QTableWidgetItem.UserType)
                                            pixmap = QPixmap()
                                            pixmap.loadFromData(img, 'jpg')
                                            itemList[2].setPixmap((pixmap).scaled(250,250))
                        

                        So in my sql update statement, I tried:

                                     values(self.fullname.text(),
                                     self.passlb.pixmap()
                        ...)```
                        
                        It didn't work.
                        
                        Do you think it should be:
                        
                               img.pixmap() or img.text() or img.read()
                        Any clue about the method I should use in updating a qpixmap label?
                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #43

                        Hi
                        You mean save an actual pixmap back to the database ?`

                        But what is the type of field in the db and how did you get the other images in there ?

                        Its clear we need to convert it to a binary stream and we can use QBuffer for that

                        (c++, could not find python example)

                            QPixmap inPixmap =self.passlb.pixmap(); 
                            QByteArray inByteArray;
                            QBuffer inBuffer( &inByteArray );
                            inBuffer.open( QIODevice::WriteOnly );
                            inPixmap.save( &inBuffer, "PNG" ); // write inPixmap into inByteArray in PNG format
                        

                        inBuffer can now be saved in DB.

                        But I wonder if there is the reverse of io.BytesIO ? we should rather use ?

                        C 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          Hi
                          You mean save an actual pixmap back to the database ?`

                          But what is the type of field in the db and how did you get the other images in there ?

                          Its clear we need to convert it to a binary stream and we can use QBuffer for that

                          (c++, could not find python example)

                              QPixmap inPixmap =self.passlb.pixmap(); 
                              QByteArray inByteArray;
                              QBuffer inBuffer( &inByteArray );
                              inBuffer.open( QIODevice::WriteOnly );
                              inPixmap.save( &inBuffer, "PNG" ); // write inPixmap into inByteArray in PNG format
                          

                          inBuffer can now be saved in DB.

                          But I wonder if there is the reverse of io.BytesIO ? we should rather use ?

                          C Offline
                          C Offline
                          CEO.
                          wrote on last edited by CEO.
                          #44

                          @mrjj sorry,

                          See this function, it's for uploading images in the form for update:

                          def openfile(self):
                              try:
                                  fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)")
                                  self.passlb.setPixmap(QPixmap(fname).scaled(250, 250))
                                  self.img = open(fname, 'rb')
                                  self.psptpath.setText(fname)
                                  # self.pixL.setText(strfn)
                                  print(self.pixlb.setText(''))
                          

                          This function is for appending the jpg to the qlabel self.passlb from the qtable after it's been retrieved from the database:

                                 if (col ==2):
                                 img = itemArray.data(QTableWidgetItem.UserType)
                                                         pixmap = QPixmap()
                                                         pixmap.loadFromData(img, 'jpg')
                                                         itemList[2].setPixmap((pixmap).scaled(250,250))
                          

                          Then here is my sql statement updating the image:
                          (The usual update statement is here)
                          values (self.fullname.text(),
                          self.img.read()
                          )

                          This is saying my program objects has no attribute 'img'

                          mrjjM C 3 Replies Last reply
                          0
                          • C CEO.

                            @mrjj sorry,

                            See this function, it's for uploading images in the form for update:

                            def openfile(self):
                                try:
                                    fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)")
                                    self.passlb.setPixmap(QPixmap(fname).scaled(250, 250))
                                    self.img = open(fname, 'rb')
                                    self.psptpath.setText(fname)
                                    # self.pixL.setText(strfn)
                                    print(self.pixlb.setText(''))
                            

                            This function is for appending the jpg to the qlabel self.passlb from the qtable after it's been retrieved from the database:

                                   if (col ==2):
                                   img = itemArray.data(QTableWidgetItem.UserType)
                                                           pixmap = QPixmap()
                                                           pixmap.loadFromData(img, 'jpg')
                                                           itemList[2].setPixmap((pixmap).scaled(250,250))
                            

                            Then here is my sql statement updating the image:
                            (The usual update statement is here)
                            values (self.fullname.text(),
                            self.img.read()
                            )

                            This is saying my program objects has no attribute 'img'

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #45

                            @CEO
                            ah well its better with such logical bug than something that just don't work :)

                            C 1 Reply Last reply
                            0
                            • C CEO.

                              @mrjj sorry,

                              See this function, it's for uploading images in the form for update:

                              def openfile(self):
                                  try:
                                      fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)")
                                      self.passlb.setPixmap(QPixmap(fname).scaled(250, 250))
                                      self.img = open(fname, 'rb')
                                      self.psptpath.setText(fname)
                                      # self.pixL.setText(strfn)
                                      print(self.pixlb.setText(''))
                              

                              This function is for appending the jpg to the qlabel self.passlb from the qtable after it's been retrieved from the database:

                                     if (col ==2):
                                     img = itemArray.data(QTableWidgetItem.UserType)
                                                             pixmap = QPixmap()
                                                             pixmap.loadFromData(img, 'jpg')
                                                             itemList[2].setPixmap((pixmap).scaled(250,250))
                              

                              Then here is my sql statement updating the image:
                              (The usual update statement is here)
                              values (self.fullname.text(),
                              self.img.read()
                              )

                              This is saying my program objects has no attribute 'img'

                              C Offline
                              C Offline
                              CEO.
                              wrote on last edited by
                              #46

                              @mrjj please see this.

                              1 Reply Last reply
                              0
                              • C CEO.

                                @mrjj sorry,

                                See this function, it's for uploading images in the form for update:

                                def openfile(self):
                                    try:
                                        fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)")
                                        self.passlb.setPixmap(QPixmap(fname).scaled(250, 250))
                                        self.img = open(fname, 'rb')
                                        self.psptpath.setText(fname)
                                        # self.pixL.setText(strfn)
                                        print(self.pixlb.setText(''))
                                

                                This function is for appending the jpg to the qlabel self.passlb from the qtable after it's been retrieved from the database:

                                       if (col ==2):
                                       img = itemArray.data(QTableWidgetItem.UserType)
                                                               pixmap = QPixmap()
                                                               pixmap.loadFromData(img, 'jpg')
                                                               itemList[2].setPixmap((pixmap).scaled(250,250))
                                

                                Then here is my sql statement updating the image:
                                (The usual update statement is here)
                                values (self.fullname.text(),
                                self.img.read()
                                )

                                This is saying my program objects has no attribute 'img'

                                C Offline
                                C Offline
                                CEO.
                                wrote on last edited by
                                #47

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

                                @mrjj sorry,

                                See this function, it's for uploading images in the form for update:

                                def openfile(self):
                                    try:
                                        fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)")
                                        self.passlb.setPixmap(QPixmap(fname).scaled(250, 250))
                                        self.img = open(fname, 'rb')
                                        self.psptpath.setText(fname)
                                        # self.pixL.setText(strfn)
                                        print(self.pixlb.setText(''))
                                

                                This function is for appending the jpg to the qlabel self.passlb from the qtable after it's been retrieved from the database:

                                       if (col ==2):
                                       img = itemArray.data(QTableWidgetItem.UserType)
                                                               pixmap = QPixmap()
                                                               pixmap.loadFromData(img, 'jpg')
                                                               itemList[2].setPixmap((pixmap).scaled(250,250))
                                

                                Then here is my sql statement updating the image:
                                (The usual update statement is here)
                                values (self.fullname.text(),
                                self.img.read()
                                )

                                This is saying my program objects has no attribute 'img'

                                @mrjj

                                1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @CEO
                                  ah well its better with such logical bug than something that just don't work :)

                                  C Offline
                                  C Offline
                                  CEO.
                                  wrote on last edited by
                                  #48

                                  @mrjj I did some testing and discovered it works fine only when I update the photo. Updating any other column without updating the image (photo) produces an error saying no object found for the image column.

                                  mrjjM 1 Reply Last reply
                                  0
                                  • C CEO.

                                    @mrjj I did some testing and discovered it works fine only when I update the photo. Updating any other column without updating the image (photo) produces an error saying no object found for the image column.

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #49

                                    @CEO
                                    But does it says that self.img don't exists ?

                                    but in def openfile(self):

                                    self.img = open(fname, 'rb')

                                    works

                                    so is the function that uses the SQL, also part of the same class ?

                                    C 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @CEO
                                      But does it says that self.img don't exists ?

                                      but in def openfile(self):

                                      self.img = open(fname, 'rb')

                                      works

                                      so is the function that uses the SQL, also part of the same class ?

                                      C Offline
                                      C Offline
                                      CEO.
                                      wrote on last edited by
                                      #50

                                      @mrjj the function that uses the SQL is part of the class. It works fine when in my update I upload a photo but comes up with that error "object has no attribute 'img' " if I don't upload a photo (i.e when I leave the photo retrieved from the database). Meaning even if I upload the same photo there, it will work. It just wants the upload to be done in that pixmap field

                                      mrjjM 1 Reply Last reply
                                      0
                                      • C CEO.

                                        @mrjj the function that uses the SQL is part of the class. It works fine when in my update I upload a photo but comes up with that error "object has no attribute 'img' " if I don't upload a photo (i.e when I leave the photo retrieved from the database). Meaning even if I upload the same photo there, it will work. It just wants the upload to be done in that pixmap field

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #51

                                        So when loading from db, you need to both set the UserRole and the pixmap field
                                        it sounds like even the error "object has no attribute 'img' " is very confusing
                                        if it is indeed a class member.

                                        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