Python Forum
[PySide6] Load ui with UiLoader
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PySide6] Load ui with UiLoader
#1
Is there any way to load ui with Pyside6 that the widgets become attributes of the class instead of attributes of another attribute of the class e.g. "self.ui"

Like how ppl load ui with "uic.loadUi()" with PyQt.
Reply
#2
Using .ui files from Designer or QtCreator with QUiLoader and pyside6-uic
Reply
#3
(Nov-23-2021, 02:20 PM)Axel_Erfurt Wrote: Using .ui files from Designer or QtCreator with QUiLoader and pyside6-uic

That's what I found. But either way, you can only access widgets though "self.ui" instead of "self" differing from what it would be with "uic.loadUi()".

Also with QUiLoader(), the class in which you set up the self.ui is just a shell. you need "self.ui.show()" to see the ui instead of self.show(). That way, it somehow does not trigger closeEvent. Even if I checked that the type of self.ui is still QMainWindow(or whatever you designed it to be) and I override closeEven method in QWidget and QMainwindow. It just won't trigger it.
Reply
#4
I can use the same code in PyQt5 and PyQt6

The only difference is

app.exec() / app.exec_()

from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6 import uic
import sys

class Ui(QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('example.ui', self)
        self.show()

app = QApplication(sys.argv)
window = Ui()
app.exec() # app.exec_() in PyQt5 #
Reply
#5
(Nov-24-2021, 01:45 PM)Axel_Erfurt Wrote: I can use the same code in PyQt5 and PyQt6

The only difference is

app.exec() / app.exec_()

from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6 import uic
import sys

class Ui(QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('example.ui', self)
        self.show()

app = QApplication(sys.argv)
window = Ui()
app.exec() # app.exec_() in PyQt5 #

they both can be used but I just want to load ui to self instead of self.ui
Still the problem of pyside loading ui this way is that it does not trigger closeEvent
because when you close the window, what you close is self.ui instead of self. So define a closeEvent in the class does not work.
self.ui should be a QMainWindow object but I don't know how to change the closeEvent of it

I tried to override the closeEvent of the QMainWindow class or define a closeEvent and pass it to self.ui.closeEvent but neither worked.
Reply
#6
You can convert your ui file to a py file

pyuic6 -x example.ui -o example.py
Reply
#7
yeah if I use my class as 'Form' and run setup(). That way I might as well go back using pyqt5 @_@
is one better than the other? pyqt5 and pyside6?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PySide6 - Connect to DBus signal - Correct syntax Drexel 1 581 Dec-18-2023, 08:03 AM
Last Post: Drexel
  PySide6 Copy and Past from clipboard to QTableWedget zinho 6 1,154 Dec-07-2023, 10:10 PM
Last Post: zinho
Bug [PyQt] Dinamically adding widgets to layout [PySide6] carecavoador 2 1,371 Jun-19-2023, 12:57 PM
Last Post: carecavoador
  PySide6 QFontDialog - bug or just me? PatM 1 1,074 Jan-22-2023, 01:29 PM
Last Post: Axel_Erfurt
  Pyside6 Larz60+ 7 2,829 Nov-28-2022, 07:25 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020