Displaying images in QTableWidget from SQLite using PyQt

Posted by


PyQt is a set of Python bindings for the Qt application framework developed by Riverbank Computing. It allows developers to create cross-platform applications with a native look and feel using the Qt toolkit.

In this tutorial, we will learn how to display an image stored in an SQLite database inside a QTableWidget using PyQt. We will first create a database and insert an image into it. Then, we will fetch the image from the database and display it in the QTableWidget.

Step 1: Set up the environment
Before we start, make sure you have PyQt installed on your system. You can install PyQt using pip:

pip install PyQt5

Step 2: Create a SQLite database
In this step, we will create a SQLite database and insert an image into it. Here is a simple script that creates a database called images.db and inserts an image named image.png into it:

import sqlite3

# Create a connection to the database
conn = sqlite3.connect('images.db')
c = conn.cursor()

# Create a table to store images
c.execute('''CREATE TABLE images (id INTEGER PRIMARY KEY, image BLOB)''')

# Read the image file
with open('image.png', 'rb') as f:
    image_data = f.read()

# Insert the image into the database
c.execute('''INSERT INTO images (image) VALUES (?)''', (sqlite3.Binary(image_data),))

# Commit the changes and close the connection
conn.commit()
conn.close()

Step 3: Display the image in QTableWidget
Now that we have inserted the image into the database, we can display it inside a QTableWidget. Here is a complete script that creates a main window with a QTableWidget and fetches the image from the database to display it:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem
from PyQt5.QtGui import QPixmap
import sqlite3
from io import BytesIO

class ImageTableWidget(QTableWidget):
    def __init__(self):
        super().__init__(1, 1)
        self.setHorizontalHeaderLabels(['Image'])
        self.setRowCount(1)
        self.setColumnCount(1)
        self.setShowGrid(False)

        self.populate_table()

    def populate_table(self):
        conn = sqlite3.connect('images.db')
        c = conn.cursor()

        c.execute('''SELECT image FROM images WHERE id=?''', (1,))
        image_data = c.fetchone()[0]

        pixmap = QPixmap()
        pixmap.loadFromData(image_data)

        item = QTableWidgetItem()
        item.setData(0, pixmap)

        self.setItem(0, 0, item)

        conn.close()

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        table_widget = ImageTableWidget()

        self.setCentralWidget(table_widget)
        self.setGeometry(100, 100, 400, 400)
        self.setWindowTitle('Display Image Inside QTableWidget from SQLite')

app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())

In this script, we create a custom QTableWidget subclass called ImageTableWidget that fetches the image from the database and displays it in the table. The MainWindow class creates the main window and sets the ImageTableWidget as the central widget.

Run the script, and you should see an image displayed inside the QTableWidget. This is how you can display an image stored in an SQLite database inside a QTableWidget using PyQt. Feel free to customize the script according to your needs.

0 0 votes
Article Rating

Leave a Reply

13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@toto2321
3 hours ago

thank you so much man

@melihcanakbulut3040
3 hours ago

Thank you. This video really was helpful

@atCao612
3 hours ago

Thank you!!!!

@aliabrar1388
3 hours ago

how to take print preview of this table…!

@hauto1367
3 hours ago

thank you very much

@MJ-tn8tw
3 hours ago

Hello bro source code bro pls

@БогданДзейтов-ъ4з
3 hours ago

help please

@Slm3lkm
3 hours ago

clicked on the link then found a bug in the filesharing service ended up browsing admin pannels, next time use GitHub or pastebin ….

@sravanchittupalli5810
3 hours ago

Thank you…Helpful video

@rendermanpro
3 hours ago

I downloaded it, but it was not trivial, why not upload it to github or google drive, that file sharing service is so shitty…. Thanks for sharing tutorial.

@Johannes_DC
3 hours ago

Please add subtitles…

@klingerandrealmeida5238
3 hours ago

Congratulations on the video. I can't download the file, just open several tabs in the browser.

@asurya
3 hours ago

Hi Sir, I would like to speak with you about QT. So could you please share your email id. Or you can share your contact details to this address: asurya676@gmail.com

13
0
Would love your thoughts, please comment.x
()
x