Threading with Pyside6 and qml - qml

With PyQT5 and QtDesigner I used to make window app with threading like that:
class MainUIClass(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self,parent=None):
super().__init__()
self.setupUi(self)
#Create threads
self.thread_1 = QThread()
# Create worker object
self.worker_1 = Worker_1(self)
# Move workers to thread
self.worker_1.moveToThread(self.thread_1)
# Connect signals and slots
self.thread_1.started.connect(self.worker_1.run)
self.thread_1.start(QThread.Priority.NormalPriority)
where Worker_1 is like:
class Worker_1(QObject):
def __init__(self,mainWin,parent = None ):
super(CoreWorker, self).__init__(parent)
#super().__init__()
self.mainWin = mainWin
def run(self):
while True:
pass
# do something
and main
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainUIClass()
win.show()
sys.exit(app.exec_())
Similarly for thread 2,3,...
Now I want try to make something similar with PySide but for design I want to use qml (in QtCreator) instead of using Ui_MainWindow from QT designer. Maybe the main difference (problem) is not in converting from PyQT to Pyside but from converting from qt designer to to qml.
Is there any help please?

Related

Qpushbutton in custom QlistWidgetItem doesn't respond when running on embedded device

I'm quite new to pyqt5 and applications on embedded devices, and was hoping someone may have any tips or suggestions on the problems I'm facing.
I am working with a pyqt5 appliaction where the widget contains a QListWidget and two buttons. I have made a seperate widget that I use as a customized QListWidgetItem. The customized QListWidgetItem contains a textfield and a QPushButton.
Everything works perfectly on Ubuntu on my computer, but when I deploy and run the app on an embedded device, the buttons in the customized QListWidgetItem no longer responds. The "normal" buttons, that are not in the customized QListWidgetItem, still works normally.
I can't find a reason or fixes for this, and am quite stuck..
Here's my code, very simplified:
Edit! After some feedback I've created a smaller representation of the app/problem, which hopefully can make it easier to debug.
I no longer use pyqtSignal between the classes, only connected the buttons to different functions directly.
In this case, both the printing and color changing will happen if the buttons respond correctly.
python file:
import sys
from CustomListItem import Ui_custom_list_widget
from CollectorWidget import Ui_collector_widget
from PyQt5.QtWidgets import QWidget, QListWidgetItem, QApplication, QStackedWidget, QMainWindow
def main():
app = QApplication(sys.argv)
stack = QStackedWidget()
collector = CollectorWidget()
stack.addWidget(collector)
stack.setCurrentWidget(collector)
stack.show()
sys.exit(app.exec_())
class CollectorWidget(Ui_collector_widget, QWidget):
temp_list_items = []
def __init__(self, parent=None):
super(CollectorWidget, self).__init__(parent)
self.setupUi(self)
self.create_item_button.clicked.connect(self.create_item_clicked)
self.another_button.clicked.connect(lambda: print("this one works"))
def create_item_clicked(self):
self.listItem = QListWidgetItem()
self.item = CustomListItem()
self.item.custom_item_button.clicked.connect(self.test_signal)
self.qlistwidget_list.addItem(self.listItem)
self.qlistwidget_list.setItemWidget(self.listItem, self.item)
self.temp_list_items.append((self.listItem, self.item))
def test_signal(self):
print("--------------IT'S WORKING!--------------------------")
class CustomListItem(Ui_custom_list_widget, QWidget):
def __init__(self):
super().__init__()
self.setupUi(self)
self.item_name.setText("testing testing")
self.custom_item_button.clicked.connect(self.button_clicked)
def setItemText(self, text):
self.item_name.setText(text)
def button_clicked(self):
self.item_name.setStyleSheet("background-color: red;")
if __name__ == "__main__":
main()
Ui files (collector):
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'CollectorWidget.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_collector_widget(object):
def setupUi(self, collector_widget):
collector_widget.setObjectName("collector_widget")
collector_widget.resize(625, 514)
self.verticalLayout = QtWidgets.QVBoxLayout(collector_widget)
self.verticalLayout.setObjectName("verticalLayout")
self.qlistwidget_list = QtWidgets.QListWidget(collector_widget)
self.qlistwidget_list.setObjectName("qlistwidget_list")
self.verticalLayout.addWidget(self.qlistwidget_list)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.create_item_button = QtWidgets.QPushButton(collector_widget)
self.create_item_button.setObjectName("create_item_button")
self.horizontalLayout.addWidget(self.create_item_button)
self.another_button = QtWidgets.QPushButton(collector_widget)
self.another_button.setObjectName("another_button")
self.horizontalLayout.addWidget(self.another_button)
self.verticalLayout.addLayout(self.horizontalLayout)
self.retranslateUi(collector_widget)
QtCore.QMetaObject.connectSlotsByName(collector_widget)
def retranslateUi(self, collector_widget):
_translate = QtCore.QCoreApplication.translate
collector_widget.setWindowTitle(_translate("collector_widget", "Form"))
self.create_item_button.setText(_translate("collector_widget", "Create"))
self.another_button.setText(_translate("collector_widget", "Just a button"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
collector_widget = QtWidgets.QWidget()
ui = Ui_collector_widget()
ui.setupUi(collector_widget)
collector_widget.show()
sys.exit(app.exec_())
Ui files (custom_list_item):
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'CustomListItem.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_custom_list_widget(object):
def setupUi(self, custom_list_widget):
custom_list_widget.setObjectName("custom_list_widget")
custom_list_widget.resize(608, 40)
self.horizontalLayout = QtWidgets.QHBoxLayout(custom_list_widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setSpacing(0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.item_name = QtWidgets.QLabel(custom_list_widget)
font = QtGui.QFont()
font.setKerning(True)
self.item_name.setFont(font)
self.item_name.setStyleSheet("")
self.item_name.setWordWrap(True)
self.item_name.setObjectName("item_name")
self.horizontalLayout_2.addWidget(self.item_name)
self.custom_item_button = QtWidgets.QPushButton(custom_list_widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.custom_item_button.sizePolicy().hasHeightForWidth())
self.custom_item_button.setSizePolicy(sizePolicy)
self.custom_item_button.setMaximumSize(QtCore.QSize(20, 20))
self.custom_item_button.setStyleSheet("")
self.custom_item_button.setText("")
self.custom_item_button.setObjectName("custom_item_button")
self.horizontalLayout_2.addWidget(self.custom_item_button)
self.horizontalLayout.addLayout(self.horizontalLayout_2)
self.retranslateUi(custom_list_widget)
QtCore.QMetaObject.connectSlotsByName(custom_list_widget)
def retranslateUi(self, custom_list_widget):
_translate = QtCore.QCoreApplication.translate
custom_list_widget.setWindowTitle(_translate("custom_list_widget", "Form"))
self.item_name.setText(_translate("custom_list_widget", "TextLabel"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
custom_list_widget = QtWidgets.QWidget()
ui = Ui_custom_list_widget()
ui.setupUi(custom_list_widget)
custom_list_widget.show()
sys.exit(app.exec_())
Picture of app:

How to separate between python generated ui and our apps logic in pyqt5? [duplicate]

I'm using Qt Designer for design GUI to use in python, after designing my desired UI in Qt Designer, convert it to python code and then I changed generated code to do some action in my python code, but if I changed the UI with Qt Designer and convert it to python code again, I lost my previous changes on my code.
how can I solve the problem?
can we Spreading a Class Over Multiple Files in python to write code in other files?
To avoid having these problems it is advisable not to modify this file but to create a new file where we implement a class that uses that design.
For example, suppose you have used the MainWindow template in the design.ui file, then convert it to Ui_Design.py like to the following structure:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
[...]
def retranslateUi(self, MainWindow):
[...]
Then we will create a new file that we will call logic.py where we will create the file that handles the logic and that uses the previous design:
class Logic(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
So even if you modify the design and generate the file again .py you will not have to modify the file of the logic.
To generalize the idea we must have the following rules but for this the logic class must have the following structure:
class Logic(PyQtClass, DesignClass):
def __init__(self, *args, **kwargs):
PyQtClass.__init__(self, *args, **kwargs)
self.setupUi(self)
PyQtClass: This class depends on the design chosen.
Template PyQtClass
─────────────────────────────────────────────
Main Window QMainWindow
Widget QWidget
Dialog with Buttons Bottom QDialog
Dialog with Buttons Right QDialog
Dialog with Without Buttons QDialog
DesignClass: The name of the class that appears in your design.
The advantage of this implementation is that you can implement all the logic since it is a widget, for example we will implement the solution closing pyqt messageBox with closeevent of the parent window :
class Logic(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
def closeEvent(self, event):
answer = QtWidgets.QMessageBox.question(
self,
'Are you sure you want to quit ?',
'Task is in progress !',
QtWidgets.QMessageBox.Yes,
QtWidgets.QMessageBox.No)
if answer == QtWidgets.QMessageBox.Yes:
event.accept()
else:
event.ignore()
The easiest way is to use the *.ui file directly in the python code, you don't need convert to *.py file every time you change the ui.
you can use this pseudo code in your project.
# imports
from PyQt5 import uic
# load ui file
baseUIClass, baseUIWidget = uic.loadUiType("MainGui.ui")
# use loaded ui file in the logic class
class Logic(baseUIWidget, baseUIClass):
def __init__(self, parent=None):
super(Logic, self).__init__(parent)
self.setupUi(self)
.
.
.
.
def main():
app = QtWidgets.QApplication(sys.argv)
ui = Logic(None)
ui.showMaximized()
sys.exit(app.exec_())

Matplot lib does not plot with PyQt

I'm having a problem with PyQt and Mathplotlib.
Here you can find a pseudocode of what I am doing: I have a class "MainWindow" that creates a main window with a menu and an empty mathplotlib graph. When I click on the menu Item, the method "Select" is executed that opens a new Dialog. There is also a method that plots on the grah the content of the global variable Data.
import TeraGui
Data = []
class MainWindow(QMainWindow, TeraGui.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.actionSelect.triggered.connect(self.Select)
# Create the frame with the graphs
self.create_main_frame()
#Plot empty graphs
self.axes.clear()
self.canvas.draw()
def create_main_frame(self):
self.main_frame = QWidget()
# Create the mpl Figure and FigCanvas objects.
# 5x4 inches, 100 dots-per-inch
#
self.dpi = 100
self.fig = Figure((5.0, 4.0), dpi=self.dpi)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self.main_frame)
#
self.axes = self.fig.add_subplot(111)
# Create the navigation toolbar, tied to the canvas
#
self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)
#
# Layout with box sizers
#
vbox = QVBoxLayout()
vbox.addWidget(self.canvas)
vbox.addWidget(self.mpl_toolbar)
self.main_frame.setLayout(vbox)
self.setCentralWidget(self.main_frame)
def Plotting(self):
""" Redraws the figure
"""
print "I am here"
time = Data[0]
sig = Data[]
plot(time, sig)
# clear the axes and redraw the plot anew
#
self.axes.clear()
self.axes.plot(time, sig)
self.canvas.draw()
def Select(self):
dialog = Dialog(self)
dialog.exec_()
Now, if I add in the init method of the MainWindow class these lines:
Global Data
Data = [[1,2,3],[4,5,6]]
self.Plotting()
"I am here" is printed and the plot is correctly displayed into the graph, BUT if I don't add these lines and i try to call Plotting from the Dialog class it doesn't work. "I am here" is plotted but the plot stays empty. In the Dialog class, method "accept" is caled when the "ok" button of a button box is pressed:
class Dialog(QDialog, TeraGui.Ui_SelectFiles):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setAttribute(Qt.WA_DeleteOnClose)
self.setupUi(self)
def accept(self):
global Data
Data = [[1,2,3],[4,5,6]]
MainWindow().Plotting()
The Plotting method draws also a separate plot by means of the command "plot(time,sig)". This plot is always showed correctly regardless the way used to call Plotting.
These are my fist tries with PyQt and matplotlib and I am not able to identify the mistake.
The problem is with the line
MainWindow().Plotting()
When you write MainWindow() you are actually creating a new instance of the MainWindow class and calling its Plotting() function, not the one of your existing MainWindow instance. This window is never shown, and since you don't save a reference to it, is subsequently deleted when accept() returns. The only evidence of its existence is the 'i am here' message it writes to the console. This is why you don't see the plot.
In this case, you are setting your MainWindow instance as the parent of dialog through dialog = Dialog(self), so you could access it though a call to parent().
self.parent().Plotting()
You should also consider adding another parameter to the Plotting() function so you can pass your data directly to it instead of having to declare globals everywhere.
def Plotting(self, Data):
...

PyQt5 crash with QThread

The following minimal code crashes in the QThread's run for loop. This works when removing the widget object.
import sys
import time
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Widget(QLabel):
def __init__(self):
super(Widget, self).__init__()
self.setText("hello")
self.show()
class Worker(QThread):
def __init__(self):
super(Worker, self).__init__()
def run(self):
for i in range(1,2):
label = QLabel()
label.setText(str(i))
label.show()
time.sleep(1)
label.close()
app = QApplication(sys.argv)
widget = Widget()
worker = Worker()
worker.start()
sys.exit(app.exec_())
From qt5 documentation:
http://qt-project.org/doc/qt-5.0/qtcore/thread-basics.html
GUI Thread and Worker Thread
As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a "worker thread" because it is used to offload processing work from the main thread.
You can't create widgets in worker thread.
Apparently time.sleep is causing the segfault. You can use QThread.sleep (self.sleep in a thread) instead.
Cheers!

PyGTK event definition class

In many PyGTk tutorials, event handlers are defined like below.
window.connect("destroy", self.close)
button.connect("clicked", self.print_hello_world)
Is there any class encapsulating "destroy", "clicked" string literals as I want to access them as constants.
In small apps, we may write code like this:
class MyApp():
def __init__(self):
self.win = Gtk.Window()
self.win.set_size_request(400, 300)
self.win.connect('destroy', self.on_app_exit)
btn = Gtk.Button("hello")
btn.connect('clicked', self.on_button_clicked)
def run(self):
self.win.show_all()
Gtk.main()
def on_app_exit(self, window):
// do something.
Gtk.main_quit()
def on_button_clicked(self, btn):
print('hello, world')
def main():
app = MyApp()
app.run()
if __name__ == '__main__':
main()