Python Forum

Full Version: Show image in qtablewidget
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Bonjour tout le monde

Je n'arrive pas à afficher des images dans la table avec ce code :

#!/usr/bin/python
# -*- coding: utf-8 -*-

from PyQt5 import QtWidgets
from PyQt5 import QtGui, QtCore
import sys
import mainwindow  # import du fichier mainwindow.py généré par pyuic5


import pycurl
import cStringIO
import re


class MyWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.ui = mainwindow.Ui_MainWindow()
        self.ui.setupUi(self)
        
        # un clic sur le bouton appellera la méthode 'action_bouton'
        self.ui.pushButton_2.clicked.connect(self.action_bouton)

        
        self.ui.tableWidget.setColumnCount(2)
        header = self.ui.tableWidget.horizontalHeader()       
        header.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
        header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)


    def action_bouton(self):
        print('Appui bouton.')
        buf = cStringIO.StringIO()
        ExampleVariable = 'TEST' 
        c = pycurl.Curl()
        c.setopt(c.URL, 'URLTEST'+ExampleVariable+'URLTEST') #->OK
        c.setopt(c.WRITEFUNCTION, buf.write)
        c.perform()
 
        from bs4 import BeautifulSoup
        soup = BeautifulSoup(buf.getvalue(), "lxml")

	for each_div in soup.findAll("div", {"class": "wrapper_product_1"}):
    		print each_div.findNext('img')['src'].encode('utf-8') #recup image -> OK 
            NameImage = each_div.findNext('img')['alt']
    		rowPosition = self.ui.tableWidget.rowCount()
        	self.ui.tableWidget.insertRow(rowPosition)
        	self.ui.tableWidget.setItem(rowPosition,0, QtWidgets.QTableWidgetItem(NameImage))

            ????
            ##self.tableWidget_SearchJeuQueG.setImage(rowPosition, 1, each_div.findNext('img')['src'])
            ##self.tableWidget_SearchJeuQueG.setItem(rowPosition , 1, QIcon("test.jpg"))
            ##self.tableWidget_SearchJeuQueG.item(rowPosition,1).setIcon(QtGui.QIcon(each_div.findNext('img')['src']))
            ????

		buf.close()



if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())
Tout le reste du code fonctionne même si là j'en ai enlevé une partie, mais rien à faire je n'arrive pas à afficher les images retournées dans ma deuxième colonne. les noms apparaissent bien dans la première. Les images sont bien trouvées car je peux les récupérer et les enregistrer sur mon disque.

Quelqu'un aurait une idée ou un explication ? Merci beaucoup d'avance !

------------------------------
Hello everybody

I can not display images in the table with this code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from PyQt5 import QtWidgets
from PyQt5 import QtGui, QtCore
import sys
import mainwindow  # import du fichier mainwindow.py généré par pyuic5


import pycurl
import cStringIO
import re


class MyWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.ui = mainwindow.Ui_MainWindow()
        self.ui.setupUi(self)
        
        # un clic sur le bouton appellera la méthode 'action_bouton'
        self.ui.pushButton_2.clicked.connect(self.action_bouton)

        
        self.ui.tableWidget.setColumnCount(2)
        header = self.ui.tableWidget.horizontalHeader()       
        header.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
        header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)


    def action_bouton(self):
        print('Appui bouton.')
        buf = cStringIO.StringIO()
        ExampleVariable = 'TEST' 
        c = pycurl.Curl()
        c.setopt(c.URL, 'URLTEST'+ExampleVariable+'URLTEST') #->OK
        c.setopt(c.WRITEFUNCTION, buf.write)
        c.perform()
 
        from bs4 import BeautifulSoup
        soup = BeautifulSoup(buf.getvalue(), "lxml")

	for each_div in soup.findAll("div", {"class": "wrapper_product_1"}):
    		print each_div.findNext('img')['src'].encode('utf-8') #recup image -> OK 
            NameImage = each_div.findNext('img')['alt']
    		rowPosition = self.ui.tableWidget.rowCount()
        	self.ui.tableWidget.insertRow(rowPosition)
        	self.ui.tableWidget.setItem(rowPosition,0, QtWidgets.QTableWidgetItem(NameImage))

            ????
            ##self.tableWidget_SearchJeuQueG.setImage(rowPosition, 1, each_div.findNext('img')['src'])
            ##self.tableWidget_SearchJeuQueG.setItem(rowPosition , 1, QIcon("test.jpg"))
            ##self.tableWidget_SearchJeuQueG.item(rowPosition,1).setIcon(QtGui.QIcon(each_div.findNext('img')['src']))
            ????

		buf.close()



if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())
All the rest of the code works even if I removed part of it, but nothing to do I can not display the images returned in my second column. the names appear well in the first. The images are well found because I can recover them and save them on my disk.

Someone would have an idea or an explanation? Thank you very much in advance !