PyQt5 - Switch between multiple QMainWindows using a stacked widget - python

So I am making a PyQt5 game and have created different .py files using Qt Designer for a start screen, level selector, and levels. So basically all the files represent the various windows which I can open/run individually using the standard:
app = QtWidgets.QApplication(sys.argv)
Level_Window = QtWidgets.QMainWindow()
ui = LevelWindow(Level_Window)
Level_Window.show()
sys.exit(app.exec_())
Now, how can I make the actual window switching to work? I was thinking to have another py file that has a window manager class with a stacked widget containing all these other windows. Also other windows should be able to access this stacked widget to like switch to a desired window when user clicks something. Can someone explain how I can accomplish this?

Related

QTextEdit / whole UI of PyQt5 behaves differently on Android when virtual keyboard active

I have a small PyQt5 / pyqtdeploy based app that runs on desktop systems as well as on Android. Inside the MainWindow there is a QTextEdit widget with its textChanged signal connected to some method.
Everything seems to work well on Android until I enter the QTextEdit instance and the virtual keyboard pops up. I can enter text but on_textChanged() will only be called when I leave the text field and thus the keyboard disappears.
It also looks like as the whole update() method of my MainWindow doesn't get called as well.
Is the Qt event system somehow disabled when the virtual keyboard is active?
(I'm using PyQt5.15.0 on Linux and Android 9)

How to open a separate UI Window from QMainWindow?

So I have a MainWindow.ui in the class QMainWindow. I also have a separate QWidget and QDialog window (not sure which one is better to use) that I would like to open by using the menu bar from MainWindow.ui. I know that I can simply do something like:
self.main_layout.addWidget(self.form_widget)
or
flashC = flashCard()
flashC.show()
But I want to do this in Qt Designer, so that I don't have to change the .py file every time I run pyuic5 (since these changes will be replaced upon running.)
My question is, how do I make it so that QMainWindow can open other windows that I make later from within Qt Designer. Right now I can't access any other windows in my signal/slot editor.
I've been trying to figure this out and the books and other resources that I've looked at haven't pointed me in the right direction. I just want to know if it's possible to call other windows from within Qt Designer.
You can have multiple QMainWindow in your application. To create a second window simply define it in Qt Designer as you have for your current window, then import and create an instance of it.
Remember it will only appear once you call .show()

Showing and hiding multiple windows in PyQt5

I'm working on a project with UI and I started to do it with PyQt5. So far I watched lots of videos read some tutorials and have progress on the project. Since PyQt is a binding of C++ for Python, there are not so many documents for complex UI with lots of windows.(or I couldn't find it and also looked examples of PyQt5). My project contains lots of windows. I'm trying to take some arguments from a user and due to user arguments optimization algorithm will work. So the project contain's these windows
Login Window(Qwidgets) {first view}
Login Sign up Window (Qwidgets) {if user don't have an account second view}
If user logs into system Tabwidget will be shown with 4 subtab view and these tabs will take arguments from a user. In this widget, there is already 2 subtab have already values that user can choose but also there are buttons which open new Qwidget class windows with OK and CANCEL button. One tab for choosing a file directory and I used QfileDialog class here. And last tab taking last arguments from a user and opening the file that user has chosen in 3rd tab.
After Tab view, I opened a Plotview, with the file that user have chosen, and in that window user by drawing some polygons giving argument on plot.{Im using Pyqtgraph library here}
Optimization Algorithm will work it's not connected with Pyqt
I have used Qt Designer mostly while designing UI. But later changed it adding some methods to take arguments from a user and for connecting other tabs or other windows.
By defining methods in the class (window) I can open other windows but I cant hide or close an already opened window. if I try to close them all process goes down but I want to make the only that window close. I have that problem in Login Signup window, popup windows for taking extra arguments from a user in 1st and 2nd tabview. My main starts with Login Window. After that in Loginwindow if login success it goes to tabview window. I have been successful by typing mainwindow.hide() and showing tabview window. But after that in all popup windows, I cant close the popup windows that takes arguments from a user.
Since the code is so long I will just put here interested parts.
class LoginUp(object):
def setupUi(self,LoginUp):
self.Buton1.clicked.connect(self.signup)
self.Buton2.clicked.connect(self.tabview)
def signup(self):
# here it shows but user cant close it by just clicking on OK button
# He must click on x button to close which I dont want.
self.signupshow = QtWidgets.QWidget()
self.ui = LoginSignUp()
self.ui.setupUi(self.signupshow)
self.signupshow.show()
def tabview(self): # Here its works
self.tabviewshow = QtWidgets.QWidget()
self.ui_tabview = TabView()
self.ui_tabview.setupUi(self.tabviewshow)
MainWindow.close()
self.tabviewshow.show()
class TabView(object):
def setupUi(self,Form):
self.button3.clicked.connect(self.addargument)
def addargument(self):
# same problem.. after that it popups window that user can give inputs but cant close the window AddArgument class
self.Add = QtWidgets.QWidget()
self.addargumentshow = AddArgument()
self.addargumentshow.setupUi(self.Add)
self.addargumentshow.show()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QWidget()
ui = LoginUp()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
One way to solve this is to use another class as a controller for all of the windows you want to show.
In each window you will send a signal when you want to switch windows and it is up to this controller class to decide how to handle the signal when received and decide which window to show. Any arguments that you need passed can be passed through the signals.
Here is a simplified complete working example.

PySide - GUI inactive zone on the menu bar

I am coding a GUI in PySide for a couple of month now. This soft is half developed using Qt designer and half hard coded. It is a MainWindow soft with a central widget and a lot of docks.
Recently I realized that the menu bar (and a tiny bit of the task bar) had a zone which was 'inactive', ie the mouse can not access the menus directly. This zone is highlighted in green in the picture.
The menus are working, since when I click on the right half of the configure menu button, I can access the File or Edit menu as shown in the following picture
The code translating the Qt designer output into python code is
call pyside-uic mainWindow.ui -o uiMainWindow.py
call pyside-rcc -o fittingRessources_rc.py pathtoressources\fittingRessources.qrc
I have tried to remove the icon of the soft. Move the task bars (in which case the buttons of the task bar are entirely active again). I have tried to remove the icons of the task bar. Nothing made a difference.
I can provide code if necessary, but first I was wondering if any of you already encountered this issue? and how it solved it? since it seems a bit ackward...
Cheers
The error appeared only when I was adding certain docks to the MainWindow using the following code:
self.variablesDock = VariablesDock(self,self.dataCurve)
where self is mainWindow. The VariablesDock class is the following
class VariablesDock(QWidget):
'''
Allows to define variables
'''
def __init__(self, mainWindow, dataCurve):
'''
Constructor
'''
super(VariablesDock,self).__init__(mainWindow)
self.mainWindow = mainWindow
self.dataCurve = dataCurve
self.variablesDockWidget = QtGui.QDockWidget(mainWindow)
... skip some code ...
self.variablesDockWidget.setWidget(self.dockWidgetContents)
mainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.variablesDockWidget)
self.variablesDockWidget.setWindowTitle("Variables")
if I change the line super(VariablesDock,self).__init__(mainWindow) into
super(VariablesDock,self).__init__(None)
the error disappeared.
Cheers

modelPanel (Maya) in QT UI using PySide

I am having an issue parenting a modelPanel (Viewport) into a predefined space in a QT Ui created in Designer. When I try to parent the modelPanel and run the script in Maya, it immediately crashes.
Example:
Create a new window in Qt Designer, and drag and drop a vertical layout in the window and save the ui. In the python class when do the parenting i do it as follows:
pm.modelPanel('modelPanelA', p = 'verticalLayout')
The window is drawn and I see the viewport, BUT the window is not scaleable. Which it has to be. So back in Qt Designer I got to Form > Lay Out as Grid. Now the vertical layout is scalable. I save the UI, reload the class, and create the window. Maya crashes! Why?

Categories

Resources