简体   繁体   中英

Display images from sqlite3 database to a column in QTableWidget with Python

After looking at various examples here and elswhere, I have not been able to show images in 'mydb.db' which is my SQlite3 database to my table QTableWidget in python. Here is my code

def fetch_all_learner_data(self):
    connection= sqlite3.connect("mydb.db")
    query = "SELECT NAME,ADM,CLASS,STREAM,CATEGORY,GENDER,COUNTY,PARENT,PARENT_CONTACT,PHOTO   FROM learner_data"
    result = connection.execute(query)
    self.classes_table.setRowCount(0)
    for row_number,row_data in enumerate(result):
        self.classes_table.insertRow(row_number)
        for column_number,data in enumerate(row_data):
            self.classes_table.setItem(row_number,column_number,QTableWidgetItem(str(data)))
    connection.commit()
    connection.close()

This is what end up getting on the image column in the qtable and which have also really slowed my system.

qtable 中显示的内容

How can I solve this?

You have to build a QIcon based on the data using a QPixmap:

def fetch_all_learner_data(self):
    connection = sqlite3.connect("mydb.db")
    query = "SELECT NAME,ADM,CLASS,STREAM,CATEGORY,GENDER,COUNTY,PARENT,PARENT_CONTACT,PHOTO   FROM learner_data"
    result = connection.execute(query)
    self.classes_table.setRowCount(0)
    for row_number, row_data in enumerate(result):
        self.classes_table.insertRow(row_number)
        for column_number, data in enumerate(row_data):
            it = QTableWidgetItem()
            if column_number == 9:
                pixmap = QPixmap()
                pixmap.loadFromData(data)
                it.setIcon(QIcon(pixmap))
            else:
                it.setText(str(data))
            self.classes_table.setItem(row_number, column_number, it)
    connection.commit()
    connection.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM