2 Attachment(s)
Opening another created gui pyqt
Hello,
I would like to know how to open a gui after already having one gui open using pyqt? For example, I have a gui called popup and I want this gui to open another gui once I click the "ok" button. Would I need to use qprocess, qfile, or a some sort of signal slot connection to do this? Attached are the two guis I want to open. Runpopup2 is the first gui that I created and when the user clicks ok, I would like runabbs2 to open and runpopup2 to close. Thank you in advance.
Re: Opening another created gui pyqt
An example portion of code from my own (click event for New User button):
Code:
def slotUserNew(self):
self.
userDialog = QtGui.
QDialog(self
) self.userDialog.ui = UserDialog(self) # caller=self, just if you wanted to call main dialog from the sub-dialog.
self.userDialog.ui.show()
Re: Opening another created gui pyqt
Quote:
Originally Posted by
aladagemre
An example portion of code from my own (click event for New User button):
Code:
def slotUserNew(self):
self.
userDialog = QtGui.
QDialog(self
) self.userDialog.ui = UserDialog(self) # caller=self, just if you wanted to call main dialog from the sub-dialog.
self.userDialog.ui.show()
Ok, so I'm trying to understand how this code works. For the first line in the function, would "userDialog" be the object name of the main window that I am trying to open?
For line 2, would "userDialog.ui" be the name of the program that I am trying to open? What is the second "UserDialog(self)?"
If this helps, the object name of the window that I am trying to open is MainWindow and the object name of the dialog that is already open is Dialog2.
Sorry for all the questions, still very new to pyqt. Thank you.
Re: Opening another created gui pyqt
I'm sorry, I just pasted a code where I used Qt Designer and imported the resulting gui.py (pyuic4 gui.ui gui.py) file. So forget it if you don't use Qt Designer. If you use it, you might want to take a look at here (13.1 Using the Generated Code)
Here's a minimal example where all the stuff is done by code:
Code:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def __init__(self):
lineEdit.setText("Just to fill up the dialog")
layout.addWidget(lineEdit)
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.setWindowTitle("Win2")
def __init__(self):
layout.addWidget(button)
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.setWindowTitle("Win1")
self.connect(button, SIGNAL('clicked()'), self.newWindow)
def newWindow(self):
self.myOtherWindow = OtherWindow()
self.myOtherWindow.show()
if __name__ == "__main__":
mainWindow = MainWindow()
mainWindow.setGeometry(100, 100, 200, 200)
mainWindow.show()
sys.exit(app.exec_())
So the key point is the latter part:
Code:
...
self.connect(button, SIGNAL('clicked()'), self.newWindow)
def newWindow(self):
self.myOtherWindow = OtherWindow()
self.myOtherWindow.show()
In the init of MainWindow, you connect the clicked signal of button to newWindow method.
newWindow method just creates a second window and shows it. That's it.
Re: Opening another created gui pyqt
Re: Opening another created gui pyqt
I have made a new post here which pointed out this thread. I'm sorry if I suddenly revive this thread. Nobody was replying at the thread I made so I thought if replying here would help me with what I'm asking ^^;;
I would like to ask you, aladagemre or jaybstory or anyone who can help, if by making ui through QtDesigner (with eric4 as I'm using it) the 2nd ui form, how do you by chance open it just like I made in the original thread I posted?
Below are parts of the 2nd post I replied within the original post
-----------------------------------
I imported the class of the second form. This is somewhat the codes (but I changed the names of the classes mostly)
form1.py
Code:
from PyQt4.QtCore import pyqtSignature
from Ui_form1 import Ui_MainWindow
from ui.form2 import FormTwo
"""
Class documentation goes here.
"""
def __init__(self, parent = None):
"""
Constructor
"""
self.setupUi(self)
@pyqtSignature("")
def on_button_released(self):
"""
Slot documentation goes here.
"""
FT = FormTwo()
FT.show()
form2.py
Code:
from PyQt4.QtCore import pyqtSignature
from Ui_form2 import Ui_Dialog
"""
Class documentation goes here.
"""
def __init__(self, parent = None):
"""
Constructor
"""
self.setupUi(self)
__init__.py
Code:
from PyQt4 import QtCore, QtGui
from ui.form1 import FormOne
if __name__ == "__main__":
import sys
ui = FormOne()
ui.show()
sys.exit(app.exec_())
There is a new problem though that the 2nd form doesn't stay at all and closes immediately after I click the button on the first form. Would it be alright to ask how to correct and solve this?
-----------------------------------
Would it be alright if I ask what I'm doing wrong or something to solve this problem by chance? Thank you *bows deeply*
Re: Opening another created gui pyqt
You might try to put a "self." in front of your Form object name:
Code:
self.FT = FormTwo()
self.FT.show()
If you don't put it, garbage collection will remove that object as soon as setupUi method finishes. So the window will disappear.
But if you put a self there, object is kept in the memory so it won't disappear.