GUI Programming Pyqt - python

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

Related

Is it possible to set QApplication style in QtDesigner?

I am currently working on a GUI application using PyQt5 and QtDesigner. As I have to make it multi-platform (at least Ubuntu and W10) I will use the "Fusion" style to make it look similar on both platforms.
I was wondering if one could simply set the QApplication style directly in QtDesigner ? I know that a simple <<.setStyle("Fusion")>> will do the trick in the code, but does it exist within QtDesigner so the lines can be automatically generated ?
I'm trying to learn QtDesigner and how much it can be pushed before going into the code.
No, it is not possible to set the style through Qt Designer.
What Qt Designer does allow is to display the GUI with different styles if you select Form-> Preview -> ...

PyQt5 connect signals method

I'm confused with the signals connections with pyqt5. My signals are working I'm just trying to understand the inheritance.
My imports are:
from PyQt5 import QtCore, QtGui, QtWidgets
Then I have a button
self.button_send_mail = QtWidgets.QPushButton('Resend', self)
self.button_send_mail.clicked.connect(self.button_send_mail_pressed)
If I open QPushButton.py there's no defined method "clicked". From Qt Docs I read that QPushButton inherits from QAbstractButton. I open QAbstractButton.py and there's the method clicked, but why my ide (pycharm 2017 with the pyqt5 modules installed) doesn't list the "clicked" method after I write self.button_send_mail.c...??
And the same happens for .connect but in this case, I have the warning: "cannot find the reference "connect" in function.
So, it works but I'd like to understand better, is it maybe because pyqt is wrapping C functions? and pycharm cannot access to them?
Thanks

how to use qt in python

I am working on a module for 3D slicer. A chunk of the template code is pasted below. It is using qt for GUI. I need to add my own GUI here, But I am not able to find how to add toolbar here. I am not able to find any documentation regarding this. Whenever I google I get PyQt4, is that different from this ? So, my question is please explain the diference between qt and PyQt4 and how can I add toolbar here ?
def __init__(self, widgetClass=None):
self.parent = qt.QFrame()
self.parent.setLayout( qt.QVBoxLayout() )
# TODO: should have way to pop up python interactor
self.buttons = qt.QFrame()
self.buttons.setLayout( qt.QHBoxLayout() )
self.parent.layout().addWidget(self.buttons)
self.addDataButton = qt.QPushButton("Add Data")
self.buttons.layout().addWidget(self.addDataButton)
self.addDataButton.connect("clicked()",slicer.app.ioManager().openAddDataDialog)
self.loadSceneButton = qt.QPushButton("Load Scene")
self.buttons.layout().addWidget(self.loadSceneButton)
self.loadSceneButton.connect("clicked()",slicer.app.ioManager().openLoadSceneDialog)
import statement
import vtk, qt, ctk, slicer
I think the form/GUI is created by tool. You can have a look at this
You can add your PyQt4 code in this, that should work fine.
If the import statement is qt, 2 solution : it use an alias (it would be weird) or it use a pyqt wrapper which allow to use PySide2, PyQt5, PySide and PyQt4 the same base code.
Maybe this one : https://github.com/mottosso/Qt.py
maybe another ... If you have a requirement.txt you should be able to know
please explain the diference between qt and PyQt4
Qt is cross-platform application framework, using C++ extensions.
In order to use it in python you need a wrapper and this is what PyQt, PySide and the rest are.
Check your application documentation and probably you will find if it uses PyQt4,5, PySide or something else.

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

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

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