Appending an image to itemList of QFormLayout widgets
-
Hi
so in your opinion the line
self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
should make sure the binary stream of the image, is kept safe as we don't call str on it ?In c++ this would not work, but I don't know if python can do something magic.
If you can't get it to function, then you could try storing the image stream in a user role. its kept as QVariant so might work.
item = QTableWidgetItem("the text it should show")
MyRole = QtCore.Qt.UserRole + 2
item.setData(MyRole, data ) // data should be the real stream.then when you click on a row and want the image
grab the item via one of the functions that directly gives it
https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidget.itemChangedand get the data back as
data = item.data(MyRole)
imgstrm = io.BytesIO(data)
imgfile = Image.open(imgstrm) -
Hi
so in your opinion the line
self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
should make sure the binary stream of the image, is kept safe as we don't call str on it ?In c++ this would not work, but I don't know if python can do something magic.
If you can't get it to function, then you could try storing the image stream in a user role. its kept as QVariant so might work.
item = QTableWidgetItem("the text it should show")
MyRole = QtCore.Qt.UserRole + 2
item.setData(MyRole, data ) // data should be the real stream.then when you click on a row and want the image
grab the item via one of the functions that directly gives it
https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidget.itemChangedand get the data back as
data = item.data(MyRole)
imgstrm = io.BytesIO(data)
imgfile = Image.open(imgstrm)@mrjj thanks for your follow-up, I discovered it made no difference. I'm talking about this line:
self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
The QTableWidgetItem is already in string, so whatever I print out from there would be in string. I tried it before.
-
Hi
so in your opinion the line
self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
should make sure the binary stream of the image, is kept safe as we don't call str on it ?In c++ this would not work, but I don't know if python can do something magic.
If you can't get it to function, then you could try storing the image stream in a user role. its kept as QVariant so might work.
item = QTableWidgetItem("the text it should show")
MyRole = QtCore.Qt.UserRole + 2
item.setData(MyRole, data ) // data should be the real stream.then when you click on a row and want the image
grab the item via one of the functions that directly gives it
https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidget.itemChangedand get the data back as
data = item.data(MyRole)
imgstrm = io.BytesIO(data)
imgfile = Image.open(imgstrm) -
Hi
so in your opinion the line
self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
should make sure the binary stream of the image, is kept safe as we don't call str on it ?In c++ this would not work, but I don't know if python can do something magic.
If you can't get it to function, then you could try storing the image stream in a user role. its kept as QVariant so might work.
item = QTableWidgetItem("the text it should show")
MyRole = QtCore.Qt.UserRole + 2
item.setData(MyRole, data ) // data should be the real stream.then when you click on a row and want the image
grab the item via one of the functions that directly gives it
https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidget.itemChangedand get the data back as
data = item.data(MyRole)
imgstrm = io.BytesIO(data)
imgfile = Image.open(imgstrm) -
Hi
so in your opinion the line
self.tab.setItem(row_number, column_number,QTableWidgetItem(data if column_number == 3 else str(data)))
should make sure the binary stream of the image, is kept safe as we don't call str on it ?In c++ this would not work, but I don't know if python can do something magic.
If you can't get it to function, then you could try storing the image stream in a user role. its kept as QVariant so might work.
item = QTableWidgetItem("the text it should show")
MyRole = QtCore.Qt.UserRole + 2
item.setData(MyRole, data ) // data should be the real stream.then when you click on a row and want the image
grab the item via one of the functions that directly gives it
https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QTableWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QTableWidget.itemChangedand get the data back as
data = item.data(MyRole)
imgstrm = io.BytesIO(data)
imgfile = Image.open(imgstrm) -
I have finally resolved the issue:
I decided to make the retrieved data display in the qtablewidget with the image displaying inside the qtablewidget (as a png, or jpg file) as it is retrieved from the database, so the varbinary texts no longer displays in the qtablewidget.
Then I use the .loadData() method in displaying it there. I was able to resolve the appendSelection function and get the displayed image of the qtablewidget to display also in the qlabel qpixmap reserved for it in the qformlayout. This resolved the appending issue for me:
if (col == 2) and itemArray: img = itemArray.data(QTableWidgetItem.UserType) pixmap = QPixmap() pixmap.loadFromData(img, 'jpg') itemList[2].setPixmap(pixmap)
-
I have finally resolved the issue:
I decided to make the retrieved data display in the qtablewidget with the image displaying inside the qtablewidget (as a png, or jpg file) as it is retrieved from the database, so the varbinary texts no longer displays in the qtablewidget.
Then I use the .loadData() method in displaying it there. I was able to resolve the appendSelection function and get the displayed image of the qtablewidget to display also in the qlabel qpixmap reserved for it in the qformlayout. This resolved the appending issue for me:
if (col == 2) and itemArray: img = itemArray.data(QTableWidgetItem.UserType) pixmap = QPixmap() pixmap.loadFromData(img, 'jpg') itemList[2].setPixmap(pixmap)
@CEO
Super.
So it helped not to store it in Linedit first but in a user role and
hence it was not broken and could convert to image ? -
@CEO
Super.
So it helped not to store it in Linedit first but in a user role and
hence it was not broken and could convert to image ? -
@mrjj yes and I was so happy and exclaimed EUREKA! immediately it worked fine. I now understand the joy Archimedes felt when the idea of upthrust struck him while bathing to the extent he had to run out of his bathroom naked. lol
@CEO
hehe well good :)
thx for the feedback. I learn by knowing what was wrong so it was as i
guessed at. the binary stream didn't like to be in lineedit first. -
@CEO
hehe well good :)
thx for the feedback. I learn by knowing what was wrong so it was as i
guessed at. the binary stream didn't like to be in lineedit first..
I wanna update the retrieved image which is displayed in jpg format. How do I go about it plz?
The image label (i.e the qpixmap label) is self.passlb
if (col == 2) and itemArray: img = itemArray.data(QTableWidgetItem.UserType) pixmap = QPixmap() pixmap.loadFromData(img, 'jpg') itemList[2].setPixmap((pixmap).scaled(250,250))
So in my sql update statement, I tried:
values(self.fullname.text(), self.passlb.pixmap() ...)``` It didn't work. Do you think it should be: img.pixmap() or img.text() or img.read() Any clue about the method I should use in updating a qpixmap label?
-
.
I wanna update the retrieved image which is displayed in jpg format. How do I go about it plz?
The image label (i.e the qpixmap label) is self.passlb
if (col == 2) and itemArray: img = itemArray.data(QTableWidgetItem.UserType) pixmap = QPixmap() pixmap.loadFromData(img, 'jpg') itemList[2].setPixmap((pixmap).scaled(250,250))
So in my sql update statement, I tried:
values(self.fullname.text(), self.passlb.pixmap() ...)``` It didn't work. Do you think it should be: img.pixmap() or img.text() or img.read() Any clue about the method I should use in updating a qpixmap label?
Hi
You mean save an actual pixmap back to the database ?`But what is the type of field in the db and how did you get the other images in there ?
Its clear we need to convert it to a binary stream and we can use QBuffer for that
(c++, could not find python example)
QPixmap inPixmap =self.passlb.pixmap(); QByteArray inByteArray; QBuffer inBuffer( &inByteArray ); inBuffer.open( QIODevice::WriteOnly ); inPixmap.save( &inBuffer, "PNG" ); // write inPixmap into inByteArray in PNG format
inBuffer can now be saved in DB.
But I wonder if there is the reverse of io.BytesIO ? we should rather use ?
-
Hi
You mean save an actual pixmap back to the database ?`But what is the type of field in the db and how did you get the other images in there ?
Its clear we need to convert it to a binary stream and we can use QBuffer for that
(c++, could not find python example)
QPixmap inPixmap =self.passlb.pixmap(); QByteArray inByteArray; QBuffer inBuffer( &inByteArray ); inBuffer.open( QIODevice::WriteOnly ); inPixmap.save( &inBuffer, "PNG" ); // write inPixmap into inByteArray in PNG format
inBuffer can now be saved in DB.
But I wonder if there is the reverse of io.BytesIO ? we should rather use ?
@mrjj sorry,
See this function, it's for uploading images in the form for update:
def openfile(self): try: fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)") self.passlb.setPixmap(QPixmap(fname).scaled(250, 250)) self.img = open(fname, 'rb') self.psptpath.setText(fname) # self.pixL.setText(strfn) print(self.pixlb.setText(''))
This function is for appending the jpg to the qlabel self.passlb from the qtable after it's been retrieved from the database:
if (col ==2): img = itemArray.data(QTableWidgetItem.UserType) pixmap = QPixmap() pixmap.loadFromData(img, 'jpg') itemList[2].setPixmap((pixmap).scaled(250,250))
Then here is my sql statement updating the image:
(The usual update statement is here)
values (self.fullname.text(),
self.img.read()
)This is saying my program objects has no attribute 'img'
-
@mrjj sorry,
See this function, it's for uploading images in the form for update:
def openfile(self): try: fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)") self.passlb.setPixmap(QPixmap(fname).scaled(250, 250)) self.img = open(fname, 'rb') self.psptpath.setText(fname) # self.pixL.setText(strfn) print(self.pixlb.setText(''))
This function is for appending the jpg to the qlabel self.passlb from the qtable after it's been retrieved from the database:
if (col ==2): img = itemArray.data(QTableWidgetItem.UserType) pixmap = QPixmap() pixmap.loadFromData(img, 'jpg') itemList[2].setPixmap((pixmap).scaled(250,250))
Then here is my sql statement updating the image:
(The usual update statement is here)
values (self.fullname.text(),
self.img.read()
)This is saying my program objects has no attribute 'img'
@CEO
ah well its better with such logical bug than something that just don't work :) -
@mrjj sorry,
See this function, it's for uploading images in the form for update:
def openfile(self): try: fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)") self.passlb.setPixmap(QPixmap(fname).scaled(250, 250)) self.img = open(fname, 'rb') self.psptpath.setText(fname) # self.pixL.setText(strfn) print(self.pixlb.setText(''))
This function is for appending the jpg to the qlabel self.passlb from the qtable after it's been retrieved from the database:
if (col ==2): img = itemArray.data(QTableWidgetItem.UserType) pixmap = QPixmap() pixmap.loadFromData(img, 'jpg') itemList[2].setPixmap((pixmap).scaled(250,250))
Then here is my sql statement updating the image:
(The usual update statement is here)
values (self.fullname.text(),
self.img.read()
)This is saying my program objects has no attribute 'img'
-
@mrjj sorry,
See this function, it's for uploading images in the form for update:
def openfile(self): try: fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)") self.passlb.setPixmap(QPixmap(fname).scaled(250, 250)) self.img = open(fname, 'rb') self.psptpath.setText(fname) # self.pixL.setText(strfn) print(self.pixlb.setText(''))
This function is for appending the jpg to the qlabel self.passlb from the qtable after it's been retrieved from the database:
if (col ==2): img = itemArray.data(QTableWidgetItem.UserType) pixmap = QPixmap() pixmap.loadFromData(img, 'jpg') itemList[2].setPixmap((pixmap).scaled(250,250))
Then here is my sql statement updating the image:
(The usual update statement is here)
values (self.fullname.text(),
self.img.read()
)This is saying my program objects has no attribute 'img'
@CEO said in Appending an image to itemList of QFormLayout widgets:
@mrjj sorry,
See this function, it's for uploading images in the form for update:
def openfile(self): try: fname, _ = QFileDialog.getOpenFileName(self, 'Open', 'c:\\', "image Files (*.jpg *.png)") self.passlb.setPixmap(QPixmap(fname).scaled(250, 250)) self.img = open(fname, 'rb') self.psptpath.setText(fname) # self.pixL.setText(strfn) print(self.pixlb.setText(''))
This function is for appending the jpg to the qlabel self.passlb from the qtable after it's been retrieved from the database:
if (col ==2): img = itemArray.data(QTableWidgetItem.UserType) pixmap = QPixmap() pixmap.loadFromData(img, 'jpg') itemList[2].setPixmap((pixmap).scaled(250,250))
Then here is my sql statement updating the image:
(The usual update statement is here)
values (self.fullname.text(),
self.img.read()
)This is saying my program objects has no attribute 'img'
-
@mrjj I did some testing and discovered it works fine only when I update the photo. Updating any other column without updating the image (photo) produces an error saying no object found for the image column.
@CEO
But does it says that self.img don't exists ?but in def openfile(self):
self.img = open(fname, 'rb')
works
so is the function that uses the SQL, also part of the same class ?
-
@CEO
But does it says that self.img don't exists ?but in def openfile(self):
self.img = open(fname, 'rb')
works
so is the function that uses the SQL, also part of the same class ?
@mrjj the function that uses the SQL is part of the class. It works fine when in my update I upload a photo but comes up with that error "object has no attribute 'img' " if I don't upload a photo (i.e when I leave the photo retrieved from the database). Meaning even if I upload the same photo there, it will work. It just wants the upload to be done in that pixmap field
-
@mrjj the function that uses the SQL is part of the class. It works fine when in my update I upload a photo but comes up with that error "object has no attribute 'img' " if I don't upload a photo (i.e when I leave the photo retrieved from the database). Meaning even if I upload the same photo there, it will work. It just wants the upload to be done in that pixmap field
So when loading from db, you need to both set the UserRole and the pixmap field
it sounds like even the error "object has no attribute 'img' " is very confusing
if it is indeed a class member.