How do you load .ui files onto python classes with PySide? - python

I've used PyQt for quite a while, and the entire time I've used it, there has been a pretty consistent programming pattern.
Use Qt Designer to create a .ui file.
Create a python class of the same type as the widget you created in the .ui file.
When initializing the python class, use uic to dynamically load the .ui file onto the class.
Is there any way to do something similar in PySide? I've read through the documentation and examples and the closest thing I could find was a calculator example that pre-rendered the .ui file out to python code, which is the super old way of doing it in PyQt (why bake it to python when you can just parse the ui?)

I'm doing exactly that with PySide. :)
You use this https://gist.github.com/cpbotha/1b42a20c8f3eb9bb7cb8 (original by Sebastian Wiesner was at https://github.com/lunaryorn/snippets/blob/master/qt4/designer/pyside_dynamic.py but has disappeared) - which overrides PySide.QtUiTools.QUiLoader and supplies a new loadUi() method so that you can do this:
class MyMainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
loadUi('mainwindow.ui', self)
When you instantiate MyMainWindow, it will have the UI that you designed with the Qt Designer.
If you also need to use custom widgets ("Promote To" in Qt Designer), see this answer: https://stackoverflow.com/a/14877624/532513

Related

Converting .ui to .py with pyuic5?

When I convert a .ui file in QtDesigner to a .py file, the format changes and it runs differently.
When I run it in QtDesigner it looks like a normal page but once I convert it to a .py file and run it, the edges are cut off and I cannot see half the buttons/labels. Even once I expand the screen that has opened the labels are cut off and only half visible.
Is there a way I can stop this from happening?
You firstly need to correctly set the layout and widgets inside them, in a way that the size of each object is guaranteed when moving to the code.
Try to watch this tutorial, I found it very useful!
Qt Designer - create application GUI (DESIGN APPLICATION LAYOUT) - part 02
And then you need to just import the .ui file as follows:
from PyQt5 import uic
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow,self).__init__()
uic.loadUi("NameofYourFile.ui",self)
self.show()

How to make signals appear in QtDesigner?

The PyQt5 docs says
"New signals defined in this way will be automatically added to the
class’s QMetaObject. This means that they will appear in Qt Designer"
How do I make anything appear in QtDesigner if it only knows how to open *.ui files, but not python files?
In my workflow I create ui-file with QtDesigner, then convert it to python using pyuic5.
It is thus a one-way conversion (I then subclass it in another file to avoid my code being overwritten).
How do I make QtDesigner "see" my python code?
Qt Designer recognizes 2 types of elements:
The .ui that are the product,
And the plugins that are the ingredients.
So if you want a widget to be seen in Qt Designer you must create a plugin, if you download the source code in the folder examples/designer/plugins there is an example.
On the other hand there is no converter from .py to .ui since the transformation is not possible in all cases.

Custom Qt Widgets with python for Qt Designer

I am trying to write a custom widget for the Qt Designer using only Python. I was following a couple of tutorials I found online but none of them were working or anything close to what I would call to be a minimum working example.
So my questions are:
What steps are involved to make a a custom widget appear in the Widget Box of Qt Designer?
If you can spare the time: Please provide a minimum working example (like a widget with a label in it saying "A truly minimal working Qt custom widget example").
Or is it maybe not possible at all to include a custom widget using only python?
There are very few examples available on how to make a custom widget in pyqt. I wrote this article with a working example: Making a Custom Widget in PyQt
Here is the answer to your question #3: How do I use promote to in Qt Designer in pyqt4?
I am using PySide and it works the same way. This method works directly with your Python custom widget code. You do not need to write any separate plugin code.
After you have promoted your custom widget, you can right click on it and add your signals with "Change signals/slots..."
I would recommend putting all you widgets in a YourCostumWidgetsPack.UI file, and then when you load this file in Qt Designer, in addition to the UI you are working. It will load all your custom widget information.
I found this article to be your answer: https://doc.qt.io/archives/qq/qq26-pyqtdesigner.html
But, I haven't been able to install it in Qt Designer though :D

GUI Programming Pyqt

I made a gui using Pyqt earlier. I did all the coding on the terminal in Linux. Now I am going to make a big project in Pyqt.
Is there any sdk which will help me to overcome the coding part, so I can just drag and drop the items? I know about qt designer, but I don't know how I should write and integrate it with Python.
Do you have suggestions on what program to use for this?
Qt Designer when coupled with PyQt4 is usually used only for the layout process, as opposed to defining signals, buddies, etc. You perform the visual layout of your widgets, and save the .ui file.
Using pyuic4, you can then compile the .ui -> .py, and import that into your coded project.
Though there are probably 3 different approaches to using the UI at this point, what I typically do is multiple inheritance. If class MyMainWindow is meant to be a QMainWindow, then I inherit my class from QMainWindow, and the UI class.
Something like this...
pyuic4 myMainWindow.ui -o myMainWindowUI.py
main.py
from PyQt4 import QtGui
from myMainWindowUI import Ui_MainWindow
class MyMainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs)
super(MyMainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
The setupUi method applies your entire UI design to the class and you can now access all of the widgets you designed by their object names.
win = MyMainWindow()
print win.listWidget
print win.button1
win.show()
See Creating interfaces visually with designer
if using pyqt4 use pyuic4
if using pyside use pyside-uic
These compile the output from QTDesigner into a python file.
You can google you way to usage of these command line tools but Riverbank is usually a good reference

Python setGeometry function

I am working with QT designer to design .ui files.I converted .ui to .py file using pyuic4 compiler and coded it.
When i try to run it,it does not show the ui file i designed.All ui components on form are overlapped . But if i use setgeometry function to locate those components on form then it works fine.
How are you doing the layout of the form? I am guessing that you are manually positioning and sizing the widgets.
You should rather use the qt layout managers. Here is some documentation on how to do this:
Using Layouts in Qt Designer
Qt Layouts – The Basics

Categories