I'm making a clipboard that can save multiple items. My clipboard using QClipboard
can store text, file paths and images. When you copy something it is shown in a QTableWidget
but there is a problem with images. I want to show them in small size as an icon in a QTableWidgetItem
, so my code converts the QImage
to a QPixmap
to a QIcon
and places it in a QTableWidgetItem
; however the cell is shown empty.
I can't figure out where the problem lies but maybe someone can spot it. The Placement in the table is executed by the "ToTable"-function.
class mainUI(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = uic.loadUi("MCInterface.ui", self)
self.qcb = QtWidgets.QApplication.clipboard()
self.clipboard = []
self.qcb.dataChanged.connect(self.GetClipboard)
def GetClipboard(self):
spinbox = self.ui.SB_ContainerLen
clen = spinbox.value()
if not self.ui.TB_Pause.isChecked():
if self.qcb.text():
data = self.qcb.text()
elif self.qcb.image():
data = (self.qcb.image())
if data not in self.clipboard and len(self.clipboard) < clen:
if isinstance(data, str) and data.startswith("file:///") and "\n" in data:
data = data.splitlines()
for d in data:
if len(self.clipboard) < clen:
self.clipboard.append(d)
self.ToTable(d)
else:
self.clipboard.append(data)
self.ToTable(data)
print(self.clipboard)
def addRow(self):
row = self.ui.TW_Clipboard.rowCount()
self.ui.TW_Clipboard.insertRow(row)
return row
def ToTable(self, data):
table = self.ui.TW_Clipboard
if isinstance(data, QImage):
row = self.addRow()
pixmap = QPixmap()
pixmap.fromImage(data)
icon = QIcon()
icon.addPixmap(pixmap)
xItem = QtWidgets.QTableWidgetItem()
xItem.setIcon(icon)
yItem = QtWidgets.QTableWidgetItem("Bild")
table.setItem(row, 0, xItem)
table.setItem(row, 1, yItem)
elif isinstance(data, str) and data.startswith("file:///"):
row = self.addRow()
table.setItem(row, 0, QtWidgets.QTableWidgetItem(data[8:]))
table.setItem(row, 1, QtWidgets.QTableWidgetItem("Datei"))
else:
row = self.addRow()
table.setItem(row, 0, QtWidgets.QTableWidgetItem(data))
table.setItem(row, 1, QtWidgets.QTableWidgetItem("Text"))
table.setRowHeight(row, 50)
fromImage()
is a static function that returns a QPixmap. Since you only executed the function without referencing its result, the pixmap
is still the empty pixmap you created with pixmap = QPixmap()
.
Change to the following:
def ToTable(self, data):
table = self.ui.TW_Clipboard
if isinstance(data, QtGui.QImage):
row = self.addRow()
pixmap = QtGui.QPixmap.fromImage(data)
# ...
Note that if you want to keep track of copied paths, you should use the mime data urls()
instead of converting them from text: copying an actual path is not the same of copying the string of that path.