Add Custom Widget to listWidget on pyqt5 - python

I used Qt Designer to make two .ui files, one is the Main Window of my application, and the second is a custom widget I made. My idea was to fill a listWidget on my Main application with this custom Widget to display data.
I made this code, which compiles without problem but it does not show the customWidget on the List when it runs
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget,QVBoxLayout,QDialog
from PyQt5 import QtCore, QtGui, QtWidgets
from mainwindowReclamo import Ui_MainWindow
from widgetReclamos import Ui_Form
#Custom Widget*
class WidgetReclamo(QWidget, Ui_Form):
"""docstring for ClassName"""
def __init__(self,*args,**kwargs):
QWidget.__init__(self,*args,**kwargs)
self.setupUi(self)
print("I am Alive")
#My Main Program*
class ProgramaReclamos(QMainWindow, Ui_MainWindow):
def __init__(self,*args,**kwargs):
QMainWindow.__init__(self,*args,**kwargs)
self.setupUi(self)
#I create an Item*
Item = QtWidgets.QListWidgetItem(self.listWidget)
#I create a custom widget*
Item_Widget = WidgetReclamo()
#I set the Size from the Item to the same of the widget*
Item.setSizeHint(Item_Widget.sizeHint())
#I add it to the list*
self.listWidget.addItem(Item)
self.listWidget.setItemWidget(Item, Item_Widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
prog = ProgramaReclamos()
prog.show()
sys.exit(app.exec_())
I saw some questions online wich their answer were for PyQt4 and they said something about using a Layout for the Widget, but I don´t understand if I have to make one becouse the widget was made in the .ui file

As EYLLANESC said on the comment:
Change Item.setSizeHint(Item_Widget.sizeHint()) to Item.setSizeHint(Item_Widget.size()) – eyllanesc
Thank you !

Related

If click Eyes icon Show/Hide password (QPushButton)

I'm trying to create a function in a register and login form using QLineEdit to show and hide password if click a QPushButton. I'm a beginner in Python, I'm just trying to do it but it's very hard... My attempt is not good because if I click the eye button the password is shown, but if click again to hide it does not work.
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QPushButton, QLineEdit
import sys
import pymysql
pymysql.install_as_MySQLdb()
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('MainWindow.ui', self)
self.eyepass_show()
self.eyepass_hide()
self.btn_show_pwd.clicked.connect(self.eyepass_hide)
self.btn_show_pwd.clicked.connect(self.eyepass_show)
def eyepass_show(self):
self.line_password.setEchoMode(QLineEdit.Normal)
print('show pass')
def eyepass_hide(self):
self.line_password.setEchoMode(QLineEdit.Password)
print('hide pass')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
form password hide/show eye:
is hiding password is show but if click again to hide not work
Instead of creating two separate methods as eyepass_show and eyepass_hide, you can create a single function and toggle the visibility. Also, you are trying to connect the same signal twice to two different methods by self.btn_show_pwd.clicked.connect(self.eyepass_hide)and self.btn_show_pwd.clicked.connect(self.eyepass_show)
Try something like this:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QPushButton, QLineEdit
import sys
import pymysql
pymysql.install_as_MySQLdb()
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('MainWindow.ui', self)
self.eyepass_show()
self.eyepass_hide()
self.btn_show_pwd.clicked.connect(self.toggleVisibility)
def toggleVisibility(self):
if self.line_password.echoMode()==QLineEdit.Normal:
self.line_password.setEchoMode(QLineEdit.Password)
else:
self.line_password.setEchoMode(QLineEdit.Normal)
# self.btn_show_pwd.clicked.connect(self.eyepass_hide)
# self.btn_show_pwd.clicked.connect(self.eyepass_show)
#
# def eyepass_show(self):
# self.line_password.setEchoMode(QLineEdit.Normal)
# print('show pass')
#
# def eyepass_hide(self):
# self.line_password.setEchoMode(QLineEdit.Password)
# print('hide pass')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
Another possibility is to add a checkable QAction to the QLineEdit and connect to the toggled (or triggered) signal.
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('MainWindow.ui', self)
icon = QtGui.QIcon('eye-icon.png')
self.showPassAction = QtWidgets.QAction(icon, 'Show password', self)
self.line_password.addAction(
self.showPassAction, QtWidgets.QLineEdit.TrailingPosition)
self.showPassAction.setCheckable(True)
self.showPassAction.toggled.connect(self.showPassword)
def showPassword(self, show):
self.line_password.setEchoMode(
QtWidgets.QLineEdit.Normal if show else QtWidgets.QLineEdit.Password)
If you want to show the password only when the mouse is pressed, then don't connect to the toggled signal, but find the child QToolButton for that action and connect to the pressed and released instead. In this case, the action doesn't need to be checkable.
self.line_password.addAction(
self.showPassAction, QtWidgets.QLineEdit.TrailingPosition)
showPassButton = self.line_password.findChild(QtWidgets.QAbstractButton)
showPassButton.pressed.connect(lambda: self.showPassword(True))
showPassButton.released.connect(lambda: self.showPassword(False))
One of the great things about PyQt5 is that it will automatically connect signals to methods for you. If you have a button named <mybutton> and a method in your app named on_<mybutton>_clicked, the the loadUi will automatically connect. So the pattern is
on_<widget_name>_<signal>
If we apply this to your problem, you should make your show/hide button a toggle button. In Qt Designer, set the checkable property to True (or do btn_show_pwd.clicked.setChecked(True))
The code:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QPushButton, QLineEdit
import sys
#import pymysql
#pymysql.install_as_MySQLdb()
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('test1.ui', self)
self.line_password.setEchoMode(QLineEdit.Password)
def on_btn_show_pwd_toggled(self, checked):
if checked:
self.line_password.setEchoMode(QLineEdit.Password)
else:
self.line_password.setEchoMode(QLineEdit.Normal)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())

How to load a Qt Designer file (.ui) in a QWizardPage using PySide2

I want to design my QWizardPages in Qt Designer and I want to load them into my Python program with PySide2. Previously I have been using PyQt5 without any problems but making the switch to PySide2 seems harder then expected.
The problem I am facing is that when I am adding a QWizardPage to my QWizard , the page is indeed added to the Wizard, but also an other (empty) page is added. I'm not able to find what I'm doing wrong so I was wondering if someone can have a look.
I have tried to add the pages with both the functions addPage() and setPage(), but they give the same results. What I also noticed is that when I explicitely set the Title of the page with setTitle(), the empty (unwanted) page gets this title, but not the page I designed in Qt Designer.
import os
import sys
from PySide2.QtWidgets import QWizard, QWizardPage, QApplication
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader
from enum import Enum
class MyWizard(QWizard):
def __init__(self):
super().__init__()
self.setPage(PageNumbers.page_one.value, PageOne(self))
class PageOne(QWizardPage):
def __init__(self, parent):
super().__init__(parent)
ui_file = os.path.join(__file__, '..', 'pageOne.ui')
file = QFile(ui_file)
file.open(QFile.ReadOnly)
loader = QUiLoader()
loader.load(file, parent)
file.close()
self.setTitle("This is another test Title")
class PageNumbers(Enum):
page_one = 1
if __name__ == '__main__':
app = QApplication(sys.argv)
wizard = MyWizard()
wizard.show()
app.exec_()
What I would expect is to have just one QWizardPage showing up with directly the Finish button. Instead I get two QWizardPages as shown in this image:
Can someone tell me what's going on?
(I get the expected result using PyQt5 with the following code: https://pastebin.com/6W2sx9M1)
The developers of PyQt implement functions to be able to create classes based on the .ui that is not implemented in Qt by default (Qt/C++ uses the MOC to do this work), but in the case of PySide2-Qt for python it does not implement it, only has the QUiLoader class that allows to create a widget based on the .ui unlike PyQt that allows filling a class.
In conclusion there is no equivalent in PySide2 of the loadUi function so you can not implement the same logic. PySide2 is not PyQt5, there are own equivalences since they use the same base but they have implementations, limitations and advantages.
Going to the practical problem, considering that the .ui is next to the .py the solution is the following:
import os
import sys
from PySide2 import QtCore, QtWidgets, QtUiTools
from enum import Enum
class PageNumbers(Enum):
page_one = 0
class MyWizard(QtWidgets.QWizard):
def __init__(self):
super().__init__()
ui_file = os.path.join(os.path.dirname(os.path.abspath(__file__)) ,'PageOne.ui')
page_one = create_widget(ui_file, self)
self.setPage(PageNumbers.page_one.value, page_one)
def create_widget(filename, parent=None):
file = QtCore.QFile(filename)
if not file.open(QtCore.QFile.ReadOnly):
return
loader = QtUiTools.QUiLoader()
widget = loader.load(file, parent)
return widget
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
wizard = MyWizard()
wizard.show()
sys.exit(app.exec_())

Qt5 UI not showing unchecked cells of table widget in python app

I'm begining with Python. I'm developing a simple desktop app to print a control check of motorcycles services. The problem is that the UI not showing the unchecked checkboxes in the table when I run the script
Screenshot with errors in red
Screenshot with cells configuration in Qt5 Designer
The code
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
class ServiceLabeler(QDialog):
def __init__(self):
super(ServiceLabeler, self).__init__()
loadUi('main.ui', self)
self.btnPrint.clicked.connect(self.printLabel)
#pyqtSlot()
def printLabel(self):
print('Printing ...')
app = QApplication(sys.argv)
widget = ServiceLabeler()
widget.show()
sys.exit(app.exec_())
If you want to see the main.ui code please follow next link to gist
https://gist.github.com/CristalT/0d2e5cc2c684c6dc2b87bd5ec1d7348e

How to add a QVideoWidget in Qt Designer?

I want to insert video in blue box(ui image) but I don't know how to insert video file.
My code is here.
I don't know how to add video... Just know example that make video player ...
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import uic
from PyQt5 import QtCore
from PyQt5.QtCore import QDir, Qt, QUrl, pyqtSlot
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget)
dir_audience=''
dir_movie = ''
dir_export = ''
select_emotion = 'happy'
class Form(QtWidgets.QDialog):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.ui = uic.loadUi("highlight_export_form.ui", self)
self.ui.show()
self.ui.load_audience.clicked.connect(self.load_audience_clicked)
self.ui.load_movie.clicked.connect(self.load_movie_clicked)
self.ui.start_recog.clicked.connect(self.start_recog_clicked)
self.ui.radio_happy.toggled.connect(self.on_radio_button_toggled)
self.ui.radio_surprised.toggled.connect(self.on_radio_button_toggled)
def load_audience_clicked(self, event):
dir_audience, _ = QFileDialog.getOpenFileName(self, "Open Audience", QDir.homePath())
self.path_audience.setText(dir_audience)
def load_movie_clicked(self, event):
dir_movie, _ = QFileDialog.getOpenFileName(self, "Open Movie", QDir.homePath())
self.path_movie.setText(dir_movie)
def start_recog_clicked(self, event):
self.check_1.setText("start_recognition")
def on_radio_button_toggled(self):
if self.radio_happy.isChecked():
select_emotion='happy'
self.check_3.setText(select_emotion)
elif self.radio_surprised.isChecked():
select_emotion='surprised'
self.check_3.setText(select_emotion)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Form()
sys.exit(app.exec())
Thank you for reading my question.
Qt Designer does not show all the Qt widget, and often we want to add our own widget through Qt, for that there are at least 2 solutions, the first is to create a plugin and load it to Qt Designer, and the other is simpler. promote the widget, the latter is what I will show in this answer.
For this you must make certain minimum changes, I do not know what type of widget is the one you use in the blue box but you must change it to the Widget type that is in the sub-menu of the containers as shown in the following image:
after them you must right click on the widget and select Promote to ..., then a dialogue will appear, in the part of Promoted class name you must place QVideoWidget, and in the part of Header File you must place PyQt5.QtMultimediaWidgets, then press the add button and then Promote:
After that you will be able to use QVideoWidget within your application.
In the following link there is an example
Answer from here was clearer to me:
QWebKit was removed in Qt 5.6. So QWebView is no longer available. Use QWebEngineView as a replacement. In Qt Designer, just add a QWidget to your form and promote it to QWebEngineView (base class: QWidget, header: QWebEngineView). Don't forget to add webenginewidgets to your project file.
Simlar issue: want add QWebEngineView into Qt Designer
for later PySide6 to import and use .ui, exported by Qt Designer
Solution: add QWidget then Promoted to QWebEngineView
Steps
drag a new QWidget into your main ui (window)
right click QWidget -> Promoted to
new popup window, input
Base class Name: QWidget
Promoted class Name: QWebEngineView
Header File: PySide6.QtWebEngineWidgets
== parent class
Global Include: not selected
-> Screenshot
click: Add
click: Promote

PyQt5 & Qt Designer: why the second form displays a bad thing?

I can't make form1 to make parent object like it needs to be.
And all content in second form align to left-top corner and i have no way to make it work fine. May be someone know what i can do with this. Thank you!
How it looks in Qt Designer:
But how it looks really:
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QMainWindow, QApplication
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# Set up the user interface from Designer.
uic.loadUi("mw.ui", self)
uic.loadUi("form1.ui", self.mn_general)
# Connect up the buttons.
self.pushButton.clicked.connect(self.BtnClck)
self.show()
def BtnClck(self):
print('Hello StackOverflow')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())

Categories

Resources