Icon not showing in QListWidgetItem - python

Icons are not showing in my QListWidgetItems.
Edit 2:
Turns out you can't use absoulte path. You must use relative path. But why is this the case?
Minimum reproducible example:
import json
import os
import sys
from math import floor
from PyQt5 import QtTest, QtGui
from PyQt5.QtDesigner import QFormBuilder
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QFrame, QGridLayout, QLabel, QMainWindow, QScrollArea, QWidget, QVBoxLayout, \
QListView, QListWidget, QListWidgetItem, QToolButton
from PyQt5.QtCore import QPoint, Qt, QIODevice, QFile
from PyQt5.Qt import QPixmap
class MainWindow(QMainWindow):
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
#ListWidget
listWidget = QListWidget(self)
listWidget.setViewMode(QListWidget.IconMode)
listWidget.setFixedSize(500, 700)
dir = r'Players'
for filename in os.listdir(dir):
#Item
item = QListWidgetItem(QIcon('Players/Pogba.jpg'), '<Name>', listWidget)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.showMaximized()
app.exec_()
I followed the docs(https://doc.qt.io/qt-5/qlistwidgetitem.html), so why are they not appearing?
Edit:
It looks like it only happens when the image is outside the source code's folder. Why is this the case?

Related

Browse media with PyQT6

I wanted to create a browsing media file with QT so I wrote that :
import sys
import random
import os
import time
from PyQt6 import QtCore, QtWidgets, QtGui
from PyQt6.QtWidgets import QLabel, QPushButton,QSizePolicy, QFileDialog,QHBoxLayout, QVBoxLayout, QSlider, QInputDialog, QMainWindow, QApplication
from PyQt6.QtGui import QPixmap, QShortcut, QKeySequence, QMovie
from PyQt6.QtCore import Qt,QUrl, QTimer, QSettings, QSize, QPoint
from PyQt6.QtMultimediaWidgets import QVideoWidget
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
# Subclass QMainWindow to customize your application's main window
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.posi = 0
#path
if (len( sys.argv ) > 1):
self.folderpath = sys.argv[1]
else:
self.folderpath = "your folder contaianing the files" #<============ DONT FORGET TO FINISH THE PATH WITH /
#pathlist
self.img_list = os.listdir(self.folderpath)
print(self.img_list)
self.setWindowTitle("My App")
self.mediaPlayer = QMediaPlayer(None)
self.videoWidget = QVideoWidget()
self.audio_output = QAudioOutput()
self.mediaPlayer.setVideoOutput(self.videoWidget)
self.mediaPlayer.setAudioOutput(self.audio_output)
self.audio_output.setVolume(50)
self.mediaPlayer.setLoops(-1)
self.mediaPlayer.setSource(QUrl(self.folderpath+self.img_list[0]))
self.mediaPlayer.play()
# Set the central widget of the Window.
self.setCentralWidget(self.videoWidget)
#shortcut
self.shortcut_right = QShortcut(QKeySequence("Right"),self)
self.shortcut_right.activated.connect(self.button_right_clicked)
def button_right_clicked(self, *args):#when navigate right
print("---->")
try:
self.img_list[self.posi+1]
except IndexError:
self.posi = 0
else:
self.posi = self.posi + 1
self.mediaPlayer.setSource(QUrl(self.folderpath+self.img_list[self.posi]))
self.mediaPlayer.play()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
But sometimes when use the method "button_right_clicked" the program shutdown without any error or closing event. This method is used to navigate through the list of files in the source folder. I'm using only mp4 file for now.
The error comes from this line i presume : self.mediaPlayer.setSource(QUrl(self.folderpath+self.img_list[self.posi]))
Because when i try to set the same media (img_list[0]) file as source, instead of using index self.posi, it doesn't crash.
According to the documentation, it seems to be the approriate way of swapping between media.
Any idea how to solve this problem ?

How to interact with interface elements through different classes? [duplicate]

This question already has answers here:
Linking a qtDesigner .ui file to python/pyqt?
(12 answers)
Correct way to address Pyside Qt widgets from a .ui file via Python
(2 answers)
Closed 11 months ago.
For example, I want to change the text of a button by binding it to a function from another class. But I get an error on the line self.ui.pushButton.setText("OK"):
AttributeError: 'bool' object has no attribute 'ui'
Here is main.py code:
from PySide6.QtWidgets import QApplication, QWidget
import test_gui
class App(QWidget):
def __init__(self):
QWidget.__init__(self)
self.ui = test_gui.Ui_Form()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(Second.func)
class Second(App):
def __init__(self):
App.__init__(self)
def func(self):
self.ui.pushButton.setText("OK")
if __name__ == "__main__":
app = QApplication([])
mw = App()
mw.show()
app.exec()
Here is test_gui.py code:
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'test_gui.ui'
##
## Created by: Qt User Interface Compiler version 6.2.4
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QPushButton, QSizePolicy, QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(392, 279)
self.pushButton = QPushButton(Form)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(120, 110, 75, 24))
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
self.pushButton.setText(QCoreApplication.translate("Form", u"PushButton", None))
# retranslateUi
Please tell me what is the problem?

PyQt5 from apt install python3-pyqt5 [duplicate]

My code was created with PyQt4 and I want to convert it to PyQt5.
I have tried some scripts to convert the code; but, nothing changed except the name.
What do I need to change manually in order to make the code work with PyQt5?
Here is the first part of my code:
import sys
from pymaxwell import *
from numpy import *
from PyQt4 import QtGui, QtCore, uic
from PyQt4.QtGui import QMainWindow, QApplication
from PyQt4.QtCore import *
from PyQt4.phonon import Phonon
from ffmpy import FFmpeg
import os
import app_window_dark
import about
uifile = 'Ui/app_window_dark.ui'
aboutfile = 'Ui/about.ui'
Ui_MainWindow, QtBaseClass = uic.loadUiType(uifile)
Ui_Dialog= uic.loadUiType(uifile)
class About(QtGui.QMainWindow, about.Ui_Dialog):
def __init__(self, parent=None):
super(About, self).__init__()
QtGui.QMainWindow.__init__(self, parent)
Ui_Dialog.__init__(self)
self.setWindowModality(QtCore.Qt.ApplicationModal)
point = parent.rect().bottomRight()
global_point = parent.mapToGlobal(point)
self.move(global_point - QPoint(395, 265))
self.setupUi(self)
class MyApp(QtGui.QMainWindow, app_window_dark.Ui_MainWindow):
def __init__(self):
super(MyApp, self).__init__()
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow.__init__(self)
self.setupUi(self)
self.about_btn.clicked.connect(self.popup)
#prev next
self.btn_next.clicked.connect(self.renderSet)
self.btn_prev.clicked.connect(self.renderSet)
and also this code:
if __name__ == "__main__":
app = QApplication(sys.argv)
#style = QApplication.setStyle('plastique')
window = MyApp()
window.setFixedSize(750, 320)
window.show()
sys.exit(app.exec_())
The main change from Qt4 to Qt5 and hence from PyQt4 to PyQt5 is the rearrangement of certain classes so that the Qt project is scalable and generates a smaller executable.
The QtGui library was divided into 2 submodules: QtGui and QtWidgets, in the second only the widgets, namely QMainWindow, QPushButton, etc. And that is the change you must make:
[...]
from PyQt5 import QtGui, QtCore, uic, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtCore import *
[...]
Ui_MainWindow, QtBaseClass = uic.loadUiType(uifile)
Ui_Dialog= uic.loadUiType(uifile)
class About(QtWidgets.QMainWindow, about.Ui_Dialog):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.setWindowModality(QtCore.Qt.ApplicationModal)
point = parent.rect().bottomRight()
global_point = parent.mapToGlobal(point)
self.move(global_point - QPoint(395, 265))
class MyApp(QtWidgets.QMainWindow, app_window_dark.Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setupUi(self)
self.about_btn.clicked.connect(self.popup)
#prev next
self.btn_next.clicked.connect(self.renderSet)
self.btn_prev.clicked.connect(self.renderSet)
Note: Phonon does not exist in PyQt5, you must use QtMultimedia, an accurate solution you can find it in the following answer: Phonon class not present in PyQt5

pyqt5 - How to add value to QComboBox fromfunction

I try to add items to QComboBox (deviceBox in my code) from function "get_devices" using "activated.connect()" method, but QComboBox is empty. Where I was mistaken?
#!/usr/bin/env python3
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QFileDialog
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from mainwindow import *
import sys
import pyudev
class MainProg (QMainWindow, Ui_MainWindow):
file_open = pyqtSignal(str, str)
def __init__(self, window):
QMainWindow.__init__(self)
self.setupUi(window)
self.openisoButton.clicked.connect(self.openISO)
self.aboutButton.clicked.connect(self.about)
self.deviceBox.activated.connect(self.get_devices)
def get_devices(self):
devices = []
context = pyudev.Context()
for device in context.list_devices(subsystem='block', ID_BUS="usb"):
devices.append(str(device['DEVNAME']))
self.deviceBox.addItems(devices)
QCombBox object have a method named addItem which could append item to exist item list.
so you need to modify your code in last line.
for device in devices:
self.deviceBox.addItem(device)

PyQt touch certain points on pixmap

I have a form with a picture on it, in this example it is a Humidity Indicator the user should be able to press the 60, 10 and 5% spot. The label should display the pressed spot.
How do I do that could somebody show me an example or is this not possible with Qt?
My code (which is pretty empty for now)
from PyQt5.QtWidgets import QApplication, QWidget, QDialog
from PyQt5.QtGui import QPixmap, QRegExpValidator
from PyQt5.QtCore import QRegExp
from mysql.connector import (connection)
from datetime import *
from bs4 import BeautifulSoup as bs
import os
import sys
import DatabaseHandling
'''Convert UI file to Python'''
os.chdir("C:\\Users\Gianni Declercq\AppData\Local\Programs\Python\Python36-32\Scripts")
os.system("pyuic5.exe M:\QtProjects\\Ui\RPI1_Third.ui -o M:\QtProjects\\RPI1_Third_ui.py")
from RPI1_Third_ui import Ui_Form3 # import after recreation of py file
class ThirdWindow(QWidget, Ui_Form3):
def __init__(self):
super(ThirdWindow, self).__init__()
self.dbu = DatabaseHandling.DatabaseUtility()
self.msl = None
# Show UI on screen + resize window
self.setupUi(self)
self.picInidicator.setPixmap(QPixmap("F:\QtProjects\\138691.jpg"))
self.setFixedSize(800, 480)
if __name__ == '__main__':
app = QApplication([])
window = ThirdWindow()
window.show()
sys.exit(app.exec_())
Example of the form

Categories