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. Retrieving database records and displaying in Qtablewidget (Python)
Forum Updated to NodeBB v4.3 + New Features

Retrieving database records and displaying in Qtablewidget (Python)

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 1 Posters 691 Views
  • 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 Offline
    C Offline
    CEO.
    wrote on 16 Sept 2021, 20:52 last edited by CEO.
    #1

    Hello,

    Although I have recently posted something similar to this but owing to the fact that we are yet to resolve it after applying different approaches, I decided to try another method in order to achieve my aim to an extent.

    Now, as usual, there's a table in a database with 4 columns:
    fname varchar(50)
    photopath varchar(500),
    imagedata varbinary(max),
    dateRecorded datetime DEFAULT getdate ()

    The image is in column 2

    Records are stored in the database. These records get displayed in a Qtablewidget of my application. While other data get displayed in their respective column of the qtablewidget, I would want the imagedata to display as an image in its column of the qtablewidget.

    The lines of code below is for the retrieval function.
    NOTE: If you've followed my previous post, be informed that this is an entirely different approach and I will appreciate you kindly study the conditional statement inside. Thank you.

    For now, the records are being retrieved into the qtablewidget but the image is not showing.

    def retrieve1(self):
        try:
            self.db_connection()
            schbox = searchtxtab.text()  # to make the searchbox active, ensure it is coded close to the db connection.
            self.cursor.execute(
                "SELECT * FROM pixxdate WHERE fname LIKE '%{}%' OR photopath LIKE '%{}%' OR imagedata LIKE '%{}%' OR dateRecorded LIKE '%{}%' "
                .format(schbox, schbox, schbox, schbox))
            result = self.cursor.fetchall()
            print(result)
    
            for row_number, row_data in enumerate(result):
                self.tab.insertRow(row_number)
                for column_number, column_data in enumerate(row_data):
                    item = str(column_data)
                    if (column_number == 2):
                        item = self.getImageLabel(column_data)
                        self.tab.setCellWidget(row_number, column_number,item)
                    else:
                        self.tab.setItem(row_number, column_number, QTableWidgetItem(str(column_data)))
    
            print(self.cursor.rowcount, "row(s) affected")
            self.conn.commit()
            msgBox = QMessageBox()
            msgBox.setIcon(QMessageBox.Information)
            msgBox.setText("DATA RETRIEVED")
            msgBox.setWindowTitle("MESSAGE FROM DB")
            msgBox.exec_()
    
        except Exception as err:
            print(err)
            self.conn.close()
            dlg = QDialog(self)
            dlg.setWindowTitle("ALERT!")
            dlg.setGeometry(100, 100, 200, 200)
            dlg.move(300, 300)
            dlg.setGeometry(100, 100, 200, 100)
            bb = QPushButton("ERROR", dlg)
            bb.move(30, 50)
            dlg.exec_()
    
        self.show()
    
    def getImageLabel(self):
        try:
            img = QLabel("Image")
            imglabel = QLabel(" ")
            imglabel.setScaledContents(True)
            pixmap = QPixmap()
            pixmap.loadFromData(img,'jpg')
            imglabel.setPixmap(pixmap)
            return imglabel
    
        except Exception as err:
            print(err)
    
    C 1 Reply Last reply 16 Sept 2021, 20:53
    0
    • C CEO.
      16 Sept 2021, 20:52

      Hello,

      Although I have recently posted something similar to this but owing to the fact that we are yet to resolve it after applying different approaches, I decided to try another method in order to achieve my aim to an extent.

      Now, as usual, there's a table in a database with 4 columns:
      fname varchar(50)
      photopath varchar(500),
      imagedata varbinary(max),
      dateRecorded datetime DEFAULT getdate ()

      The image is in column 2

      Records are stored in the database. These records get displayed in a Qtablewidget of my application. While other data get displayed in their respective column of the qtablewidget, I would want the imagedata to display as an image in its column of the qtablewidget.

      The lines of code below is for the retrieval function.
      NOTE: If you've followed my previous post, be informed that this is an entirely different approach and I will appreciate you kindly study the conditional statement inside. Thank you.

      For now, the records are being retrieved into the qtablewidget but the image is not showing.

      def retrieve1(self):
          try:
              self.db_connection()
              schbox = searchtxtab.text()  # to make the searchbox active, ensure it is coded close to the db connection.
              self.cursor.execute(
                  "SELECT * FROM pixxdate WHERE fname LIKE '%{}%' OR photopath LIKE '%{}%' OR imagedata LIKE '%{}%' OR dateRecorded LIKE '%{}%' "
                  .format(schbox, schbox, schbox, schbox))
              result = self.cursor.fetchall()
              print(result)
      
              for row_number, row_data in enumerate(result):
                  self.tab.insertRow(row_number)
                  for column_number, column_data in enumerate(row_data):
                      item = str(column_data)
                      if (column_number == 2):
                          item = self.getImageLabel(column_data)
                          self.tab.setCellWidget(row_number, column_number,item)
                      else:
                          self.tab.setItem(row_number, column_number, QTableWidgetItem(str(column_data)))
      
              print(self.cursor.rowcount, "row(s) affected")
              self.conn.commit()
              msgBox = QMessageBox()
              msgBox.setIcon(QMessageBox.Information)
              msgBox.setText("DATA RETRIEVED")
              msgBox.setWindowTitle("MESSAGE FROM DB")
              msgBox.exec_()
      
          except Exception as err:
              print(err)
              self.conn.close()
              dlg = QDialog(self)
              dlg.setWindowTitle("ALERT!")
              dlg.setGeometry(100, 100, 200, 200)
              dlg.move(300, 300)
              dlg.setGeometry(100, 100, 200, 100)
              bb = QPushButton("ERROR", dlg)
              bb.move(30, 50)
              dlg.exec_()
      
          self.show()
      
      def getImageLabel(self):
          try:
              img = QLabel("Image")
              imglabel = QLabel(" ")
              imglabel.setScaledContents(True)
              pixmap = QPixmap()
              pixmap.loadFromData(img,'jpg')
              imglabel.setPixmap(pixmap)
              return imglabel
      
          except Exception as err:
              print(err)
      
      C Offline
      C Offline
      CEO.
      wrote on 16 Sept 2021, 20:53 last edited by
      #2

      @mrjj attention please.
      This time around, I am trying to make the picture display inside the qtablewidget column. Kindly study the code

      C 1 Reply Last reply 16 Sept 2021, 21:02
      0
      • C CEO.
        16 Sept 2021, 20:53

        @mrjj attention please.
        This time around, I am trying to make the picture display inside the qtablewidget column. Kindly study the code

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

        Hey!
        I've fixed it just now.

        I discovered the images were in png format, I used jpg there in the getImageLabel method there. I changed it to png and it worked!!

        C 1 Reply Last reply 16 Sept 2021, 21:48
        0
        • C CEO.
          16 Sept 2021, 21:02

          Hey!
          I've fixed it just now.

          I discovered the images were in png format, I used jpg there in the getImageLabel method there. I changed it to png and it worked!!

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

          @mrjj @SGaist @jsulm

          Having been able to achieve some steps, can you help me here:

             def appendSelection(self):
                     try:
                         rows = {index.row() for index in self.tab.selectionModel().selectedIndexes()}
                         lines = []
                         itemList = []
             
                         itemList.append(self.fname)
                         itemList.append(self.pixpath)
             
                         itemList.append(self.passlb)
             itemList.append(self.datetx)
             
                        
             
                         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 == 0):
                                     itemList[0].setText(text)
                                 if (col == 1):
                                     itemList[1].setText(text)
                                 if (col == 2):
                                     itemList[2].setText(text)
                                
             
                                 if (col == 3):
                                  itemList[3].setText(text)
          

          Column 2 is the image column. I want to append it to a qpixmap inside a formlayout. The qpixmap in the formlayout is self.passlb. it is a QLabel.

          So how do I append it just like other data (text) get appended to form qLineEdits.

          I tried:
          if (col == 2):
          itemList[2].setPixmap()

          But it didn't work.
          Kindly help with the right line of code.

          1 Reply Last reply
          0

          1/4

          16 Sept 2021, 20:52

          • Login

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