将 blob 图像从 sqlite db 插入到 QPixMap 到 PyQt5 中的 QTextBrowser
Insert blob image into QPixMap from sqlite db to QTextBrowser in PyQt5
我无法将 qpixmap 图像插入带有 html 标签的 qtextbrowser。 (我需要使用 html 而不是其他方法来插入)
我尝试了下面的代码。
def readImage(self):
cur = self.db.cursor()
covers = cur.execute("select cover from covers")
pm = QPixmap()
for cover in covers:
pm.loadFromData(cover[0])
self.ui.textBrowser.setHtml("<img src=pm /img>")
结果只是在qtextbrowser中出现了一个小图标,并没有显示实际图像。
请不要给我一个 qt c++ 文档页面,我无法理解那些 c++ 的东西。
没有必要将数据转换为 QPixmap,如果 QtSql 将 return 一个可以将其编码为 base64 的 QByteArray,从而可以将图像添加到 QTextEdit:
from PyQt5 import QtCore, QtGui, QtWidgets, QtSql
def create_connection(path=":memory:"):
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(path)
if not db.open():
QtWidgets.QMessageBox.critical(
None,
QtWidgets.qApp.tr("Cannot open database"),
QtWidgets.qApp.tr(
"Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information "
"how to build it.\n\n"
"Click Cancel to exit."
),
QtWidgets.QMessageBox.Cancel,
)
return False
return True
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.text_edit = QtWidgets.QTextEdit()
self.setCentralWidget(self.text_edit)
query = QtSql.QSqlQuery("SELECT cover FROM covers")
while query.next():
ba = query.value(0)
base64 = ba.toBase64().data().decode()
self.text_edit.insertHtml(
"""<img height="200" width="200" src="data:image/png;base64,{}"/>""".format(base64)
)
self.resize(640, 480)
if __name__ == "__main__":
import os
import sys
app = QtWidgets.QApplication(sys.argv)
current_dir = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(current_dir, "rap.sqlite")
if not create_connection(path):
sys.exit(-1)
w = MainWindow()
w.show()
sys.exit(app.exec_())
如果您想继续使用 sqlite3,那么您必须将字节转换为 QByteArray 并应用相同的逻辑:
import os
import sqlite3
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.text_edit = QtWidgets.QTextEdit()
self.setCentralWidget(self.text_edit)
current_dir = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(current_dir, "rap.sqlite")
conn = sqlite3.connect(path)
c = conn.cursor()
covers = c.execute("SELECT cover FROM covers")
for cover in covers:
data, *_ = cover
ba = QtCore.QByteArray(data)
base64 = ba.toBase64().data().decode()
self.text_edit.insertHtml(
"""<img height="200" width="200" src="data:image/png;base64,{}"/>""".format(base64)
)
self.resize(640, 480)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
我无法将 qpixmap 图像插入带有 html 标签的 qtextbrowser。 (我需要使用 html 而不是其他方法来插入)
我尝试了下面的代码。
def readImage(self):
cur = self.db.cursor()
covers = cur.execute("select cover from covers")
pm = QPixmap()
for cover in covers:
pm.loadFromData(cover[0])
self.ui.textBrowser.setHtml("<img src=pm /img>")
结果只是在qtextbrowser中出现了一个小图标,并没有显示实际图像。 请不要给我一个 qt c++ 文档页面,我无法理解那些 c++ 的东西。
没有必要将数据转换为 QPixmap,如果 QtSql 将 return 一个可以将其编码为 base64 的 QByteArray,从而可以将图像添加到 QTextEdit:
from PyQt5 import QtCore, QtGui, QtWidgets, QtSql
def create_connection(path=":memory:"):
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(path)
if not db.open():
QtWidgets.QMessageBox.critical(
None,
QtWidgets.qApp.tr("Cannot open database"),
QtWidgets.qApp.tr(
"Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information "
"how to build it.\n\n"
"Click Cancel to exit."
),
QtWidgets.QMessageBox.Cancel,
)
return False
return True
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.text_edit = QtWidgets.QTextEdit()
self.setCentralWidget(self.text_edit)
query = QtSql.QSqlQuery("SELECT cover FROM covers")
while query.next():
ba = query.value(0)
base64 = ba.toBase64().data().decode()
self.text_edit.insertHtml(
"""<img height="200" width="200" src="data:image/png;base64,{}"/>""".format(base64)
)
self.resize(640, 480)
if __name__ == "__main__":
import os
import sys
app = QtWidgets.QApplication(sys.argv)
current_dir = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(current_dir, "rap.sqlite")
if not create_connection(path):
sys.exit(-1)
w = MainWindow()
w.show()
sys.exit(app.exec_())
如果您想继续使用 sqlite3,那么您必须将字节转换为 QByteArray 并应用相同的逻辑:
import os
import sqlite3
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.text_edit = QtWidgets.QTextEdit()
self.setCentralWidget(self.text_edit)
current_dir = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(current_dir, "rap.sqlite")
conn = sqlite3.connect(path)
c = conn.cursor()
covers = c.execute("SELECT cover FROM covers")
for cover in covers:
data, *_ = cover
ba = QtCore.QByteArray(data)
base64 = ba.toBase64().data().decode()
self.text_edit.insertHtml(
"""<img height="200" width="200" src="data:image/png;base64,{}"/>""".format(base64)
)
self.resize(640, 480)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())